Ubuntu20.04 编译安装python3.7
1. 编译安装openssl
1.1. 下载对应版本的openssl
Ubuntu20.04内置的openssl版本兼容python3.8,却不支持python3.7,故python3.7若想继续使用openssl,需要装对应版本的openssl。下载链接:https://www.openssl.org/source/
。
Note:
如果不使用对应python版本的openssl,在使用pip下载python包时,若指定的pip源是https协议时,会产生pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available
的错误。
Note:
https://www.openssl.org/source/openssl-1.1.1a.tar.gz
为测试成功的openssl版本。
1.2. 编译安装openssl
下载适合的openssl版本之后,解压之后进行编译。进入openssl文件夹使用./config --prefix=/usr/local/openssl no-zlib
命令配置编译参数,可以看到如下结果。
Using os-specific seed configuration
Creating configdata.pm
Creating Makefile
**********************************************************************
*** ***
*** OpenSSL has been successfully configured ***
*** ***
*** If you encounter a problem while building, please open an ***
*** issue on GitHub <https://github.com/openssl/openssl/issues> ***
*** and include the output from the following command: ***
*** ***
*** perl configdata.pm --dump ***
*** ***
*** (If you are new to OpenSSL, you might want to consult the ***
*** 'Troubleshooting' section in the INSTALL file first) ***
*** ***
**********************************************************************
然后通过make && make install
命令进行编译和安装。
Note:
可以通过echo $?
命令查看编译结果,该命令会返回上一条命令时退出的状态,返回0则证明上一条命令正常退出。
1.3. 检验安装的openssl
进入/usr/local/openssl/bin
目录,使用./openssl version
命令,结果如下。出现的问题是,可执行文件是openssl 1.1.1a
,动态链接库是openssl 1.1.1f
。
wsn@firefly:/usr/local/openssl/bin$ ./openssl version
OpenSSL 1.1.1a 20 Nov 2018 (Library: OpenSSL 1.1.1f 31 Mar 2020)
Note:
解决这个问题需要使用一种优先级更高的方式去指定动态链接库的搜索路径。ubuntu默认的动态链接库的搜索路径为/usr/lib
和/lib
(同时也是最低优先级)<通过/etc/ld.so.conf.d
目录指定<export LD_LIBRARY_PATH=
Note:
通过在.bashrc
文件中添加export LD_LIBRARY_PATH=/usr/local/openssl/lib
Note:
将二进制执行文件openssl
添加到/usr/bin
,使其可以在任何位置使用。
#进入/usr/bin备份旧openssl二进制执行文件
sudo mv openssl openssl.bak
#建立软连接指向新openssl二进制执行文件
sudo ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl
#显示结果
openssl version
OpenSSL 1.1.1a 20 Nov 2018
2. 编译安装python
Note:
在编译安装之前需要安装外部函数库(libffi)的开发链接库软件包,输入以下命令sudo apt-get install libffi-dev
。参考链接:https://blog.csdn.net/wuyepiaoxue789/article/details/117958240。
2.1. 下载python源码
下载链接:https://www.python.org/ftp/python/3.7.6/Python-3.7.6.tgz
。
wget https://www.python.org/ftp/python/3.7.6/Python-3.7.6.tgz
tar -zxvf Python-3.7.6.tgz
cd Python-3.7.6
2.2. 编译python,并指定刚才安装的openssl
./configure --prefix=/usr/local/python3.7.6-with_openssl --with-openssl=/usr/local/openssl
make
sudo make install
3. 结果展示
#进入python安装目录
wsn@firefly:~$ cd /usr/local/python3.7.6-with_openssl/bin/
#查看python安装版本
wsn@firefly:/usr/local/python3.7.6-with_openssl/bin$ python3.7 --version
Python 3.7.6
#查看pip版本
wsn@firefly:/usr/local/python3.7.6-with_openssl/bin$ pip3.7 --version
pip 22.2.2 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)
#pip安装
wsn@firefly:~$ pip3-ssl install numpy
Defaulting to user installation because normal site-packages is not writeable
Collecting numpy
Using cached numpy-1.21.6-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (13.0 MB)
Installing collected packages: numpy
WARNING: The scripts f2py, f2py3 and f2py3.7 are installed in '/home/wsn/.local/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed numpy-1.21.6
成功安装python3.7.6。