在 CentOS 上安装 Python,主要看你是用系统自带 Python还是自己装新版本 Python,不同 CentOS 版本情况不一样:
一、各 CentOS 版本与 Python 的关系
✅ CentOS 7
- 系统自带:Python 2.7
- 适合安装:Python 3.6 ~ 3.9(推荐)
- 安装方式:
yum install python3(通常是 3.6)
- 或编译安装 Python 3.8 / 3.9
- 注意:
✅ CentOS 8(已 EOL,但可用)
- 系统自带:Python 3.6 / 3.8
- 适合安装:Python 3.8 / 3.9
- 使用
dnf 安装更方便
- 已停止官方维护,不建议新项目使用
✅ CentOS Stream 8 / 9(推荐新项目)
- Stream 9 自带 Python 3.9
- 适合安装:Python 3.9 ~ 3.11
- 软件较新,适合生产环境
✅ Rocky Linux / AlmaLinux(CentOS 替代品)
- Rocky 9 / Alma 9
- 自带 Python 3.9
- 推荐用于新服务器
二、一般推荐结论
✅ 如果你是新装服务器
推荐:
- CentOS Stream 9
- 或 Rocky Linux 9 / AlmaLinux 9
- Python 版本:3.9 / 3.10 / 3.11
✅ 如果是老服务器(CentOS 7)
- 保留系统 Python 2.7
- 单独安装 Python 3.9
三、正确安装方式(不破坏系统)
CentOS 7 示例
yum install -y gcc openssl-devel bzip2-devel libffi-devel
wget https://www.python.org/ftp/python/3.9.18/Python-3.9.18.tgz
tar xzf Python-3.9.18.tgz
cd Python-3.9.18
./configure --enable-optimizations
make altinstall
使用:
python3.9 -V
四、总结一句话
CentOS 7 装 Python 3.9 最稳;新机器直接用 CentOS Stream 9 / Rocky 9 + Python 3.10/3.11。
如果你告诉我:
- 用途(学习 / 生产 / 爬虫 / AI)
- 是否必须 CentOS
我可以给你最合适的版本组合。