CentOS 7.9 安装 Python 3.10
CentOS 7.9 安装 Python 3.10
最推荐的方法是源码编译,不会影响系统自带的 Python 2.7 和 Python 3.7。
不要替换 /usr/bin/python 和 /usr/bin/python3,CentOS 7 的很多系统工具依赖 Python 2.7。
1、安装编译依赖
yum -y groupinstall "Development Tools"
yum -y install \
gcc gcc-c++ make wget tar \
openssl-devel bzip2-devel libffi-devel \
zlib-devel xz-devel readline-devel \
sqlite-devel ncurses-devel tk-devel \
gdbm-devel db4-devel expat-devel
2、下载 Python 3.10
cd /usr/local/src
wget https://www.python.org/ftp/python/3.10.18/Python-3.10.18.tgz
如果下载慢,可以先下载上传服务器。
解压:
tar xf Python-3.10.18.tgz
cd Python-3.10.18
3、编译安装
推荐安装到 /usr/local/python3.10
./configure \
--prefix=/usr/local/python3.10 \
--enable-optimizations \
--with-openssl=/usr
然后编译:
make -j$(nproc)
安装:
make install
4、查看版本
/usr/local/python3.10/bin/python3.10 --version
输出类似:
Python 3.10.18
5、配置 PATH(可选)
echo 'export PATH=/usr/local/python3.10/bin:$PATH' >/etc/profile.d/python310.sh
source /etc/profile.d/python310.sh
查看:
python3.10 --version
Python 3.10.18
6、pip
Python3.10 自带 pip:
python3.10 -m pip --version
升级:
python3.10 -m pip install --upgrade pip
查看:
pip3.10 --version
7、创建软链接(推荐)
不要覆盖系统的 python3,新增命令即可:
ln -s /usr/local/python3.10/bin/python3.10 /usr/bin/python3.10
ln -s /usr/local/python3.10/bin/pip3.10 /usr/bin/pip3.10
以后直接使用:
python3.10
pip3.10
8、保持系统 Python 不变
你当前环境:
python -> Python 2.7.18
python3 -> Python 3.7.9
python3.10 -> Python 3.10.18
建议保持这样即可,不要修改:
/usr/bin/python
/usr/bin/python3
这样不会影响 yum、cloud-init、firewalld 等系统组件。
如果希望 python3 默认就是 3.10(谨慎)
可以使用 alternatives 管理:
alternatives --install /usr/bin/python3 python3 /usr/bin/python3 10
alternatives --install /usr/bin/python3 python3 /usr/local/python3.10/bin/python3.10 20
alternatives --config python3
选择 Python 3.10 即可。
不建议修改 python(Python 2.7),否则很容易导致 CentOS 7 的 yum 等系统工具无法正常工作。

浙公网安备 33010602011771号