温馨提示×

centos旧版装python

小樊
33
2026-10-06 07:17:33
栏目: 编程语言

在 CentOS 旧版本(如 CentOS 6 / 7) 上安装 Python,常见有以下几种方式,按推荐程度说明:


一、推荐方式:用 SCL(Software Collections)安装(CentOS 7)

不破坏系统自带 Python(系统依赖 python2)

1. 安装 SCL

yum install -y centos-release-scl

2. 安装 Python 3

例如 Python 3.6 / 3.8:

yum install -y rh-python36
# 或
yum install -y rh-python38

3. 使用 Python

scl enable rh-python36 bash
python --version

退出 shell 后恢复系统 Python


二、源码编译安装(最通用,适合 CentOS 6/7)

可装任意版本(如 3.9 / 3.10)

1. 安装依赖

yum groupinstall -y "Development Tools"
yum install -y zlib-devel bzip2-devel openssl-devel \
ncurses-devel sqlite-devel readline-devel tk-devel \
libffi-devel wget

2. 下载 Python 源码

cd /usr/src
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

3. 编译安装

./configure --enable-optimizations
make -j $(nproc)
make altinstall

使用 altinstall 避免覆盖系统 python

4. 验证

python3.9 --version
pip3.9 --version

三、使用 pyenv(适合多版本管理)

yum install -y git gcc make zlib-devel
curl https://pyenv.run | bash

然后:

pyenv install 3.9.18
pyenv global 3.9.18

⚠ 重要提醒(CentOS 旧版)

  • 不要替换系统 python(yum 依赖 python2)
  • 用 python3 / pip3 而不是 python
  • CentOS 6 已 EOL,建议尽量用 CentOS 7+

如果你能告诉我:

  • CentOS 具体版本(6 / 7)
  • 想装 Python 几(3.6 / 3.9 / 3.11?)

我可以给你精确命令。

0 踩