最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
CentOS Python安装更新及升级
时间:2026-07-26 08:44:55 编辑:袖梨 来源:一聚教程网
Checking Current Python VersionBefore updating Python, verify the installed versions on your CentOS system using:

python --version# Checks Python 2.x (if installed)python3 --version # Checks Python 3.x (default in CentOS 7+)This helps determine whether an update is needed and identifies the current default version.
Updating System PackagesEnsure your system’s package index is up-to-date to avoid compatibility issues during Python installation:
sudo yum update -y# For CentOS 7sudo dnf update -y# For CentOS 8+This step installs the latest security patches and dependencies required for Python.
Installing/Updating Python via Yum/DNF (Recommended for Stability)The easiest way to install or update Python is using CentOS’ native package manager. This method ensures compatibility with system tools but may not provide the latest Python versions.
- For CentOS 7:
sudo yum install epel-release -y# Enable EPEL for additional packagessudo yum install python3 -y # Installs/updates Python 3 (e.g., 3.6) - For CentOS 8+:
sudo dnf install epel-release -ysudo dnf install python3 -y # Installs/updates Python 3 (e.g., 3.9)
After installation, verify the version:
python3 --versionTo set Python 3 as the default python command (optional), use:
sudo alternatives --install /usr/bin/python python /usr/bin/python3 1sudo alternatives --config python # Select Python 3 from the listInstalling/Updating Python from Source (For Latest Versions)If you need a specific Python version (e.g., 3.11) or custom optimizations, compile from source. This method requires manual dependency management but offers flexibility.
- Install Dependencies:
sudo yum groupinstall "Development Tools" -y# Compilers, make, etc.sudo yum install openssl-devel bzip2-devel libffi-devel wget -y# Python build dependencies - Download and Extract Source Code:Visit the Python official website to get the latest version link. For example, for Python 3.11.6:
cd /usr/src# Recommended directory for source filessudo wget https://www.python.org/ftp/python/3.11.6/Python-3.11.6.tgzsudo tar xzf Python-3.11.6.tgzcd Python-3.11.6 - Configure, Compile, and Install:
Note: Usesudo ./configure --enable-optimizations# Optimizes Python performancesudo make altinstall # Avoids overwriting system Python (installs as python3.11)altinstallinstead ofinstallto prevent conflicts with the system’s default Python. - Verify Installation:
python3.11 --version# Replace with your installed version
Managing Multiple Python Versions with pyenvFor users needing multiple Python versions (e.g., 3.8 for legacy apps, 3.11 for new projects), pyenv is a powerful tool. It allows easy switching between versions without affecting system stability.
- Install pyenv:
Add the following to yourcurl https://pyenv.run | bash~/.bashrc(or~/.zshrc) to enable pyenv:
Reload the shell:export PATH="$HOME/.pyenv/bin:$PATH"eval "$(pyenv init --path)"eval "$(pyenv virtualenv-init -)"source ~/.bashrc - Install and Set Python Version:
Verify with:pyenv install 3.11.6# Installs Python 3.11.6pyenv global 3.11.6 # Sets as the default Python for the user
To install a specific version for a project (without changing the global default):python --version# Should show 3.11.6cd /path/to/projectpyenv local 3.8.12# Creates a .python-version file for the project
Creating Virtual Environments (Best Practice for Isolation)After updating Python, always use virtual environments to isolate project dependencies and avoid conflicts.
- Install venv Module (comes with Python 3.3+):
sudo yum install python3-venv -y - Create and Activate a Virtual Environment:
Once activated, install packages usingpython3 -m venv myenv# Creates a virtual environment named 'myenv'source myenv/bin/activate# Activates the environment (changes prompt to (myenv))pip(e.g.,pip install numpy). Deactivate withdeactivatewhen done.
Key Notes for a Safe Update
- Backup Data: Before making system-level changes, back up important files (e.g.,
/etc,/home). - Check Compatibility: Ensure your applications (e.g., web servers, scripts) support the new Python version. Test in a staging environment first.
- Avoid Overwriting System Python: CentOS relies on Python 2/3 for system tools (e.g.,
yum). Always usealtinstallor tools likepyenvto prevent breakage.
相关文章
- 用AI抄你的参考图:教你用反推提示词生成爆款设计图 07-26
- 用AI生成修仙门派视频:教你如何拍宗门弟子修仙日常vlog短视频 07-26
- 零基础学AI绘图:3大美学风格提示词分享 07-26
- Ai绘画教程:1个公式让你画出电影剧照画面(附提示词) 07-26
- 乐可广播剧全集免费在线收听地址在哪 07-26
- 企查查在线查询直达入口-企查查在线查询 07-26