Install Python on Rocky Linux

April 12, 2023


Python is a versatile programming language used for a wide range of applications, including web development, AI, scripting, data science, and much more. It is simple, versatile, and has a large development community.

Python is a cross-platform language and it is available on a majority of the popular operating systems, including Rocky Linux - an enterprise-grade OS designed for stability and ease of use.

In this tutorial, you will learn to install Python on Rocky Linux.

Install Python on Rocky Linux - a tutorial.


Prepare the System

Prepare the system for the Python installation by updating the system package repository. Updating the package repository ensures that the latest available version of the program is installed.

Note: The key feature of Rocky Linux is stability, so the latest version available in the repository may not be the latest released version, but the most stable one. Each stable version is supported for several years.

The default package manager for Rocky Linux is dnf, which is the one we will use in this tutorial. Run the following command to update the package repository and upgrade any available packages:

dnf update -y
Updating the package repository on Rocky Linux.

Specifying the -y flag automatically answers yes to any prompts during the update. If you want to review the prompts, omit the flag.

Install Python

After the update finishes, install Python on Rocky Linux by running the following command:

dnf install python3 -y

The command installs Python and resolves all the required dependencies. After the installation completes, check if the installation was successful by running:

python -V
Check the default Python version.

The output shows which Python version is installed.

Set Default Python Version

In case you have multiple Python versions installed, you can set the default one on your system. Check which Python versions are available on your system by running:

ls -ls /usr/bin/python* & ls -ls /usr/local/bin/python*
Listing all installed Python versions on a system.

The output shows all the available Python versions. To change the default version, use the alternatives command:

alternatives --config python

Note: Make sure that the different versions of Python are added to the alternatives command for it to work. Use the following syntax to add them:

alternatives --install [link] [name] [path] [priority]

For example:

alternatives --install /usr/bin/python python /usr/bin/python3.9.14 1

The command adds the specified binary as an alternative and sets the priority to 1.

In the interactive prompt, enter the number that defines the Python version you want to set as default and press Enter to confirm. The current default version is marked with a +.

After setting the default version, check if the settings are applied by running:

python -V

Set Up a Virtual Environment

A virtual environment in Python is an isolated environment allowing users to install and manage Python packages with their own dependencies, separate from the system's global Python installation.

Python supports multiple environments on a single machine. Having multiple virtual environments is useful when working on multiple projects with different requirements, or when you want to test features without affecting your global Python environment.

Follow the steps below to set up a virtual programming environment:

1. Install Prerequisites

Before setting up a virtual programming environment for Python, make sure to install Rocky Linux Development Tools. The tools are a prerequisite that allow you to build and compile software from the source code.

Run the following command to install Rocky Linux Development Tools:

dnf groupinstall development -y

Wait for the installation to complete.

2. Choose Location

Choose which directory will contain the Python virtual environment. Either select an existing one, or create a new directory using the mkdir command:

mkdir [directory name]

For this tutorial, we have created a new directory called python-test:

mkdir python-test

3. Create Environment

Once you change the current directory to the one where you want to hold the virtual environment, use Python's built-in venv module to create the new virtual environment. The syntax is:

python -m venv [env_name]

Replace [env_name] with a unique name for the environment. For example:

Creating a new virtual environment for Python on Rocky Linux.

The command creates a new directory with the specified name and places a standalone Python installation and the pip package manager in the directory.

Use the ls command to check the files were created in the new directory:

Listing files in a virtual environment.

4. Activate Environment

Before using the virtual environment, you must activate it. The syntax for activating the environment is:

source [env_name]/bin/activate

For example:

Activating a Python virtual environment.

The command invokes the activation script in the bin directory and activates the virtual environment. We did not specify the [env_name] since we are already in the environment's directory.

Upon activation, the prompt changes to the name of your environment, which means it is now active. All Python packages are now installed in the virtual environment's local directory and don't affect your system's global Python environment.

Test Installation

To test the Python installation, we will create a simple script that returns an average value from a list of numbers. Follow the steps below:

Note: If you want to make a different program, follow our guide and create a calculator using Python.

1. A prerequisite for our program is the numpy package. Install the numpy package with pip, the Python package manager:

pip install numpy
Installing the numpy package for Python.

2. Create a new Python file in the directory using a text editor. For example, we will create a file called using the nano text editor:


3. Paste the following code in the file:

import numpy as np
def calculate_average(numbers):
    average = np.mean(numbers)
    return average
if __name__ == '__main__':
    numbers = [1, 3, 5, 7, 9]
    average = calculate_average(numbers)
    print(f"The average number of {numbers} is {average:.2f}")

The code takes a list of numbers as an argument, numpy calculates the average, and returns the result. There is also a test case that uses the calculate_average function to calculate the average of the list and prints the result.

4. Save and close the file.

5. Run the Python program:

Testing the Python installation with a simple script.

6. When you finish working on the project, deactivate the virtual environment using the following command:

Deactivating a virtual environment.


This tutorial explained how to install Python on Rocky Linux and how to set up a virtual environment to test the installation. Python is a highly versatile programming language, and when combined with Rocky Linux, they offer a stable and powerful coding solution.

In our other Python tutorials, learn about string slicing, see how to append a string in Python, or learn to substring a string in Python.

Was this article helpful?
Bosko Marijan
Having worked as an educator and content writer, combined with his lifelong passion for all things high-tech, Bosko strives to simplify intricate concepts and make them user-friendly. That has led him to technical writing at PhoenixNAP, where he continues his mission of spreading knowledge.
Next you should read
How to Read From stdin in Python
February 7, 2023

Learn how to take user input and read from stdin in Python through hands-on examples. This article shows how to read an input stream, read from user input, and from file streams.
Read more
Add Elements to Python Array
February 2, 2023

Although Python doesn't have a built-in array data structure, there are other ways to create and add to arrays. Learn how to add values to lists, array module arrays, and NumPy arrays in this guide.
Read more
How to Find the List Length in Python
January 25, 2023

When working with Python lists, knowing the list length is important. A list length helps describe how many elements are in a list. Read this guide to see how you can find the list length in Python.
Read more
Python Power Operator and Function
January 23, 2023

Python has two ways to calculate the power of a number: using a power operator and a built-in function. Check out this guide to see how to use both through examples.
Read more