安装pylibmc报错解决方案

本文最后更新于:10 个月前

错误及起因

今日运行nbviewer的依赖时,其中有一个包为pylibmc,在安装时出现了错误,错误日志如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
ERROR: Command errored out with exit status 1:
command: /usr/local/anaconda3/envs/python3.8/bin/python -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-rvgaquvc/pylibmc_1484525a14914cc5947b9d2eb1b1d957/setup.py'"'"'; __file__='"'"'/tmp/pip-install-rvgaquvc/pylibmc_1484525a14914cc5947b9d2eb1b1d957/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-lai2n16m/install-record.txt --single-version-externally-managed --compile --install-headers /usr/local/anaconda3/envs/python3.8/include/python3.8/pylibmc
cwd: /tmp/pip-install-rvgaquvc/pylibmc_1484525a14914cc5947b9d2eb1b1d957/
Complete output (28 lines):
/tmp/pip-install-rvgaquvc/pylibmc_1484525a14914cc5947b9d2eb1b1d957/setup.py:98: DeprecationWarning: 'U' mode is deprecated
with open("README.rst", "U", encoding="utf-8") as r:
/tmp/pip-install-rvgaquvc/pylibmc_1484525a14914cc5947b9d2eb1b1d957/setup.py:100: DeprecationWarning: 'U' mode is deprecated
with open("src/pylibmc-version.h", "U", encoding="utf-8") as r:
running install
running build
running build_py
creating build
creating build/lib.linux-x86_64-3.8
creating build/lib.linux-x86_64-3.8/pylibmc
copying src/pylibmc/__init__.py -> build/lib.linux-x86_64-3.8/pylibmc
copying src/pylibmc/autoconf.py -> build/lib.linux-x86_64-3.8/pylibmc
copying src/pylibmc/test.py -> build/lib.linux-x86_64-3.8/pylibmc
copying src/pylibmc/__main__.py -> build/lib.linux-x86_64-3.8/pylibmc
copying src/pylibmc/pools.py -> build/lib.linux-x86_64-3.8/pylibmc
copying src/pylibmc/client.py -> build/lib.linux-x86_64-3.8/pylibmc
copying src/pylibmc/consts.py -> build/lib.linux-x86_64-3.8/pylibmc
running build_ext
building '_pylibmc' extension
creating build/temp.linux-x86_64-3.8
creating build/temp.linux-x86_64-3.8/src
gcc -pthread -B /usr/local/anaconda3/envs/python3.8/compiler_compat -Wl,--sysroot=/ -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -DUSE_ZLIB -I/usr/local/anaconda3/envs/python3.8/include/python3.8 -c src/_pylibmcmodule.c -o build/temp.linux-x86_64-3.8/src/_pylibmcmodule.o -fno-strict-aliasing -std=c99
In file included from src/_pylibmcmodule.c:34:
src/_pylibmcmodule.h:42:10: fatal error: libmemcached/memcached.h: No such file or directory
42 | #include <libmemcached/memcached.h>
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
error: command 'gcc' failed with exit status 1
----------------------------------------
ERROR: Command errored out with exit status 1: /usr/local/anaconda3/envs/python3.8/bin/python -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-rvgaquvc/pylibmc_1484525a14914cc5947b9d2eb1b1d957/setup.py'"'"'; __file__='"'"'/tmp/pip-install-rvgaquvc/pylibmc_1484525a14914cc5947b9d2eb1b1d957/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-lai2n16m/install-record.txt --single-version-externally-managed --compile --install-headers /usr/local/anaconda3/envs/python3.8/include/python3.8/pylibmc Check the logs for full command output.

其中的信息为libmemcached/memcached.h: No such file or directory,标明缺少libmemcached包,根据不同的系统进行安装即可。

解决方案

MACOS系统

macos系统需要先安装homebrew,没安装的话使用以下命令安装,已安装则忽略:

1
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

安装完homebrew之后,即可使用brew命令安装libmemcached

1
brew install libmemcached

之后重新执行pip install pylibmc安装即可。若继续出现错误,则指定一下libmemcached路径,首先可以查询下libmemcached安装位置,一般在/usr/local/Cellar/libmemcached/1.0.18/,但是根据每个人的不同情况略有不同,可以使用如下命令查询:

1
brew list libmemcached

查询到之后,在安装pylibmc时指定libmemcached位置,后面的目录根据自己的情况进行更换即可:

1
pip install pylibmc --install-option="--with-libmemcached=/usr/local/Cellar/libmemcached/1.0.18/

Ubuntu/CentOS系统

Linux下使用以下命令即可(Ubuntu实测有效,CentOS为同理推导,若不行请使用yum查询下具体包名进行安装,有可能为libmemcached-devel):

1
2
sudo apt install libmemcached-dev
sudo yum install libmemcached-dev #或者 sudo yum install libmemcached-devel

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!