sodcalculator

10 Tech How-To

How to Install Python 3 on CentOS

Guide 10-4776 · Updated · Step by step

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

  1. Check the current Python version – Run python3 --version or python --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.
  2. Update the system package list – Run sudo yum update (or sudo dnf update on CentOS 8/9). This ensures your package manager is working and repositories are fresh.
  3. 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.
  4. 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.
  5. Verify the installation – After installation, run python3 --version to confirm. You should see the installed version. Also check pip with pip3 --version (if pip wasn’t installed, run sudo yum install python3-pip or sudo dnf install python3-pip).
  6. 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" and sudo yum install openssl-devel bzip2-devel libffi-devel (on CentOS 8/9 use dnf).
  7. 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).
  8. Extract and compile – Run tar -xzf Python-3.12.4.tgz, then cd Python-3.12.4. Configure with ./configure --enable-optimizations (this takes longer but optimizes the build). Then run make -j 4 (adjust the number after -j to your CPU cores) and sudo make altinstall. Using altinstall prevents overwriting the system’s python3 binary.
  9. 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, so python3.12 -m pip --version should work.
  10. Set up a virtual environment (recommended) – To avoid conflicts, create a virtual environment for your projects: python3.12 -m venv myenv, then activate it with source myenv/bin/activate. This keeps your system Python clean.

Common Mistakes

  • Using make install instead of make altinstall when 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 python instead of python3 on CentOS 7 – the default python command points to Python 2.

Tips

  • Use alternatives to 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 python3 isn’t found after installation, try python3.6 or python3.9 depending on what you installed. Also check your PATH.
  • Missing ssl module – When compiling, if you get errors about ssl, install openssl-devel and re-run configure/make. You may need to clean the build with make clean first.
  • 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 run python3.12 -m ensurepip.
  • Compilation fails with “No module named ‘_ctypes'” – Install libffi-devel and 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-name to 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

The whole Tech How-To bed →