• 网络上的资源太丰富了.也导致每个人的环境和情况都不一样.方法各不相同.
  • 我也是醉了,自己整理一篇实操的记录吧.

环境

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

安装依赖

yum -y install gcc zlib zlib-devel bzip2-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel uuid-devel libffi libffi-devel epel-release

yum -y install openssl-devel openssl11 openssl11-devel

下载python最新版本

# 最新版本可能后期会有更新,所以自行修改
wget https://www.python.org/ftp/python/3.11.4/Python-3.11.4.tgz

# 解压压缩包
tar -xzf Python-3.11.4.tgz

编译和安装

# 进入解压后的目录
cd Python-3.11.0

# 设置编译FLAG,以便使用最新的openssl库
export CFLAGS=$(pkg-config --cflags openssl11)
export LDFLAGS=$(pkg-config --libs openssl11)

# 指定python3的安装目录为 /usr/python3.11 并使用ssl模块
./configure --prefix=/usr/python3.11 --with-ssl

# 源码编译并安装, `-j 4` 是使用4个cpu核心并发
make -j 4 && make install

安装后可能会有一段警告,可以不用理会

Installing collected packages: setuptools, pip
  WARNING: The scripts pip3 and pip3.11 are installed in '/usr/python/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-23.1.2 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

设置python3.11.4为默认python版本

# 可以看到当前是安装的最新版
[root@racknerd-6c0f3d bin]# /usr/python3.11/bin/python3 -V
Python 3.11.4
[root@racknerd-6c0f3d bin]# /usr/python3.11/bin/pip3 -V
pip 23.1.2 from /usr/python3.11/lib/python3.11/site-packages/pip (python 3.11)
# 删除或者备份之前的软连接
mv /usr/bin/python /usr/bin/python-back
# 或
rm -rf /usr/bin/python

# 建立python3的软链接
ln -s /usr/python3.11/bin/python3 /usr/bin/python

# 接下来可以查看到默认版本已经修改成刚刚安装的版本了
[root@racknerd-6c0f3d bin]# python -V
Python 3.11.4


安装python3后会导致yum运行报错,需要修改如下

vim /usr/bin/yum
vim /usr/libexec/urlgrabber-ext-down

将以上2个文件第一行的声明改成: #! /usr/bin/python2

最后修改:2023 年 08 月 09 日
如果觉得我的文章对你有用,请随意赞赏