杰瑞科技汇

CentOS 6.4如何安装Python?

Of course. Working with Python on CentOS 6.4 requires a specific approach because the system's default Python version is very old (Python 2.6). This OS is long past its End of Life (EOL), so modern Python versions are not available in the official repositories.

Here’s a comprehensive guide covering the challenges and the best ways to install and use Python on CentOS 6.4.

The Core Challenge: Outdated Repositories

CentOS 6.4 was released in 2012. Its software repositories (yum) contain Python 2.6 and no version of Python 3. Attempting to install python3 with yum will fail.

# This command will fail on CentOS 6.4
sudo yum install python3

Therefore, you must install Python from source code. The most common and recommended approach is to install Python 3.6, as it's the oldest Python 3 version still receiving security patches and is a good balance for legacy systems.


Recommended Method: Install Python 3.6 from Source

This is the most robust method. It installs Python 3.6 alongside the system's default Python 2.6, without breaking any system dependencies that rely on Python 2.6.

Step 1: Install Required Dependencies

First, you need to install the development tools and libraries required to compile Python from source.

sudo yum groupinstall "Development Tools"
sudo yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel zlib-devel

Step 2: Download Python 3.6 Source Code

We'll download the source tarball for Python 3.6.15 (the final patch release in the 3.6 series) using wget. If wget isn't installed, run sudo yum install wget first.

cd /tmp
wget https://www.python.org/ftp/python/3.6.15/Python-3.6.15.tgz

Step 3: Extract and Compile the Source

Now, extract the archive, configure the build, and compile it.

# Extract the archive
tar -xzf Python-3.6.15.tgz
cd Python-3.6.15
# Configure the build. The --prefix flag sets the installation directory.
# We're installing to /usr/local, a standard location for custom software.
./configure --prefix=/usr/local
# Compile the source code (this can take a few minutes)
make
# Install the compiled code
sudo make altinstall

Why sudo make altinstall and not sudo make install?

  • make install would overwrite the system's python and pip executables in /usr/bin, which is a bad idea as many system scripts depend on Python 2.6.
  • make altinstall installs Python with a different name, like python3.6 and pip3.6, avoiding conflicts with the system's Python.

Step 4: Verify the Installation

Check that the new Python version is installed correctly.

# Check the version
python3.6 --version
# Expected output: Python 3.6.15
# Check the pip version
pip3.6 --version
# Expected output: pip X.X.X from /usr/local/lib/python3.6/site-packages/pip (python 3.6)

You now have a fully functional Python 3.6 environment on your CentOS 6.4 system!


Managing Python Environments (Virtual Environments)

It's highly recommended to use virtual environments to isolate your projects. The built-in venv module is available in Python 3.6.

  1. Create a virtual environment for a project:

    # Create a new directory for your project
    mkdir my_project
    cd my_project
    # Create a virtual environment named 'venv'
    python3.6 -m venv venv
  2. Activate the virtual environment:

    source venv/bin/activate

    Your command prompt will change to show (venv), indicating the environment is active.

  3. Work inside the environment:

    Now, python and pip will point to the versions inside your venv (which are based on Python 3.6).

    (venv) pip install requests
    (venv) python my_script.py
  4. Deactivate the environment:

    When you're done, simply type:

    deactivate

Alternative (Simpler but Less Flexible) Method: Using IUS

The IUS (Inline with Upstream Stable) repository provides newer software for older RHEL-based systems. It's simpler than compiling from source but may not have the exact Python version you want.

  1. Add the IUS Repository:

    sudo yum install -y https://repo.ius.io/ius-release-el6.rpm
  2. Install Python 3.6 from IUS:

    sudo yum install -y python36u python36u-libs python36u-devel python36u-pip
  3. Verify the Installation:

    python3.6 --version
    pip3.6 --version

    This method is faster but gives you less control over the build configuration compared to compiling from source.


Critical Security Warning for CentOS 6.4

CentOS 6.4 reached its End of Life in November 2029. This means it no longer receives security updates.

  • Your system is vulnerable. Any software on it, including Python, is potentially exposed to known security flaws.
  • Python 3.6 is also EOL. It stopped receiving security patches in December 2025.

You should only use this setup for learning purposes, for maintaining legacy applications that cannot be moved, or in a completely isolated network environment. It is not suitable for any production system connected to the internet.

Summary

Method Pros Cons Best For
Compile from Source Full control over version and features; latest patch releases (e.g., 3.6.15); most reliable. More complex; requires installing build dependencies; longer setup time. Serious development, production (if you must stay on CentOS 6), getting a specific version.
IUS Repository Very simple and fast yum installation. Limited to versions in the IUS repo; may not have the latest patch; another external repo to manage. Quick setup, non-critical tasks, users who prefer package managers.

For CentOS 6.4, compiling Python 3.6 from source is the recommended and most robust method. Just be acutely aware of the security implications of using an EOL operating system.

分享:
扫描分享到社交APP
上一篇
下一篇