Linux 下通过编译源代码安装Python的过程中不是特别顺利, 经常会有一些模块编译失败, 特别是SSL的配置是一个问题。
这篇文章是个人编译安装过程中的一些总结, 希望可以帮助其他人少走一些弯路。
安装过程适用于 Redhat 6.5/Redhat 7.2
Openssl的库经常会有兼容性问题, 推荐使用 LibreSSL的库。
https://ftp.openbsd.org/pub/OpenBSD/LibreSSL/libressl-2.9.0.tar.gz
解压缩并切换到 /midware/soft/libressl-2.9.0
$ ./configure --prefix=/usr/local
$ sudo make install
$ sudo vi /etc/ld.so.conf.d/local.conf
添加以下内容
/usr/local/lib
$ sudo ldconfig
$ openssl version
LibreSSL 2.9.0
如果不想改变系统的LD_LIBRARY_PATH, 也可以设置环境变量把这个路径加上去
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tar.xz
解压缩, 然后打开SSL编译
vi Modules/Setup.dist
$ sudo yum install -y libffi, libffi-devel, tcl, tk, tcl-devel, tk-devel, sqlite-devel, bzip2-devel, ncurses-devel, xz-devel, libuuid-devel
$ ./configure --prefix=/midware/python3.7.2
$ make
$ make install
【重要更新】
Python3 内置了 venv 所以不需要安装 virtualenv 了
$ /midware/python3.7.2/bin/python3 -m venv ~/.python-ve
# 激活
$ . venv/bin/activate
# 测试验证
$ python -V
Python 3.7.2
vi $HOME/.config/pip/pip.conf
[global]
trusted-host=mirrors.aliyun.com
index-url=https://mirrors.aliyun.com/pypi/simple/
INFO: Can't locate Tcl/Tk libs and/or headers
INFO: Could not locate ffi libs and/or headers
Python build finished successfully!
The necessary bits to build these optional modules were not found:
_bz2 _curses _curses_panel
_lzma _sqlite3 _tkinter
_uuid readline zlib
安装相关依赖
$ sudo yum install -y libffi, libffi-devel, tcl, tk, tcl-devel, tk-devel, sqlite-devel, bzip2-devel, ncurses-devel, xz-devel, libuuid-devel
重新构建
$ make clean
$ ./configure --prefix=/midware/python3.7.2
$ make
$ make install