在找到 CentOS 8 代替品之前先继续用 CentOS 7 苟着了, Python 3.10 的 5 年支持够用了 End of support 2026-10
安装编译环境
安装必要的依赖包和开发工具
yum install epel-release centos-release-scl -y
yum install devtoolset-9-gcc* -y
yum groupinstall "Development tools" -y
安装 Python 3.10.2 所需的依赖包
# Python 3.10.2 依赖
yum install bzip2-devel ncurses-devel gdbm-devel xz-devel sqlite-devel tk-devel libffi-devel libuuid-devel readline-devel zlib-devel openssl11 openssl11-devel -y
特别注意事项
- Python 3.10 需要 OpenSSL 版本至少为 1.1.1
安装 OpenSSL 1.1.1
Python 将 OpenSSL 用于加密相关功能, 例如 https 通信和消息摘要创建。
以前, OpenSSL 版本 1.0.2 或更高版本在 Python 中可用, 但从 Python 3.10 开始, OpenSSL 版本至少为 1.1.1, 而 CentOS 7 默认包含 OpenSSL 1.0.2, 所以需要从 EPEL 安装 OpenSSL 1.1.1
使用以下命令安装 OpenSSL 1.1.1
yum install epel-release
yum install openssl11 openssl11-devel
export CFLAGS=$(pkg-config --cflags openssl11)
export LDFLAGS=$(pkg-config --libs openssl11)
下载和编译 Python 3.10.2
下载源码
wget https://www.python.org/ftp/python/3.10.2/Python-3.10.2.tar.xz
tar xf Python-3.10.2.tar.xz
cd Python-3.10.2
设置编译参数并编译
# 设置编译使用 openssl 1.1.1 依赖库
sed -i 's/PKG_CONFIG openssl /PKG_CONFIG openssl11 /g' configure
scl enable devtoolset-9 "./configure --enable-optimizations"
scl enable devtoolset-9 "make -j"
注意事项
Python 3.10 需要 gcc 版本大于 5.3
使用系统自带的 gcc 编译会遇到这个错误
gcc: error: unrecognized command line option ‘-fno-semantic-interposition’
我们使用 devtoolset-9 中的 gcc 9.3.1 编译 Python 3.10
[root@centos7 ~]# gcc --version
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44)
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
[root@centos7 ~]# scl enable devtoolset-9 "gcc --version"
gcc (GCC) 9.3.1 20200408 (Red Hat 9.3.1-2)
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
依赖包对应关系
bzip2-devel _bz2
ncurses-devel _curses _curses_panel
gdbm-devel _dbm _gdbm
xz-devel _lzma
sqlite-devel _sqlite3
tk-devel _tkinter
libffi-devel _ctypes
libuuid-devel _uuid
readline-devel readline
zlib-devel zlib
openssl11-devel _hashlib _ssl
# 缺失的依赖库可以对应安装
Python build finished successfully!
The necessary bits to build these optional modules were not found:
_bz2 _curses _curses_panel
_dbm _gdbm _lzma
_uuid _sqlite3 _tkinter
readline zlib
To find the necessary bits, look in setup.py in detect_modules() for the module's name.
The following modules found by detect_modules() in setup.py, have been
built by the Makefile instead, as configured by the Setup files:
_abc pwd time
Failed to build these modules:
_hashlib _ssl
Could not build the ssl module!
Python requires a OpenSSL 1.1.1 or newer
Custom linker flags may require --with-openssl-rpath=auto
检查编译版本号
# 检查编译完成 版本号
[root@centos7 Python-3.10.2]# ./python --version
Python 3.10.2
安装 Python 3.10.2
使用 make install
完成安装, 会自动创建符号链接和手册
# 安装到系统 会执行 commoninstall、bininstall、maninstall 会创建符号链接
make install
[root@centos7 ~]# ls -all /usr/local/bin | grep python
lrwxrwxrwx 1 root root 10 Jan 31 12:43 python3 -> python3.10
-rwxr-xr-x 1 root root 23504344 Jan 31 12:43 python3.10
-rwxr-xr-x 1 root root 3116 Jan 31 12:39 python3.10-config
lrwxrwxrwx 1 root root 17 Jan 31 12:43 python3-config -> python3.10-config
使用 make altinstall
仅安装 Python, 不创建符号链接和手册
# 只执行 commoninstall 不会创建软链和手册相关信息
make altinstall
[root@centos7 ~]# ls -all /usr/local/bin | grep python
-rwxr-xr-x 1 root root 23504344 Jan 31 12:43 python3.10
-rwxr-xr-x 1 root root 3116 Jan 31 12:39 python3.10-config
创建符号链接 (可选)
# 创建软连接
ln -s /usr/local/bin/python3.10 /usr/local/bin/python3
ln -s /usr/local/bin/python3.10-config /usr/local/bin/python3-config
默认安装位置
# 默认安装位置 /usr/local/lib
[root@centos7 ~]# tree -L 1 /usr/local/lib
/usr/local/lib
├── libpython3.10.a
├── pkgconfig
└── python3.10
更新 pip3
python3 -m pip install --upgrade pip
原文
Python 3.10の新機能(その8) OpenSSL 1.1.1が必須に
How to link python3 to use openssl11 / or latest version of openssl (1.1.1) on centos 7
Can’t build Twisted==20.3.0 on Linux using gcc 4.8.5
Can’t build optional modules readline and _curses when compiling Python3.4 from source on CentOS7
Python安装:make install和make altinstall的差别