Last modified: May 26, 2025 By Alexander Williams
Install Python on CentOS RHEL
Python is a popular programming language. Many Linux users need it. This guide shows how to install Python on CentOS and RHEL.
Table Of Contents
Check Existing Python Version
First, check if Python is already installed. Run this command in the terminal:
python --version
For Python 3, use:
python3 --version
If Python is installed, you'll see the version number. If not, proceed to installation.
Install Python from Default Repositories
The easiest way is using default repositories. CentOS/RHEL often come with Python pre-installed.
To install Python 3:
sudo yum install python3
For Python 2 (legacy systems):
sudo yum install python
After installation, verify with python3 --version
.
Install Python from EPEL Repository
For newer Python versions, use the EPEL repository. First, enable EPEL:
sudo yum install epel-release
Then install Python:
sudo yum install python36
Replace "36" with your desired version number.
Install Python from Source
For complete control, install from source. This lets you get the latest version.
First, install dependencies:
sudo yum groupinstall "Development Tools"
sudo yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel
Download Python source code:
wget https://www.python.org/ftp/python/3.9.7/Python-3.9.7.tgz
tar xzf Python-3.9.7.tgz
cd Python-3.9.7
Configure and install:
./configure --enable-optimizations
make -j 4
sudo make altinstall
The --enable-optimizations
flag optimizes Python. make -j 4
speeds up compilation.
Install pip for Python
pip is Python's package manager. Install it with:
sudo yum install python3-pip
For Python 2:
sudo yum install python-pip
Verify pip installation:
pip3 --version
Learn more about installing pip in our detailed guide.
Install Multiple Python Versions
Use pyenv
to manage multiple Python versions. This is useful for testing.
First, install pyenv dependencies:
sudo yum install git gcc zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl-devel
Install pyenv:
curl https://pyenv.run | bash
Follow our pyenv installation guide for complete instructions.
Create Python Virtual Environments
Virtual environments isolate Python projects. Create one with:
python3 -m venv myenv
source myenv/bin/activate
Learn more about Python virtual environments in our tutorial.
Verify Python Installation
After installation, test Python with a simple script:
print("Hello, CentOS/RHEL!")
Save as test.py
and run:
python3 test.py
You should see the output:
Hello, CentOS/RHEL!
Conclusion
Installing Python on CentOS/RHEL is straightforward. You can use default repositories, EPEL, or compile from source.
Remember: Always verify your installation. Consider using virtual environments for project isolation.
For other systems, see our guide on installing Python on Windows, macOS, and Linux.