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:
- Deploy a Rocky Linux 9 server.
- Create a non-root
sudo
user. For instructions, see How to Create a Non-Root Sudo User on Rocky Linux 9
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.
-
Update your system.
CONSOLE$ sudo dnf update $ sudo dnf upgrade
-
Add the EPEL repository to your system
CONSOLE$ sudo dnf install epel-release
-
Install the default Python3 version.
CONSOLE$ sudo dnf install python3
-
Verify the new Python version.
CONSOLE$ python3 --version
Output:
CONSOLEPython 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:
-
Enable the SCL repository.
CONSOLE$ sudo dnf install centos-release-scl
-
Install Python
3.8
from the SCL repository.CONSOLE$ sudo dnf install rh-python38
-
Verify the new Python version.
CONSOLE$ scl enable rh-python38 'python3 --version'
-
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:
-
Install pip for
python3
version.CONSOLE$ sudo dnf install python3-pip
-
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.
-
Create a new
hello-world.py
file using thenano
text editor.CONSOLE$ nano hello-world.py
-
Add the following Python code to the
hello-world.py
file.Pythonprint("Hello, World!")
-
Save and close the file by pressing Ctrl + X, then Y.
-
Execute the
hello-world.py
file using Python.CONSOLE$ python3 hello-world.py
If you installed a specific Python
3.x
version, replace38
withpython3.x
. For instancepython3.8
:CONSOLE$ scl enable rh-python38 'python3 hello-world.py'
Output.
CONSOLEHello, 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.