CentOS7.9 安装 Python3.11

前言
在 windows 机器上开发了几个Python 爬虫,想扔到云服务器上去跑。windows 使用的Python 是3.11,而 CentOS7.9 默认只有 2.7. 所以需要在CentOS7.9 把3.11部署上。

3.11 属于相对较新的版本,没有找到现成可供参考的方案。折腾半天,总算搞定。

正文
1. 确认系统版本

[root@serverAliYun ~]# cat /etc/redhat-release 
CentOS Linux release 7.9.2009 (Core)

2. 安装依赖
这里openssl 部分是强相关的,如果不安装,后续 import ssl 会报错提示找不到 module. 代码中涉及到 https 的部分都无法运行。

cd /etc/pki/rpm-gpg
wget https://archive.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-7

centos7 自带的 yum 仓库源没有 openssl11-libs 软件包,所以要添加 epel 的 repo
# vi /etc/yum.repos.d/epel.repo //创建 epel.repo 文件,内容如下:

[epel]
name=epel-repo
metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=$basearch
failovermethod=priority
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

然后更新

yum -y groupinstall "Development tools"
yum install -y ncurses-devel gdbm-devel xz-devel sqlite-devel tk-devel uuid-devel readline-devel bzip2-devel libffi-devel
yum install -y openssl-devel openssl11 openssl11-devel

3. openssl 环境变量

export CFLAGS=$(pkg-config --cflags openssl11)
export LDFLAGS=$(pkg-config --libs openssl11)

4. 下载python源码包

wget -P /test https://www.python.org/ftp/python/3.11.1/Python-3.11.1.tar.xz

5. 解压 && 移动

tar xvf Python-3.11.1.tar.xz

6. 修改配置文件,vi Modules/Setup,取消以下部分的注释,让Python 在编译时就把 ssl 带上

第147行

_socket socketmodule.c


第215行 和 216行

_ssl _ssl.c $(OPENSSL_INCLUDES) $(OPENSSL_LDFLAGS) $(OPENSSL_LIBS)
_hashlib _hashopenssl.c $(OPENSSL_INCLUDES) $(OPENSSL_LDFLAGS) -lcrypto

7. 安装 && 编译

./configure --prefix=/usr/local/bin/python3.11
make && make install

8. 等待安装完成

Installing collected packages: setuptools, pip
  WARNING: The scripts pip3 and pip3.11 are installed in '/usr/local/sbin/python3.11/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 pip-22.3.1 setuptools-65.5.0
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv

9. 初步验证

[root@serverAliYun python3.11]# /usr/local/bin/python3.11/bin/python3 -V
Python 3.11.1
[root@serverAliYun python3.11]# /usr/local/bin/python3.11/bin/python3
Python 3.11.1 (main, Jan 23 2023, 11:22:16) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
>>> quit()

10. 移除旧软连接,创建新软连接
此时python 还是对应默认的 Python2.7,我们需要把他切换为 Python3 (还有个 Python-config 没有动,暂时保留)

# 查找
ll /usr/bin/ |grep python
ll /usr/bin/ |grep pip

# 删除
rm -rf /usr/bin/python
rm -rf /usr/bin/pip

# 重建
ln -sv /usr/local/bin/python3.11/bin/python3 /usr/bin/python
ln -s /usr/local/bin/python3.11/bin/pip3 /usr/bin/pip

11. 确认效果

[root@serverAliYun ~]# python -V
Python 3.11.1
[root@serverAliYun ~]# pip --version
pip 22.3.1 from /usr/local/bin/python3.11/lib/python3.11/site-packages/pip (python 3.11)
[root@serverAliYun ~]# 

12. 此时python默认已经是3.11,但CentOS yum 还是依赖Python2.7,需要修改 yum 相关配置文件,确保yum 依然能够正常运行

将/usr/bin/yum的顶部的:

!/usr/bin/python  改成  !/usr/bin/python2.7 


将/usr/libexec/urlgrabber-ext-down的顶部的:

/usr/bin/python  改为   /usr/bin/python2.7


将/usr/bin/yum-config-manager的顶部的

#!/usr/bin/python 改为 #!/usr/bin/python2.7  

13. 大功告成,参考文档

点赞