CentOS官方不维护版本,配置python升级方法,无损版

针对 CentOS 8 系统,更新 Python 需要特别注意。CentOS 8 已经进入 EOL (End Of Life) 状态,这意味着官方的 yumdnf 源已经不再提供更新。因此,我们通常会使用替代源,如 Vault 或第三方源(如 AlmaLinux/Rocky Linux 的源)。

下方是文章发布时能使用的源

复制代码
# CentOS-Vault.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client.  You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the
# remarked out baseurl= line instead.

[base]
name=CentOS-8 - Base
baseurl=https://vault.centos.org/8.5.2111/BaseOS/x86_64/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

[appstream]
name=CentOS-8 - AppStream
baseurl=https://vault.centos.org/8.5.2111/AppStream/x86_64/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

[extras]
name=CentOS-8 - Extras
baseurl=https://vault.centos.org/8.5.2111/extras/x86_64/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

[centosplus]
name=CentOS-8 - Plus
baseurl=https://vault.centos.org/8.5.2111/centosplus/x86_64/os/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

[PowerTools]
name=CentOS-8 - PowerTools
baseurl=https://vault.centos.org/8.5.2111/PowerTools/x86_64/os/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

核心原则不变:绝对不要删除或替换系统自带的 Python (/usr/bin/python3) ,否则 dnf 等系统工具将无法工作。

步骤 1: 安装编译依赖

首先,你需要安装编译 Python 源码所需的工具和库。由于 CentOS 8 EOL,你可能需要先配置好可用的源(例如,指向 Vault 源)。

复制代码
# 更新软件包索引
sudo dnf update -y

# 安装编译工具和依赖库
sudo dnf groupinstall -y "Development Tools"
sudo dnf install -y \
    zlib-devel \
    bzip2-devel \
    openssl-devel \
    ncurses-devel \
    sqlite-devel \
    readline-devel \
    tk-devel \
    libffi-devel \
    gdbm-devel \
    xz-devel \
    uuid-devel
步骤 2: 安装 pyenv

使用官方的安装脚本是最简单的方式。

复制代码
# 下载并执行安装脚本
curl https://pyenv.run | bash
步骤 3: 配置环境变量

安装完成后,你需要将 pyenv 添加到你的 shell 配置文件中,以便每次登录时都能使用。

复制代码
# 对于 bash shell (CentOS 默认)
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc

# 重新加载 shell 配置
exec "$SHELL"
步骤 4: 安装和使用新的 Python 版本

现在你可以安装任何你需要的 Python 版本了。

复制代码
# 1. 查看所有可安装的 Python 版本
pyenv install --list

# 2. 安装你想要的版本,例如 Python 3.11.6
# (这个过程会从源码编译,需要一些时间)
pyenv install 3.11.6

# 3. 查看已安装的版本
pyenv versions

# 4. 设置全局默认版本 (这会影响你当前用户的所有shell)
pyenv global 3.11.6

# 5. 验证当前 Python 版本
python --version
# 或者
python3 --version
# 输出应该类似: Python 3.11.6

# 6. 查看 pip 版本,它也对应了新的 Python
pip --version
相关推荐
张3蜂10 分钟前
深入理解 Python 的 frozenset:为什么要有“不可变集合”?
前端·python·spring
70asunflower22 分钟前
Emulation,Simulation,Virtualization,Imitation 的区别?
linux·docker
皮卡丘不断更23 分钟前
手搓本地 RAG:我用 Python 和 Spring Boot 给 AI 装上了“实时代码监控”
人工智能·spring boot·python·ai编程
爱打代码的小林39 分钟前
基于 MediaPipe 实现实时面部关键点检测
python·opencv·计算机视觉
极客小云1 小时前
【ComfyUI API 自动化利器:comfyui_xy Python 库使用详解】
网络·python·自动化·comfyui
神梦流1 小时前
GE 引擎的内存优化终局:静态生命周期分析指导下的内存分配与复用策略
linux·运维·服务器
凡人叶枫1 小时前
C++中输入、输出和文件操作详解(Linux实战版)| 从基础到项目落地,避坑指南
linux·服务器·c语言·开发语言·c++
wdfk_prog1 小时前
[Linux]学习笔记系列 -- [drivers][input]serio
linux·笔记·学习
闲人编程1 小时前
Elasticsearch搜索引擎集成指南
python·elasticsearch·搜索引擎·jenkins·索引·副本·分片
痴儿哈哈1 小时前
自动化机器学习(AutoML)库TPOT使用指南
jvm·数据库·python