This guide walks you through installing Python 3 on CentOS, covering both the default repository method and the more up-to-date source compilation approach. You’ll end with a working Python 3 interpreter and pip, ready for scripting or development. Before starting, ensure you have sudo or root access to the system.
What You’ll Need
- A CentOS system (7, 8, or 9) with internet access
- A user account with sudo privileges or direct root access
- Basic familiarity with the command line
Step-by-Step
- Check the current Python version – Run
python3 --versionorpython --version. CentOS 8 and 9 often come with Python 3 pre-installed. If you see a version like 3.6.8, you may want a newer one. If the command isn’t found, proceed with installation. - Update the system package list – Run
sudo yum update(orsudo dnf updateon CentOS 8/9). This ensures your package manager is working and repositories are fresh. - Install Python 3 from the default repository (CentOS 7) – On CentOS 7, run
sudo yum install python3. This installs Python 3.6.8, which is older but stable and supported by the distro. For a newer version, skip to Step 5. - Install Python 3 from the default repository (CentOS 8/9) – On CentOS 8 or 9, run
sudo dnf install python3. This typically installs Python 3.9 or 3.11 depending on the release. This is the simplest method and recommended for most users. - Verify the installation – After installation, run
python3 --versionto confirm. You should see the installed version. Also check pip withpip3 --version(if pip wasn’t installed, runsudo yum install python3-piporsudo dnf install python3-pip). - Install a newer Python from source (optional) – If you need a specific version (e.g., 3.12), you’ll compile it. First, install build dependencies:
sudo yum groupinstall "Development Tools"andsudo yum install openssl-devel bzip2-devel libffi-devel(on CentOS 8/9 usednf). - Download the Python source – Go to python.org/downloads, copy the URL for the version you want (e.g., 3.12.4). Then run
wget https://www.python.org/ftp/python/3.12.4/Python-3.12.4.tgz(adjust the version). - Extract and compile – Run
tar -xzf Python-3.12.4.tgz, thencd Python-3.12.4. Configure with./configure --enable-optimizations(this takes longer but optimizes the build). Then runmake -j 4(adjust the number after -j to your CPU cores) andsudo make altinstall. Usingaltinstallprevents overwriting the system’s python3 binary. - Verify the new installation – Run
python3.12 --version(or the version you installed). It should show the new version. Pip is included by default in modern Python, sopython3.12 -m pip --versionshould work. - Set up a virtual environment (recommended) – To avoid conflicts, create a virtual environment for your projects:
python3.12 -m venv myenv, then activate it withsource myenv/bin/activate. This keeps your system Python clean.
Common Mistakes
- Using
make installinstead ofmake altinstallwhen compiling – this can overwrite the system’s python3 binary and break system tools. - Forgetting to install development libraries (like openssl-devel) before compiling, leading to missing modules like ssl.
- Not updating the package manager first, which can cause repository errors.
- Assuming the default repo version is the latest – on CentOS 7, you get Python 3.6, which is quite old.
- Running
pythoninstead ofpython3on CentOS 7 – the defaultpythoncommand points to Python 2.
Tips
- Use
alternativesto set a default Python version if you need to:sudo alternatives --set python3 /usr/bin/python3.6(adjust paths). - Always use virtual environments for project dependencies – it prevents version conflicts.
- If you’re on CentOS 8/9, the default Python 3 is often sufficient for most tasks; only compile if you need a specific feature or version.
- Check the official Python documentation for any version-specific build requirements.
- Keep your system updated regularly to receive security patches for the installed Python.
Troubleshooting
- Command not found – If
python3isn’t found after installation, trypython3.6orpython3.9depending on what you installed. Also check your PATH. - Missing ssl module – When compiling, if you get errors about ssl, install
openssl-develand re-run configure/make. You may need to clean the build withmake cleanfirst. - pip not installed – On CentOS 7, the python3 package doesn’t include pip. Install it with
sudo yum install python3-pip. For source builds, pip is included, but you can also runpython3.12 -m ensurepip. - Compilation fails with “No module named ‘_ctypes'” – Install
libffi-develand rebuild. - System Python broken after make install – If you accidentally overwrote the system python, you may need to reinstall the system package:
sudo yum reinstall python3(or dnf). - Permission denied when installing packages – Use
pip3 install --user package-nameto install to your user directory, or use a virtual environment.
Frequently Asked Questions
Can I have multiple Python versions installed?
Yes, you can have several versions side by side. When compiling, use make altinstall to install as python3.x without overwriting the default. Use virtual environments to manage project-specific versions.
What’s the difference between yum and dnf?
Yum is the package manager on CentOS 7 and older; dnf is the default on CentOS 8 and 9. Dnf is a fork of yum with better dependency resolution. Both work similarly for basic installs.
Why is the Python version in the CentOS repo so old?
CentOS prioritizes stability over latest features. The repo version is tested and supported for the life of the OS. If you need a newer version, compiling from source is the way to go.
Do I need to install pip separately?
On CentOS 7, yes, install python3-pip. On CentOS 8/9 and source builds, pip is usually included. You can check with pip3 --version.
How do I set Python 3 as the default python command?
You can create an alias in your shell config: alias python=python3. Or use the alternatives system: sudo alternatives --set python /usr/bin/python3 (if configured). Be cautious, as some system scripts expect Python 2.
Is it safe to remove Python 2 from CentOS 7?
No, Python 2 is used by system tools like yum. Removing it can break your system. Leave it installed and use Python 3 for your own work.
Related
General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.
More in this bed
- How to Find Leaked Passwords in Chrome, Edge, Wallet & iCloud Keychain
- Fingerprint Limit Reached on MacBook Pro: Fix It
- WhatsApp Call Errors: Meanings & Fixes
- How to Start a Jam on Spotify (Phone & Desktop)
- Error 0x8007025d: Fix Windows Install Fails
- Net Framework 3.5 Installation Error 0x800f0950