Introduction
Python is a popular, stable, and well-performing programming language. Many different Linux distributions use it, including CentOS 7.
As it doesn’t come preinstalled on CentOS 7, you need to set it up manually.
This guide shows you two options for installing Python 3 on CentOS 7, depending on the version you want to use.
Prerequisites
- Access to a user account with sudo privileges
- The yum package manager, included by default
- A terminal window/command line (Ctrl-Alt-F2)
Installing Python on CentOS 7
There are two ways to install Python 3 on your CentOS system:
- Using the official CentOS package manager.
- Downloading it from the source code.
Option 1: Install Python From Package Manager
The newest Python 3 version available in the package manager is Python 3.6.8. For the latest major release, you need to install the package from the source code. For instructions on how to do so, refer to the next section. If you prefer installing version 3.6.8, follow the steps listed below.
1. Start by updating the repository:
sudo yum update -y
2. Before you move on to installing Python 3 on your CentOS system, make sure it is available in the package repository. If you have CentOS version 7.7 or newer, skip to the next step. If you are using a CentOS release older than 7.7, you need to add IUS, a yum repository that provides newer software versions and includes Python 3.
To add the IUS repository, use the command:
sudo yum install https://repo.ius.io/ius-release-el$(rpm -E '%{rhel}').rpm
Wait for the installation to complete. Then, update the repository:
sudo yum update -y
Note: Not sure which CentOS release you are running? There are many different ways to check the CentOS version.
3. Install Python 3 by running the following command in the terminal window:
sudo yum install -y python3
Wait for the installation to complete. The output should display it has installed python3 and the required dependencies.
4. Verify you have successfully installed Python 3 with:
python3 --version
You should see the version of Python 3 now available on your CentOS system.
Option 2: Install Python From Source Code
To install the latest major release of Python, which is 3.9.6 (at the time of writing), you need to download a copy of the source code and take some additional steps when setting up.
1. First, install the required packages and dependencies:
sudo yum groupinstall "Development Tools" -y
sudo yum install gcc open-ssl-devel bzip2-devel libffi-devel -y
2. Next, use the wget
command to download the desired Python version. If you don’t have wget
, install it by simply running:
sudo yum install wget -y
To download Python 3.9.6, use the command:
wget https://www.python.org/ftp/python/3.9.6/Python-3.9.6.tgz
3. Extract the package:
sudo tar xzf Python-3.9.6.tgz
4. Then, move to the directory:
cd Python 3.9.6
5. Once in the Python directory, compile the source code into an installation package with the following two commands:
./configure --enable-optimizations
make altinstall
The make
command builds the installer package. The altinstall
command instructs the system to create a second installation of this version of Python. Without it, the system would replace the default version of Python.
6. Check the Python version to verify the installation:
python3.9
The system should display:
Conclusion
This guide provided two different ways on how to install Python 3 on CentOS 7. For the newer version of CentOS, check out our article on how to install Python on CentOS 8.
With everything set, you can start with some basics scripts like getting the current time and date in Python or learn file handling in Python with built-in methods, which include creating, opening, and closing files.