关于 Ubuntu 下编译安装 Python 时 OpenSSL 库问题的解决方法
系统环境:Ubuntu 16.04
编译过程:
sudo apt -y build-dep python3.5
pushd /usr/local/src
wget https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tgz
tar xzf Python-3.7.3.tgz && pushd Python-3.7.3
./configure --prefix=/usr/local/python37 --enable-shared --enable-optimization --enable-ipv6 --with-pydebug --with-assertions --with-lto
make
得到如下输出:
Could not build the ssl module!
Python requires an OpenSSL 1.0.2 or 1.1 compatible libssl with X509_VERIFY_PARAM_set1_host().
LibreSSL 2.6.4 and earlier do not provide the necessary APIs, https://github.com/libressl-portable/portable/issues/381
根据输出的提示,是系统 openssl 动态库版本过低,但是事实上,ubuntu 16.04 自带 OpenSSL 版本为 1.0.2g ,理论上来说应该满足 Python 的需求,也可以确认:
$ openssl version
OpenSSL 1.0.2g 1 Mar 2016
$ apt -y install libssl-dev
Reading package lists... Done
Building dependency tree
Reading state information... Done
libssl-dev is already the newest version (1.0.2g-1ubuntu4.15).
通过搜索后我在StackOverflow上得到的答案是:
# OpenSSL 1.0.1g
./config shared --prefix=/my/path --openssldir=/my/path/openssl
make
make install
Python 3.4
export LDFLAGS="-L/my/path/lib/ -L/my/path/lib64/"
export LD_LIBRARY_PATH="/my/path/lib/:/my/path/lib64/"
export CPPFLAGS="-I/my/path/include -I/my/path/include/openssl"
./configure --prefix=/my/path/
make
make install
现在问题如下:
- 为什么系统 Libssl 版本为 1.0.2g 的情况下,Python 编译过程仍有 OpenSSL 版本过低的输出从而无法编译 ssl 库?
- 查阅 Python 的
./configure --help可以看到有一个--with-openssl参数,但我下载 openssl-1.1.1c 源码包后添加该参数生成 Makefile 并进行编译后仍然得到 OpenSSL 版本过低的输出。那么这个--with-openssl参数有何作用?很多其他的程序如 Nginx 通过--with-openssl参数是可以指定静态编译的 OpenSSL 版本的。
感谢指点!
关于 Ubuntu 下编译安装 Python 时 OpenSSL 库问题的解决方法
手动编译是有什么特殊需求吗?不能用 pyenv 这种自动编译的脚本吗?
帖子标题说的是在Ubuntu上编译安装Python时遇到的OpenSSL库问题。这通常是因为系统缺少开发版的OpenSSL头文件和库,导致Python的_ssl和_hashlib模块编译失败。
核心原因就是Python的构建系统找不到openssl/opensslv.h头文件或者libssl.so库。在Ubuntu上,你得先装上libssl-dev这个包,它包含了编译时需要的所有东西。
解决方法很简单,就两步:
- 更新包列表并安装开发包:
sudo apt update sudo apt install libssl-dev - 然后重新配置和编译Python。假设你在Python源码目录里,用
configure时最好明确指定OpenSSL的路径,这样最保险:
那个./configure --with-openssl=/usr make sudo make install--with-openssl=/usr参数就是告诉编译器去系统的标准位置(/usr/include和/usr/lib)找OpenSSL。
搞定这个,再make一般就不会报SSL相关的错了。一句话总结:装好libssl-dev,编译时指定--with-openssl参数指向系统库目录。
#1 非常感谢,这个我还真不知道。。。看起来不错
#1 不过我还是想知道这样编译问题在哪里
pkg-config --libs libssl 看下链接了哪个库
可能你系统里有多个库
是否安装了 libressl
#6 并没有
推荐用 miniconda
我没手动编译过,所以也不知道是什么问题……
3.7 和 3.6 以前的这个依赖不一样了, 既然都选择 Ubuntu 了, 使用
apt-get install python3.7-dev
就可以了
我给阿里云机器升级的 Ubuntu 18 用的这个方法, 反正 python3-dev 是不够用的
Ubuntu 16.04 官方源里面没有 Python 3.7 吧?不过我印象中有个 ppa 源有的。
找到了 https://launchpad.net/~deadsnakes/+archive/ubuntu/ppa
#11 正确的编译方法就是 StackOverflow 上那样编译之后再把编译好的 openssl 的.so 文件加入系统 /etc/ld.so.conf 然后 ldconfig,这个问题即使用 pyenv 也无法避免,它好像不会自动解决 openssl 的依赖问题。现在我就很好奇为什么系统的 openssl 库会被认为版本过低以及那个–with-openssl 参数的作用……
用 Vagrant 下的 Ubuntu 16.04 测试了一下,pyenv 安装 Python 3.7.3。没有出现你所说的问题,一切正常。pyenv 编译很简单,只要确保编译依赖安装齐全。https://github.com/pyenv/pyenv/wiki
vagrantubuntu-xenial ~
❯ which -a openssl
/usr/bin/openssl
vagrantubuntu-xenial ~
❯ openssl version
OpenSSL 1.0.2g 1 Mar 2016
vagrantubuntu-xenial ~
❯ pyenv which python
/home/vagrant/.local/share/pyenv/versions/3.7.3/bin/python


