How to Install Python 3 on CentOS 7
How to Install Python 3 on CentOS 7
CentOS 7, a popular server operating system known for its stability, ships with Python 2.x by default. However, many modern applications require Python 3, which offers significant improvements and new features. Installing Python 3 on CentOS 7 is an essential step for developers and system administrators who want to keep their environments up to date. This guide will walk you through the process of installing Python 3 on CentOS 7.
Prerequisites
Before starting, ensure that you have:
- Root or sudo access to your CentOS 7 machine.
- An updated system to ensure compatibility.
Step 1: Update Your System
Before installing Python 3, it is a good practice to update your system to ensure all existing packages are up to date. Run the following commands:
sudo yum update -y
This will update all packages and dependencies to the latest versions available in the CentOS repositories.
Step 2: Install the Required Development Tools
To compile Python or install certain Python packages, you will need some development tools. Install them with the following command:
sudo yum groupinstall -y "Development Tools"
sudo yum install -y yum-utils
These tools include
gcc
make
Step 3: Enable the EPEL Repository
Python 3 is not available in the default CentOS 7 repositories, so we need to enable the EPEL (Extra Packages for Enterprise Linux) repository:
sudo yum install -y epel-release
The EPEL repository includes many additional packages, including Python 3, that are not present in the standard CentOS repositories.
Step 4: Install Python 3
With the EPEL repository enabled, you can install Python 3. There are two main options depending on the version you want:
- Install Python 3.6 Using: The simplest option is to install Python 3.6:
yum
sudo yum install -y python3
This will install Python 3.6 along with
, the package manager for Python.pip3
- Install a Newer Python Version (e.g., Python 3.9): If you require a newer version of Python, such as Python 3.9, you can use the Software Collections (SCL):
sudo yum install -y centos-release-scl
sudo yum install -y rh-python39
After installation, enable Python 3.9 with:
scl enable rh-python39 bash
This will allow you to use Python 3.9 within the current terminal session.
Step 5: Verify the Installation
To make sure Python 3 is installed correctly, check the version using the following command:
python3 --version
You should see an output like this:
Python 3.6.8
Or, if you installed Python 3.9:
Python 3.9.x
Step 6: Set Python 3 as the Default Version (Optional)
If you want Python 3 to be the default version when running
python
echo 'alias python=python3' >> ~/.bashrc
source ~/.bashrc
This will make
python
python3
Step 7: Installpip
for Python 3
pip
pip
sudo yum install -y python3-pip
Verify the installation with:
pip3 --version
This should display the installed version of
pip
Step 8: Create a Python Virtual Environment (Recommended)
Using a virtual environment is a good practice for Python development, as it isolates project dependencies:
python3 -m venv myprojectenv
Activate the virtual environment with:
source myprojectenv/bin/activate
You will see that your terminal prompt changes, indicating that the virtual environment is active. To deactivate it, simply run:
deactivate
Conclusion
You have successfully installed Python 3 on CentOS 7! Whether you are using Python 3.6 or a newer version like Python 3.9, you now have the tools you need for modern Python development. This setup allows you to leverage the latest Python features and libraries while maintaining the stability of CentOS 7.