安装mysql-python时出现“无法找到-lssl;无法找到-lcrypto”的错误?

38

我在Ubuntu 14.04上安装mysql-python pip(不论是系统范围内还是在虚拟环境中),并且已经安装了MariaDB 10,但尝试使用MariaDB 5.5也出现了相同的错误。如果只安装原版的mysql-server则不会出现此问题。

我已使用apt-get安装了以下内容:

  • build-essential
  • python-dev
  • libmariadbclient-dev(该库取代了libmysqlclient-dev)
  • python-mysqldb

最初我认为这个问题是在虚拟环境中安装时出现的,但后来发现mysql-python无法在系统范围内安装。以下是我在虚拟环境中安装所使用的命令。

virtualenv venv
. venv/bin/activate
pip install mysql-python==1.2.5

In file included from _mysql.c:44:0:
/usr/include/mysql/my_config.h:439:0: warning: "HAVE_WCSCOLL" redefined [enabled by default]
 #define HAVE_WCSCOLL
^
In file included from /usr/include/python2.7/pyconfig.h:3:0,  
                 from /usr/include/python2.7/Python.h:8,
                 from _mysql.c:29:
/usr/include/x86_64-linux-gnu/python2.7/pyconfig.h:911:0: note: this is the location of the     previous definition
 #define HAVE_WCSCOLL 1

^x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions     -Wl,-z,relro -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -D_FORTIFY_SOURCE=2 -g -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security build/temp.linux-x86_64-2.7/_mysql.o -L/usr/lib/x86_64-linux-gnu -lmariadbclient_r -lpthread -lz -lm -lssl -lcrypto -ldl -o build/lib.linux-x86_64-2.7/_mysql.so
/usr/bin/ld: cannot find -lssl
/usr/bin/ld: cannot find -lcrypto
collect2: error: ld returned 1 exit status

error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
----------------------------------------
Cleaning up...
Command /root/env/bin/python -c "import setuptools, tokenize;__file__='/root/env/build/mysql-    python/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-EyhO2v-record/install-record.txt --single-version-externally-managed --compile --install-headers /root/env/include/site/python2.7 failed with error code 1 in /root/env/build/mysql-python
Storing debug log for failure in /root/.pip/pip.log
4个回答

52
您需要安装OpenSSL的开发库。如果您的发行版提供了分离的dev库包,则可以是libssl-dev、libssl-devel。否则,如果没有提供,可以安装完整的openssl包。
(venv)➜  src  pip install mysql-python==1.2.5
Downloading/unpacking mysql-python==1.2.5
  Downloading MySQL-python-1.2.5.zip (108kB): 108kB downloaded
  Running setup.py (path:/home/braiam/src/venv/build/mysql-python/setup.py) egg_info for package mysql-python
    
Installing collected packages: mysql-python
  Running setup.py install for mysql-python
    building '_mysql' extension
    x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Dversion_info=(1,2,5,'final',1) -D__version__=1.2.5 -I/usr/include/mysql -I/usr/include/python2.7 -c _mysql.c -o build/temp.linux-x86_64-2.7/_mysql.o -DBIG_JOINS=1 -fno-strict-aliasing -g -static-libgcc -fno-omit-frame-pointer -fno-strict-aliasing
    In file included from _mysql.c:44:0:
    /usr/include/mysql/my_config.h:439:0: warning: "HAVE_WCSCOLL" redefined
     #define HAVE_WCSCOLL
     ^
    In file included from /usr/include/python2.7/pyconfig.h:3:0,
                     from /usr/include/python2.7/Python.h:8,
                     from _mysql.c:29:
    /usr/include/x86_64-linux-gnu/python2.7/pyconfig.h:911:0: note: this is the location of the previous definition
     #define HAVE_WCSCOLL 1
     ^
    x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-z,relro -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -Wl,-z,relro -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security build/temp.linux-x86_64-2.7/_mysql.o -L/usr/lib/x86_64-linux-gnu -lmariadbclient_r -lpthread -lz -lm -lssl -lcrypto -ldl -o build/lib.linux-x86_64-2.7/_mysql.so
    
Successfully installed mysql-python
Cleaning up...

但是,你有两条消息是我没有的,分别是:

/usr/bin/ld: cannot find -lssl
/usr/bin/ld: cannot find -lcrypto

如果你运行ld -lcrypto --verbose或者ld -lssl --verbose,不会得到这样的结果:

➜  src  ld -lcrypto --verbose | grep succeeded
attempt to open //usr/lib/x86_64-linux-gnu/libcrypto.so succeeded
ld: warning: cannot find entry symbol _start; not setting start address
➜  src  ld -lssl --verbose | grep succeeded
attempt to open //usr/lib/x86_64-linux-gnu/libssl.so succeeded
ld: warning: cannot find entry symbol _start; not setting start address

所以,为了解决这个问题,只需确保您已安装提供两个库的libssl-dev包。


8
对于CentOS 6,使用yum install openssl-devel命令,然后执行pip install MySQL-python即可解决问题。 - chirale
有没有针对macOS的解决方案?它没有libssl-dev软件包,而安装openssl软件包(被认为是libssl的Mac等效版本)也没有任何帮助。 - saran3h
@saran3h https://superuser.com/questions/1089390/how-to-install-libssl-dev-libffi-dev-on-mac-os请问如何在Mac OS上安装libssl-dev和libffi-dev? - Braiam
@Braiam:为什么你要更改这个问题的标签:https://dev59.com/zFsX5IYBdhLWcg3wFsPV?你的工作是在`javascript`之类的东西上。你对`机器学习`了解多少? - stackoverflowuser2010

16

我在 MacOS 上遇到了类似的错误,但对我来说没有出现 "/usr/bin/ld: cannot find -lssl"。

按照以下步骤解决:

步骤1. 确保你已经使用 Homebrew 安装了 openssl。

brew install openssl

第二步。在终端中:

export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib/

对于Linux,您也可以尝试yum install 您缺少的库并将它们添加到LIBRARY_PATH。


11

适用于 Debian 9.x:

apt install libssl-dev


6

对于CentOS:

sudo yum install openssl-devel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接