V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
fourstring
V2EX  ›  Python

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

  •  
  •   fourstring · 2019-05-31 22:49:31 +08:00 · 4232 次点击
    这是一个创建于 1784 天前的主题,其中的信息可能已经有所发展或是发生改变。

    系统环境: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 版本的。

    感谢指点!

    13 条回复    2019-06-03 09:48:05 +08:00
    ysc3839
        1
    ysc3839  
       2019-05-31 23:00:14 +08:00 via Android   ❤️ 1
    手动编译是有什么特殊需求吗?不能用 pyenv 这种自动编译的脚本吗?
    fourstring
        2
    fourstring  
    OP
       2019-05-31 23:02:07 +08:00
    @ysc3839 #1 非常感谢,这个我还真不知道。。。看起来不错
    fourstring
        3
    fourstring  
    OP
       2019-05-31 23:02:48 +08:00
    @ysc3839 #1 不过我还是想知道这样编译问题在哪里
    gstqc
        4
    gstqc  
       2019-05-31 23:22:42 +08:00 via Android   ❤️ 1
    pkg-config --libs libssl 看下链接了哪个库
    可能你系统里有多个库
    fourstring
        5
    fourstring  
    OP
       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
        6
    gstqc  
       2019-05-31 23:33:43 +08:00 via Android   ❤️ 1
    是否安装了 libressl
    fourstring
        7
    fourstring  
    OP
       2019-05-31 23:38:24 +08:00
    @gstqc #6 并没有
    JackieMe
        8
    JackieMe  
       2019-05-31 23:45:24 +08:00 via Android   ❤️ 1
    推荐用 miniconda
    ysc3839
        9
    ysc3839  
       2019-05-31 23:50:36 +08:00   ❤️ 1
    @fourstring 我没手动编译过,所以也不知道是什么问题……
    ClericPy
        10
    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
        11
    ysc3839  
       2019-06-01 18:47:05 +08:00 via Android
    @ClericPy Ubuntu 16.04 官方源里面没有 Python 3.7 吧?不过我印象中有个 ppa 源有的。

    找到了 https://launchpad.net/~deadsnakes/+archive/ubuntu/ppa
    fourstring
        12
    fourstring  
    OP
       2019-06-01 18:49:57 +08:00   ❤️ 1
    @ysc3839 #11 正确的编译方法就是 StackOverflow 上那样编译之后再把编译好的 openssl 的.so 文件加入系统 /etc/ld.so.conf 然后 ldconfig,这个问题即使用 pyenv 也无法避免,它好像不会自动解决 openssl 的依赖问题。现在我就很好奇为什么系统的 openssl 库会被认为版本过低以及那个--with-openssl 参数的作用……
    Kobayashi
        13
    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
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2822 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 12:04 · PVG 20:04 · LAX 05:04 · JFK 08:04
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.