
Installing and Managing Python Versions on Rocky Linux 8: A Complete Guide
Python is an indispensable tool for developers, system administrators, and data scientists. Its clean syntax and vast ecosystem of libraries make it perfect for everything from web development to automation. While Rocky Linux 8 is a powerful and stable server operating system, its default system Python version (Python 3.6) may not be suitable for modern development projects that require newer features.
This guide provides a comprehensive, step-by-step walkthrough for installing and managing newer Python versions on Rocky Linux 8. We will cover how to install a specific version, set it as the default, and follow best practices using virtual environments to ensure a clean and stable system.
Checking the Default Python Installation
Before making any changes, it’s wise to check which version of Python is already installed and configured as the system default. Open your terminal and run the following command:
python3 --version
You will likely see an output similar to Python 3.6.8
. This is the system’s native Python interpreter, which is used by critical system tools like the DNF package manager.
It is crucial that you do not attempt to uninstall or remove the default Python 3.6 installation, as this can break your operating system’s core functionalities. Instead, we will install newer versions alongside it.
Installing a Newer Python Version with DNF
Rocky Linux 8 makes it easy to install more recent versions of Python directly from its official repositories using the dnf
package manager. First, you can list the available Python 3 packages to see your options.
sudo dnf search python3
The output will show several available versions, such as python39
(Python 3.9) or python311
(Python 3.11). To install a specific version, use the dnf install
command. For this example, let’s install Python 3.9.
sudo dnf install python39
The system will resolve dependencies and ask for your confirmation. Press y
to proceed. Once the installation is complete, you can verify it by checking its version directly.
python3.9 --version
This should return Python 3.9.x
, confirming a successful installation.
Managing Multiple Python Versions with alternatives
Now that you have multiple Python versions installed (3.6 and 3.9), your system still defaults to Python 3.6 when you type python3
. To change the default version for your user session or system-wide, you can use the alternatives
utility.
This tool creates symbolic links to manage which version of a program is used by default.
List available Python alternatives: See which versions are configured with the tool.
sudo alternatives --config python3
The output will show you a list of installed Python interpreters, with the current default marked.
Set a new default version: When prompted, enter the selection number corresponding to the version you want to set as the new default (e.g., the number for
/usr/bin/python3.9
).
After making your selection, you can verify that the default has been changed:
python3 --version
The output should now show the new version you selected. Using alternatives
is the recommended way to manage system-wide defaults because it avoids modifying system files directly and can be easily reverted.
Best Practice: Using Python Virtual Environments (venv
)
While changing the system-wide default Python is possible, the professional standard for development is to use virtual environments. A virtual environment is an isolated directory containing a specific Python interpreter and its own set of installed packages. This prevents conflicts between projects and keeps your global package directory clean.
Here’s how to create and use a virtual environment with the newly installed Python 3.9.
Create a project directory:
mkdir my_new_project cd my_new_project
Create the virtual environment: Use the
venv
module of your desired Python version. We’ll name our environmentenv
in this example.python3.9 -m venv env
This command creates a new directory named
env
containing a copy of the Python 3.9 interpreter and its standard libraries.Activate the virtual environment: To start using it, you must activate it.
source env/bin/activate
Your terminal prompt will change to indicate that the environment is active, often by prepending
(env)
.Work in your isolated environment: Now, any Python commands (
python
,pip
) will use the interpreter and packages from within theenv
directory. For example, to install a library:pip install requests
This
requests
library will be installed only insideenv
and will not affect your global system or other projects.Deactivate the environment: When you are finished working, simply run the deactivate command.
bash
deactivate
Your prompt will return to normal, and you will be back to using the system’s default Python interpreter.
Using virtual environments is the most robust and scalable way to manage Python projects. It ensures that each project’s dependencies are isolated and reproducible, preventing the common issue known as “dependency hell.”
Key Takeaways
- Rocky Linux 8 includes Python 3.6 by default for system operations; do not remove it.
- You can easily install newer versions like Python 3.9 or 3.11 using the command
sudo dnf install python39
. - The
alternatives --config python3
command is the proper tool for safely managing the system-wide default Python version. - For all development work, always use Python virtual environments (
venv
) to isolate project dependencies and maintain a stable system.
Source: https://kifarunix.com/install-python-on-rocky-linux-8/