ubuntu14.04 LTS 编译安装Python3.7.1

ubuntu14.04 LTS 编译安装Python3.7.1

之前一直用Python2.7开发,最近由于项目需要,需要上python3.7. 生产环境使用ubuntu14.04 或者 ubuntu12.04.
本以为很简单的事情,结果整整折腾了一天,前后编译安装了几次,还是决定记录一下踩过的坑。

一,查看系统自带的python3 版本
ubuntu14.04 LTS系统自带的python3 版本是3.4.3
root@ansen-VirtualBox:/opt/python3/build/bin# python3 --version
Python 3.4.3

****切记不用删除系统自带的python库, 因为系统的其他模块对系统自带的python有依赖,我第一次安装,时由于卸载了
系统自带的python, 导致了ubuntu桌面的任务栏消失。 折腾了很久。***

二, 安装python依赖的lib, 以下库如果系统已经安装,会自动忽略。
sudo apt-get update; sudo apt-get install --no-install-recommends make build-essential libssl-dev \
zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev xz-utils tk-dev\
 libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev
 
 

 
三,下载python3源码,选择下载安装3.7.1
 Linux 版的 Python 通常以源码编译的方式安装,到 Python 官网下载地址: https://www.python.org/downloads/source/

 sudo curl -O https://www.python.org/ftp/python/3.7.1/Python-3.7.1.tar.xz
 tar -xzvf Python-3.7.1.tar.xz
 
 
四, 安装比较新版本的openssl
在ubuntu14.04上安装python3.7,很多时候提示缺少安装_ssl模块,这导致很多依赖于ssl的模块无法正常安装,如ulib3,requests。
这是也是经过第一次失败得出的经验。 系统自带的libssl-dev库,不能支持python3.7以上的版本,安装完成后会报ssl_模块安装失败。
所以要手动安比较新版本的openssl, 在编译时候指定openssl库的安装位置。 安装步骤参考下面链接。
https://help.dreamhost.com/hc/en-us/articles/360001435926-Installing-OpenSSL-locally-under-your-username
 
五, 编译Python3
1) mkdir -p /opt/python3/build   #设置python3.7.1的安装目录
2) cd Python-3.7.1; ./configure --with-openssl=/home/ansen/ssl --with-ensurepip -prefix=/opt/python3/build

--with-openssl 指定自编译的opensll路径:

如果不指定新版本的openssl库,会默认使用系统自带的openssl库,libssl-dev. 由于版本较低,在configure生产makefile的时候,会发现以下报警。
checking for pkg-config... /usr/bin/pkg-config
checking whether compiling and linking against OpenSSL works... yes
checking for X509_VERIFY_PARAM_set1_host in libssl... no
checking for --with-ssl-default-suites... python

X509_VERIFY_PARAM_set1_host 不支持会导致python3 ssl_模块的编译失败,进而影响很多相关模块的失败。就连pip3也无法正常工作。
如果使用自行安装的新版本openssl, 这个告警小时,提示checking for X509_VERIFY_PARAM_set1_host in libssl... yes

--ensurepip    同时编译安装pip
-prefix        指定make install 的安装路径

3) make
在编译的时候会发现这个错误。 后来经过调查,python3.7.1 的编译已经成功了。但是系统在验证加载的时候,提示找不到libssl.so.0.1 文件
*** WARNING: renaming "_ssl" since importing it failed: libssl.so.1.0.1: cannot open shared object file: No such file or directory

使用strace跟踪编译过程
$ strace -f make 2>&1 | grep libssl.so.1.0.1
[pid  5584] open("/lib/libssl.so.1.0.1", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid  5584] open("/usr/lib/libssl.so.1.0.1", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid  5584] open("/lib64/tls/x86_64/libssl.so.1.0.1", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid  5584] open("/lib64/tls/libssl.so.1.0.1", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid  5584] open("/lib64/x86_64/libssl.so.1.0.1", O_RDONLY) = -1 ENOENT (No such file or   directory)
[pid  5584] open("/lib64/libssl.so.1.0.1", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid  5584] open("/usr/lib64/tls/x86_64/libssl.so.1.0.1", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid  5584] open("/usr/lib64/tls/libssl.so.1.0.1", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid  5584] open("/usr/lib64/x86_64/libssl.so.1.0.1", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid  5584] open("/usr/lib64 /libssl.so.1.0.1", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid  5584] write(1, "*** WARNING: renaming \"_ssl\" sin"..., 131*** WARNING: renaming "_ssl" since importing it failed: libssl.so.1.0.1: cannot open shared object file: No such file or directory
[pid  5584] open("/lib/libssl.so.1.0.1", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid  5584] open("/usr/lib/libssl.so.1.0.1", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid  5584] open("/lib64/libssl.so.1.0.1", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid  5584] open("/usr/lib64/tls/libssl.so.1.0.1", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid  5584] open("/usr/lib64/libssl.so.1.0.1", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid  5584] write(1, "*** WARNING: renaming \"_hashlib\""..., 135*** WARNING: renaming "_hashlib" since importing it failed: libssl.so.1.0.1: cannot open shared object file: No such file or directory

发现系统没尝试去我的openssl安装路径下,查找openssl库。通过设置LD_LIBRARY_PATH 设置新编译的ssl 库的路径。然后重新编译,一切正常。
LD_LIBRARY_PATH=/home/ansen/ssl
这里发现python 的编译脚本设置, --with-openssl 只是设置了编译和连接过程中的库查找位置,后续运行时加载,还是会找系统默认的库查找位置。在python 运行时,手动指定库的优先查找位置, 这个比较麻烦。


4) make install

 


 参考文档:
 
 https://www.python.org/ftp/python/3.7.1/Python-3.7.1.tgz
 https://github.com/pyenv/pyenv/wiki
 https://github.com/pyenv/pyenv
 https://github.com/pyenv/pyenv/wiki/Common-build-problems
 https://help.dreamhost.com/hc/en-us/articles/360001435926-Installing-OpenSSL-locally-under-your-username
 https://www.jianshu.com/p/3789d688e6c0
https://stackoverflow.com/questions/5937337/building-python-with-ssl-support-in-non-standard-location
 https://blog.csdn.net/xietansheng/article/details/84791703
 
 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值