CentOS 8 修改方式
centos8默认安装的python版本是python3.6,当需要更高版本的python3.9或3.11时,我们经常搜索到源码安装然后编写软连接。
[root@manager Python-3.9.0]# python3 --version
Python 3.6.8
[root@manager Python-3.9.0]# pip3 --version
pip 9.0.3 from /usr/lib/python3.6/site-packages (python 3.6)
不过其实可以不用这样,最简单的步骤如下:
- yum安装需要的python版本(如3.9)
# 假如需要3.9
yum install -y python39
- alternatives命令指定默认python
alternatives --config python3
切换完成。
Rocky 9 修改方式
最近发现,使用上述方法
sudo alternatives --config python3
,结果没有回显。搜索了一下解决了。
1. 确认已安装 Python 3.11
首先,确保你的系统上已经安装了 Python 3.11。你可以通过以下命令来检查:
python3.11 --version
如果没有安装 Python 3.11,可以使用以下命令安装(针对 Rocky Linux 9):
sudo dnf install python3.11
2. 使用 alternatives
配置默认 Python 版本
在 Linux 系统中,alternatives
命令可以帮助管理多个版本的程序。你可以通过它来设置默认的 Python 版本。按照以下步骤操作:
添加 Python 版本到 alternatives
系统中
首先,确保将 Python 3.9 和 Python 3.11 都添加到 alternatives
系统中(如果它们尚未添加)。
sudo alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 1
sudo alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 2
这两个命令将 Python 3.9 和 Python 3.11 注册到 alternatives
中,并为它们指定优先级(1 和 2)。数字越小,优先级越高。
配置默认 Python 版本
接下来,你可以使用以下命令来选择默认的 Python 版本:
sudo alternatives --config python3
运行该命令后,系统会显示一个选择列表,类似于:
There are 2 choices for the alternative python3 (providing /usr/bin/python3).
Selection Path Priority Status
------------------------------------------------------------
1 /usr/bin/python3.9 1 auto mode
2 /usr/bin/python3.11 2 manual mode
Press <enter> to keep the current choice[*], or type selection number:
你可以输入对应的数字(比如 2
)来选择 Python 3.11 作为默认版本。
3. 验证更改
完成上述操作后,验证默认 Python 版本是否已经更新:
python3 --version
如果显示 Python 3.11.x
,那么就表示成功切换了默认 Python 版本。