杰瑞科技汇

yum install python会安装哪个版本?

Of course. Here is a comprehensive guide to installing Python on a system using yum, which is the package manager for Red Hat-based Linux distributions like CentOS, RHEL, and Fedora.

yum install python会安装哪个版本?-图1
(图片来源网络,侵删)

Quick Answer (For Modern Systems)

If you are using a modern version of Fedora or CentOS/RHEL 8 and above, the recommended way to get the latest Python 3 is with the dnf package manager, which is the successor to yum.

# For CentOS/RHEL 8+ and modern Fedora
sudo dnf install python3

For yum (common on CentOS/RHEL 7 and older):

# For CentOS/RHEL 7 and older
sudo yum install python3

Detailed Guide

The "best" way to install Python depends heavily on your specific Linux distribution and version. Here’s a breakdown of the scenarios you'll encounter.

The Modern Approach: Using dnf (Recommended)

dnf (Dandified Yum) is the default package manager for Fedora and CentOS/RHEL 8 and later. It's faster and has better dependency resolution than yum. It is the preferred method.

yum install python会安装哪个版本?-图2
(图片来源网络,侵删)

For CentOS/RHEL 8+ and Fedora:

# Update your package lists
sudo dnf update
# Install Python 3
sudo dnf install python3
# Verify the installation
python3 --version
# Expected output: Python 3.x.x

This will typically install the latest stable version of Python 3 available for your distribution's repositories.

The Traditional Approach: Using yum

If you are on an older system like CentOS 7 or RHEL 7, you will use yum. The process is very similar.

For CentOS/RHEL 7:

yum install python会安装哪个版本?-图3
(图片来源网络,侵删)
# Update your package lists
sudo yum update
# Install Python 3
sudo yum install python3
# Verify the installation
python3 --version
# Expected output: Python 3.6.x or similar

Important Note on Python 2: On older systems, you might find that yum itself depends on Python 2. Installing python3 will not affect or remove Python 2. You can check if it's installed with:

python --version
# Expected output: Python 2.7.x

The Common Problem: No package python3 available

This error usually means one of two things:

  1. Your repositories are not enabled: The default repositories on minimal installations of CentOS/RHEL might not include the "PowerTools" or "AppStream" repository, which contains Python 3.
  2. You are on an EOL (End-of-Life) distribution: If you are on a very old version like CentOS 6, Python 3 is not available in the official repositories.

Solution: Enable Required Repositories (for CentOS/RHEL 7)

For CentOS/RHEL 7, you need to enable the PowerTools repository.

# Enable the EPEL repository (Extra Packages for Enterprise Linux)
sudo yum install epel-release
# Enable the PowerTools repository
sudo yum config-manager --enable powertools
# Now try installing Python 3 again
sudo yum install python3

The Advanced Approach: Compiling from Source

If you need a specific version of Python (e.g., Python 3.10) that isn't in your repositories, or if you want to customize the build, you must compile it from source. This gives you full control.

Warning: This is more complex and can lead to dependency issues if not done carefully. It's generally recommended for developers and advanced users.

# Step 1: Install essential build tools and development libraries
sudo yum groupinstall "Development Tools"
sudo yum install openssl-devel bzip2-devel libffi-devel xz-devel wget
# Step 2: Download the desired Python source code
# Go to https://www.python.org/downloads/ to find the latest version
wget https://www.python.org/ftp/python/3.10.11/Python-3.10.11.tgz
# Step 3: Extract the archive
tar -xf Python-3.10.11.tgz
cd Python-3.10.11
# Step 4: Configure, Compile, and Install
# The --enable-optimizations flag creates a more optimized binary but takes longer
./configure --enable-optimizations
sudo make altinstall

Why make altinstall and not make install? Using altinstall is crucial. It installs Python alongside your system's default Python without overwriting it. This prevents breaking system tools that rely on the system's default Python version (e.g., yum).

Verify the installation:

python3.10 --version
# Expected output: Python 3.10.11

Summary Table

Method Command Best For Notes
dnf (Modern) sudo dnf install python3 Fedora, CentOS/RHEL 8+ Recommended. The easiest and most reliable way.
yum (Legacy) sudo yum install python3 CentOS/RHEL 7 May require enabling EPEL and PowerTools.
Compile from Source ./configure && make altinstall Need a specific version or custom build Advanced. Use altinstall to avoid breaking the system.

Final Recommendation

  1. Check your system: Run cat /etc/os-release to see your distribution and version.
  2. Use dnf if you can: If your version supports it, sudo dnf install python3 is the best command.
  3. Use yum for older systems: If dnf is not available, use sudo yum install python3 and be prepared to enable extra repositories.
  4. Use the source only if necessary: If the package manager can't provide the version you need, then compile from source.
分享:
扫描分享到社交APP
上一篇
下一篇