CentOS 7 编译安装 Python 3.10.2

标签: none

旧笔记于 2019年04月08日 整理
因为 CentOS 7上默认的版本是 Python 2.7, 所以我们可以通过添加 IUS 软件源 yum 方式安装 Python 3.6
这个生命周期已经结束了 笔记更新

本笔记于 2022年02月01日 更新
采用编译安装 Python 3.10.2
在找到 centos 8 代替品之前先继续用 centos 7 苟着了, Python 3.10 的 5年支持够用了 End of support 2026-10

旧笔记 yum 方式安装 Python 3.6

# 安装 EPEL 和 IUS 软件源
yum install epel-release
yum install https://repo.ius.io/ius-release-el7.rpm

# 安装 Python3.6 / 安装 pip3
yum install python36u python36u-pip

# 创建 python3 连接符
ln -s /bin/python3.6 /bin/python3

# 创建 pip3 链接符
ln -s /bin/pip3.6 /bin/pip3

# 这样就完成了
# 安装一些常用的支持库
pip3 install requests
pip3 install pymysql
pip3 install xmltodict
pip3 install six

新笔记 编译安装 Python 3.10.2

安装编译环境

yum install epel-release centos-release-scl -y
yum install devtoolset-9-gcc* -y
yum groupinstall "Development tools" -y

# 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

Python 将 OpenSSL 用于加密相关功能,例如 https 通信和消息摘要创建。
以前,OpenSSL 版本 1.0.2 或更高版本在 Python 中可用,但从 Python 3.10 开始,需要 OpenSSL 1.1.1 或更高版本

CentOS 7 系统中使用 OpenSSL 1.0.2,所以如果你想安装 Python 3.10,你需要从 EPEL 安装 OpenSSL 1.1.1。下载 Python 3.10 源代码并解压缩存档后,使用以下命令安装并构建 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)

./configure
make
make install
  • 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

下载 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

编译安装 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"

# 检查编译完成 版本号
[root@centos7 Python-3.10.2]# ./python --version
Python 3.10.2

# 安装到系统 会执行 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


# 只执行 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的差别


扫描二维码,在手机上阅读!

添加新评论