Introduction

Python is a popular programming language known for its versatility and power. Its ease of use, readability, and extensive libraries make it a favorite among developers for various applications, including web development, data analysis, and machine learning.

This guide shows you how to install Python on Rocky Linux 9.

Prerequisites

Before you begin:

Install Python from the EPEL Repository

The Extra Packages for Enterprise Linux (EPEL) repository provides additional packages for Enterprise Linux, including Rocky Linux. Update your system and add the EPEL repository to your system to install Python.

  1. Update your system.

    CONSOLE
    $ sudo dnf update
    $ sudo dnf upgrade
    
  2. Add the EPEL repository to your system

    CONSOLE
    $ sudo dnf install epel-release
    
  3. Install the default Python3 version.

    CONSOLE
    $ sudo dnf install python3
    
  4. Verify the new Python version.

    CONSOLE
    $ python3 --version
    

    Output:

    CONSOLE
    Python 3.9.13
    

Install Specific Python 3.x Version

If you need a specific Python3 version , such as Python 3.8 or Python 3.9, use the Software Collections (SCL) repository to install the required version. Follow these steps below:

  1. Enable the SCL repository.

    CONSOLE
    $ sudo dnf install centos-release-scl
    
  2. Install Python 3.8 from the SCL repository.

    CONSOLE
    $ sudo dnf install rh-python38
    
  3. Verify the new Python version.

    CONSOLE
    $ scl enable rh-python38 'python3 --version'
    
  4. Enable the SCL environment to use new Python 3.8 version.

    CONSOLE
    $ scl enable rh-python38 bash
    

Install pip

pip is the most widely used package manager for Python that allows you to install and manage Python packages from the Python Package Index (PyPI) and other repositories. Install pip for the installed Python version:

  1. Install pip for python3 version.

    CONSOLE
    $ sudo dnf install python3-pip
    
  2. Change python38 to install pip for a specific Python 3.x version.

    CONSOLE
    $ scl enable rh-python38 'python3 -m ensurepip'
    

Create a Hello, World! Application

A Hello, World! application is a simple program that prints Hello, World! to the screen. It's often the first program you write to confirm that Python is working correctly.

  1. Create a new hello-world.py file using the nano text editor.

    CONSOLE
    $ nano hello-world.py
    
  2. Add the following Python code to the hello-world.py file.

    Python
    print("Hello, World!")
    
  3. Save and close the file by pressing Ctrl + X, then Y.

  4. Execute the hello-world.py file using Python.

    CONSOLE
    $ python3 hello-world.py
    

    If you installed a specific Python 3.x version, replace 38 with python3.x. For instance python3.8:

    CONSOLE
    $ scl enable rh-python38 'python3 hello-world.py'
    

    Output.

    CONSOLE
    Hello, World!
    

    This output confirms that Python works as expected.

Conclusion

This guide has shown you how to install Python on Rocky Linux 9, including instructions for installing specific Python 3.x versions, setting up pip, and running a basic "Hello, World!" application. Following these steps, you now have a fully functional Python environment on your Rocky Linux system, ready for development.