Historically, administrators of Red Hat Enterprise Linux (RHEL) used the alternatives command to change the system's default python3
interpreter. This method was technically a challenge to support. Because the system Python is deeply integrated into RHEL, changing its version can break critical components. This post explains why alternatives
is unsupported in RHEL 9 and later, and offers a safer alternative.
Python is a vital system component
Numerous core RHEL components are tightly coupled with the system's version of Python. One prominent example is dnf
, the package manager that installs and updates applications. It's a Python application and relies on the Python version it was tested with. An unauthorized change to the system's default Python environment could render a tool like dnf
inoperable, effectively crippling your ability to manage software on the system, which could lead to a broken operating system. Other critical tools, from firewall managers to system initializers and printer drivers, also often depend on the default system Python and could fail unexpectedly should the Python version unexpectedly change.
Even though RHEL 9 was released in 2022, some users still ask how to change what gets executed when the python3
command is issued. For example, a user might want to configure a system to run /usr/bin/python3.11
rather than, for instance, /usr/bin/python3.9
when python3
is called.
The best solution is to keep the system Python untouched, and instead create a symbolic link that redefines python3
on your system to your desired Python interpreter. This has zero impact on system packages or other users. It's also easy to reverse by simply removing the symbolic link file.
As root you can do:
# sudo ln -s /usr/bin/python3.11 /usr/local/bin/python3
# python3 --version
Python 3.11.11
# ls -la /usr/local/bin/python*
lrwxrwxrwx. 1 root root 19 Jun 23 09:06 /usr/local/bin/python3 -> /usr/bin/python3.11
# echo $PATH
/root/.local/bin:/root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
When the symbolic link is created by root, it is also available for other users and services on the system.
As a regular user:
$ ln -s /usr/bin/python3.11 ~/.local/bin/python3
$ python3 --version
Python 3.11.11
$ ls -laG ~/.local/bin/python3
lrwxrwxrwx. 1 tux 19 Jun 24 05:41 /home/tux/.local/bin/python3 -> /usr/bin/python3.11
$ echo $PATH
/home/tux/.local/bin:/home/tux/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin
You can safely do the same with the python
command.
Both /usr/local/bin/
and ~/.local/bin/
are part of the default PATH
environment variable, so the python3
command will always use a symbolic link defined there.
Use env in scripts for portability
In your scripts, you can use #!/usr/bin/env python3
instead of hard-coding the path #!/usr/bin/python3
. This ensures improved script portability.
Different systems (or even different users on the same system) might have the same program installed in different locations. For example:
- On a standard Linux system, Python might be in
/usr/bin/python3
- A user might have a new version installed in a different location.
- A developer might use a virtual environment (
venv
), which puts apython3
executable in a project-specificbin
directory and prepends that to thePATH
.
A script with a hard-coded path like #!/usr/bin/python3
uses the system Python in all but the first case.
A script starting with #!/usr/bin/env python3
handles all these scenarios. It also respects the user's configured environment. When a developer has intentionally placed a specific version of Python first in their PATH
, env
finds it and uses that version. This allows users to control which interpreter runs a script without modifying the script itself.
The art of managing interpreters
Your Linux system can help you manage the version of Python and other popular programming languages and interpreters. Should you wish to override the default settings, there are tools built into Linux to let you do that in a safe and portable way. Use env
in your scripts, venv
while developing Python applications, maintain your PATH
, and use symlinks for targeted overrides.