关于 Ubuntu 下编译安装 Python 时 OpenSSL 库问题

2019-05-31 22:49:31 +08:00
 fourstring

系统环境: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

现在问题如下:

  1. 为什么系统 Libssl 版本为 1.0.2g 的情况下,Python 编译过程仍有 OpenSSL 版本过低的输出从而无法编译 ssl 库?
  2. 查阅 Python 的./configure --help可以看到有一个--with-openssl参数,但我下载 openssl-1.1.1c 源码包后添加该参数生成 Makefile 并进行编译后仍然得到 OpenSSL 版本过低的输出。那么这个--with-openssl参数有何作用?很多其他的程序如 Nginx 通过--with-openssl参数是可以指定静态编译的 OpenSSL 版本的。

感谢指点!

4253 次点击
所在节点    Python
13 条回复
ysc3839
2019-05-31 23:00:14 +08:00
手动编译是有什么特殊需求吗?不能用 pyenv 这种自动编译的脚本吗?
fourstring
2019-05-31 23:02:07 +08:00
@ysc3839 #1 非常感谢,这个我还真不知道。。。看起来不错
fourstring
2019-05-31 23:02:48 +08:00
@ysc3839 #1 不过我还是想知道这样编译问题在哪里
gstqc
2019-05-31 23:22:42 +08:00
pkg-config --libs libssl 看下链接了哪个库
可能你系统里有多个库
fourstring
2019-05-31 23:29:25 +08:00
@gstqc #4 输出只有-lssl 这应该是链接的系统 libssl 吧。
另外执行 dpkg -l | grep libssl 输出如下
ii libssl-dev:amd64 1.0.2g-1ubuntu4.15 amd64 Secure Sockets Layer toolkit - development files
ii libssl1.0.0:amd64 1.0.2g-1ubuntu4.15 amd64 Secure Sockets Layer toolkit - shared libraries
这说明我系统只有一个 libssl 库啊
gstqc
2019-05-31 23:33:43 +08:00
是否安装了 libressl
fourstring
2019-05-31 23:38:24 +08:00
@gstqc #6 并没有
JackieMe
2019-05-31 23:45:24 +08:00
推荐用 miniconda
ysc3839
2019-05-31 23:50:36 +08:00
@fourstring 我没手动编译过,所以也不知道是什么问题……
ClericPy
2019-06-01 10:17:27 +08:00
3.7 和 3.6 以前的这个依赖不一样了, 既然都选择 Ubuntu 了, 使用
apt-get install python3.7-dev
就可以了
我给阿里云机器升级的 Ubuntu 18 用的这个方法, 反正 python3-dev 是不够用的
ysc3839
2019-06-01 18:47:05 +08:00
@ClericPy Ubuntu 16.04 官方源里面没有 Python 3.7 吧?不过我印象中有个 ppa 源有的。

找到了 https://launchpad.net/~deadsnakes/+archive/ubuntu/ppa
fourstring
2019-06-01 18:49:57 +08:00
@ysc3839 #11 正确的编译方法就是 StackOverflow 上那样编译之后再把编译好的 openssl 的.so 文件加入系统 /etc/ld.so.conf 然后 ldconfig,这个问题即使用 pyenv 也无法避免,它好像不会自动解决 openssl 的依赖问题。现在我就很好奇为什么系统的 openssl 库会被认为版本过低以及那个--with-openssl 参数的作用……
Kobayashi
2019-06-03 09:48:05 +08:00
@fourstring 用 Vagrant 下的 Ubuntu 16.04 测试了一下,pyenv 安装 Python 3.7.3。没有出现你所说的问题,一切正常。pyenv 编译很简单,只要确保编译依赖安装齐全。https://github.com/pyenv/pyenv/wiki

vagrant@ubuntu-xenial ~
❯ which -a openssl
/usr/bin/openssl

vagrant@ubuntu-xenial ~
❯ openssl version
OpenSSL 1.0.2g 1 Mar 2016

vagrant@ubuntu-xenial ~
❯ pyenv which python
/home/vagrant/.local/share/pyenv/versions/3.7.3/bin/python

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/569606

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX