Install Python 3 and Set Up Virtual Environments on AlmaLinux 9 and Rocky Linux 9
Which Python versions the RHEL 9 family ships, how to install python3.11, 3.12 or 3.14 next to the system Python 3.9, and how to isolate projects with venv.
This guide shows which Python versions AlmaLinux 9, Rocky Linux 9 and CentOS Stream 9 provide, how to install a newer interpreter next to the system one, and how to give each project its own virtual environment with venv. Everything comes from the distribution's own repositories; nothing is compiled from source.
CentOS 6 reached end of life in November 2020, and Python 2 has had no upstream support since January 2020. Python 3.6 is end-of-life as well. The RHEL 9 family has no Python 2 package at all, so code that still needs Python 2.7 has to be ported; do not build an unsupported interpreter on a server that faces the internet.
Python versions on the RHEL 9 family
The interpreters are ordinary packages in the AppStream repository and can be installed side by side:
| Package | Command | Notes |
|---|---|---|
python3 | python3 | Python 3.9, the system default. System tools such as dnf depend on it; it is maintained for the whole life of the release. |
python3.11 | python3.11 | Available since release 9.2. |
python3.12 | python3.12 | Available since release 9.4. |
python3.14 | python3.14 | Present in current 9.x AppStream repositories. |
The additional versions have a shorter support period than the default 3.9. There is no python3.13 package. To see what your own mirror offers right now, run:
dnf list python3 python3.11 python3.12 python3.14Requirements
- A server from the RHEL 9 family, updated to the current minor release (
sudo dnf upgrade), so that the newer Python packages are available. - A user with
sudorights for installing packages. Virtual environments themselves are created as a normal user.
Install Python
The default interpreter is usually installed already. Make sure it and its pip are present:
sudo dnf install python3 python3-pipTo add a newer version, install the versioned package and the matching pip. This example uses 3.12; replace the number for another version.
sudo dnf install python3.12 python3.12-pipCheck both:
python3 --version
python3.12 --versionInstalling a newer version does not change what python3 means. It stays Python 3.9, and it must: do not repoint /usr/bin/python3 with symlinks or alternatives, because system tools rely on it. Call the newer interpreter by its full name, python3.12.
A bare python command does not exist by default. If a script insists on it, install the package that provides it. It points to Python 3.9:
sudo dnf install python-unversioned-commandIf you will install packages that compile C extensions, also install a compiler and the headers for the version you use:
sudo dnf install gcc python3.12-develCreate a virtual environment
A virtual environment is a directory with its own python and pip and its own set of installed packages. The venv module that creates it is part of Python's standard library, so the separate virtualenv tool that older tutorials install is not needed.
As your normal user, create a project directory and an environment inside it with the interpreter you want the project to use:
mkdir -p ~/myapp
cd ~/myapp
python3.12 -m venv .venvActivate it:
source .venv/bin/activateYour prompt now starts with (.venv), and python and pip refer to the environment:
python --version
which pythonInstall packages inside the environment
Update pip itself first, then install what the project needs. No sudo: everything goes into .venv.
python -m pip install --upgrade pip
python -m pip install requestsRecord the exact versions so that the environment can be rebuilt elsewhere:
python -m pip freeze > requirements.txtOn another machine, or after deleting .venv, recreate it and install from that file:
python3.12 -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements.txtLeave the environment with:
deactivateUse the environment from services and cron
Activation only adjusts the PATH of your shell. It is not required: a systemd unit or cron job can call the environment's interpreter by its full path, and it will find the environment's packages.
/home/deploy/myapp/.venv/bin/python /home/deploy/myapp/app.pyCommand-line tools installed with pip into the environment live in the same bin directory and can be called the same way.
Maintenance
- Security updates for the interpreters arrive through
sudo dnf upgrade. Packages inside a virtual environment are not touched bydnf; update them withpython -m pip install --upgrade nameinside the environment. - Do not move or copy an environment. It contains absolute paths. If the project directory moves, delete
.venvand recreate it fromrequirements.txt. - Switching Python versions works the same way: delete
.venv, create it again with the other interpreter and reinstall the requirements. - Keep
.venvout of version control. Commitrequirements.txtinstead.
The venv documentation lists further options, and Red Hat describes the packaged interpreters in Installing and using Python.
Frequently asked questions
Why not run sudo pip install?
Installing with pip as root writes into the system's Python directories, next to files that dnf manages. The two do not know about each other, and an overwritten library can break system tools. Use a virtual environment for project dependencies, and dnf for system-wide Python packages such as python3-requests.
Which version should I pick for a new project?
If the default 3.9 is enough for your dependencies, it has the longest support on this release. If a library needs something newer, take the newest packaged version your dependencies support, and keep the server on the current minor release so the interpreter continues to receive fixes.
Can I still get Python 2.7 for a legacy application?
Not from the RHEL 9 repositories. The practical routes are porting the code to Python 3 or isolating the old application in a container that has no direct exposure to the internet until it is ported. Python 2 no longer receives security fixes from the Python project.
Do I need to build Python from source to get a newer version?
Rarely. The packaged versions cover most needs and are patched by the distribution. A source build is yours to maintain: you have to rebuild it for every security release. If you do build one, install it with make altinstall under /usr/local so it cannot replace the system python3.