How to Install Python 3.9 on Rocky Linux

Python is an interpreted high-level programming language. It is a multi-paradigm programming language that emphasizes code readability and simplicity. Python has an extensive standard library that supports many common programming tasks. It can be used as a scripting and object-oriented language for larger projects.

Python was first published in 1991 by Guido van Rossum, making it one of the first programming languages developed in the 1990s. This means that it has had several decades to evolve from a purely procedural language to classes and packages and other object-oriented features such as polymorphism and encapsulation. Today, Python features one of the most extensive sets of modules supported for all kinds of applications, including web development, networking, scientific and numerical computing, etc.

Python is one of the most popular programming languages as it can be used for various purposes such as scripting, web development and system administration. Python is a general-purpose language that allows you to develop front-end applications such as games or graphical user interfaces, and even to program back-office applications and automation tools with frameworks such as pywinauto and server-side applications.

This guide shows you how to install Python 3.9 on a Rocky Linux 8 system. The same steps also apply to RockyLinux 9.

Prerequisites

  • You will need a Rocky Linux 8 or 9 server with an internet connection.
  • A non-root user with sudo privileges.

Step 1. Updating the System

Before we can do any work on our machine, we need to update it in order to get the latest updates and security patches in place. Make sure that your system is up-to-date by running the following command.

sudo dnf check-update
sudo dnf update -y

Step 2. Installing Python 3.9 Using DNF

By default, the Rocky Linux official repo has three different Python versions: 3.6, 3.8, and 3.9. Since we want to install Python 3.9, the latest version, we will use DNF (Dandified YUM) package manager.

sudo dnf install python39 -y

After successfully installing Python 3.9 on our system, we should check the version of the packages by running the command below.

python3.9 --version

You should get something like the one below. As you can see, we have successfully installed Python 3.9.2 on our system.

Check Python version

Installing Python 3.9 from Source

Python is readily available for installation on every operating system. However, some Linux distributions may not have it by default. Python has a dynamic, ever-changing nature, which means you may occasionally need to compile it from the source in order to obtain the most up-to-date features.

First, we need to install some required dependencies to compile Python 3.9 using GCC, and download the latest version of Python from their official website using the wget command.

sudo dnf install unzip wget bzip2-devel openssl-devel libffi-devel openssl-devel -y
sudo dnf groupinstall "Development Tools" -y
wget https://www.python.org/ftp/python/3.9.7/Python-3.9.7.tar.xz

Once the download is finished, extract the downloaded file using the tar command.

tar -xvf Python-3.9.7.tar.xz

Then, change the directory to your newly created directory by running cd followed by the path of the opened directory that you just extracted, which should be ‘Python-3.9.7’ in this case.

cd Python-3.9.7/

Then, we will build Python 3.9 using the default configuration file that the package manager created for us by running the ./configure command.

./configure –enable-optimizations

make -j 4

We use the -j option to specify the number of threads to use for building. This means that we will use 4 threads to build Python 3.9 on our system, which should drastically increase the process’s speed. The default value of this option is 1, so if you do not use -j 4, the process will take much longer. Remember to replace the 4 with your number of cores.

Once the build process is complete, we can install Python 3.9 to our system by running the following command.

sudo make altinstall

We use the altinstall parameter instead of the install parameter because altinstall won’t overwrite our previous installations, so it’s safe to use this parameter at this point.

Once the install process is complete, delete the directory where you downloaded or extracted Python 3.9 using the rm command with the folder path.

rm -rf Python-3.9.7/

To do a final check, just run the command ‘python3.9’ with the -v flag and see if everything is working as expected.

python3.9 --version

You should get something like the one below. As you can see, we have Python 3.9.7, which is newer than the v3.9.2 version on the Rocky Linux official repo in the previous step.

Install Python from source

Testing the Python 3.9 Installation

Now that we have successfully installed Python 3.9, let’s do a quick test to see if everything is working as expected. In this step, we will create a simple helloworld project, compile it and execute it using Python 3.9.

First, let’s create the helloworld project by creating a directory for our new project using the mkdir command in your root directory.

cd && mkdir helloworld

Now, let’s move inside our newly created directory by running cd followed by the name of the directory we just created. In this example, we will go one level deeper than our current directory.

cd helloworld

Create a new file named ‘helloworld’ using the nano editor or whichever text editor you like.

sudo nano helloworld.py

Now, type the following lines of code in the new file. This example prints ‘Hello World!’ to stdout.

print("Vitux Hello World!")

Once done, save and exit the file by pressing CTRL+X, Y, and Enter.

Now, let’s compile and execute the file we just created by running the python3 command with the name of the file as a parameter.

python3 helloworld.py

You should see ‘Vitux Hello World!’ as the output. If you do, congratulations! You have successfully installed and tested Python 3.9 on your Rocky Linux 8 system.

It is safe to say that starting from this point forward. You can use Python 3.9 instead of the older version of Python on your system. Just add ‘python3’ in front of all commands from now on, and you should be ready.

Conclusion

This tutorial taught you how to install Python 3.9 on your Rocky Linux 8 system. We hope this guide was helpful, and please click the ‘Share’ button below if you think so too! We also welcome suggestions for our next articles.