How to Install Python on Linux (AlmaLinux)

Reading Time: 5 minutes


One of the most widely used, versatile, and high-level programming languages is Python. It is used for object-oriented applications ranging from straightforward scripts to complicated machine learning algorithms. Python has an extensive standard library, simple syntax, and predictable semantics. This article will review how to install Python on Linux (AlmaLinux).

Steps to Install Python on Linux (AlmaLinux)

Prerequisites

  • Operating System and Version: AlmaLinux OS 8.
  • Permissions: Access the AlmaLinux 8 system, using the sudo command as root or a user with administrative privileges.
  • System Minimum Requirements: 2 CPUs, 4 GB RAM, and 10 GB of storage.

To learn how to install Python on Linux (AlmaLinux), follow the steps below.

Step #1: Update Your System

Every new AlmaLinux installation requires the packages to be updated to the most recent versions. To update your AlmaLinux system, use the following commands:

sudo dnf update
sudo dnf upgrade

Step #2: Install the Necessary Packages

Use the following command to install the necessary packages required for Python 3.9 installation:

sudo dnf install openssl-devel bzip2-devel libffi-devel

Then, you must install development tools on your AlmaLinux system using the following command:

sudo dnf groupinstall "Development Tools"

The groupinstall command installs the Development Tools, a set of packages typically used for creating software from source code. The Development Tools assist developers in creating, debugging, maintaining, and supporting other programs and applications.

To confirm that gcc is correctly installed, use the following gcc command. You should be aware that gcc is a compiler that creates binary files from source code:

gcc --version

Here is the output:

~]# gcc --version
gcc (GCC) 8.5.0 20210514 (Red Hat 8.5.0-18)

Step #3: Installing Python Using dnf

You can use the following command to install Python 3.9 using dnf:

sudo dnf install python39

Here is the output:

~]# sudo dnf install python39
Last metadata expiration check: 0:15:15 ago on Fri 01 Sep 2023 04:39:18 AM UTC.
Dependencies resolved.
==============================================================================================================================================================================================================================================
 Package                                                        Architecture                                Version                                                                      Repository                                      Size
==============================================================================================================================================================================================================================================
Installing:
 python39                                                       x86_64                                      3.9.16-1.module_el8.8.0+3577+f4afbb0e.1                                      appstream                                       32 k
Installing dependencies:
 python39-libs                                                  x86_64                                      3.9.16-1.module_el8.8.0+3577+f4afbb0e.1                                      appstream                                      8.2 M
 python39-pip-wheel                                             noarch                                      20.2.4-7.module_el8.6.0+2780+a40f65e1                                        appstream                                      1.1 M
 python39-setuptools-wheel                                      noarch                                      50.3.2-4.module_el8.6.0+2780+a40f65e1                                        appstream                                      496 k
Installing weak dependencies:
 python39-pip                                                   noarch                                      20.2.4-7.module_el8.6.0+2780+a40f65e1                                        appstream                                      1.9 M
 python39-setuptools                                            noarch                                      50.3.2-4.module_el8.6.0+2780+a40f65e1                                        appstream                                      870 k
Enabling module streams:
 python39                                                                                                   3.9                                                                                                                              

Transaction Summary
==============================================================================================================================================================================================================================================
Install  6 Packages

Total download size: 13 M
Installed size: 45 M
Is this ok [y/N]: y
Downloading Packages:
(1/6): python39-3.9.16-1.module_el8.8.0+3577+f4afbb0e.1.x86_64.rpm                                                                                                                                            1.7 MB/s |  32 kB     00:00    
(2/6): python39-pip-wheel-20.2.4-7.module_el8.6.0+2780+a40f65e1.noarch.rpm                                                                                                                                     33 MB/s | 1.1 MB     00:00    
(3/6): python39-pip-20.2.4-7.module_el8.6.0+2780+a40f65e1.noarch.rpm                                                                                                                                           24 MB/s | 1.9 MB     00:00    
(4/6): python39-setuptools-50.3.2-4.module_el8.6.0+2780+a40f65e1.noarch.rpm                                                                                                                                    19 MB/s | 870 kB     00:00    
(5/6): python39-setuptools-wheel-50.3.2-4.module_el8.6.0+2780+a40f65e1.noarch.rpm                                                                                                                              20 MB/s | 496 kB     00:00    
(6/6): python39-libs-3.9.16-1.module_el8.8.0+3577+f4afbb0e.1.x86_64.rpm                                                                                                                                        45 MB/s | 8.2 MB     00:00    
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                                                                          40 MB/s |  13 MB     00:00     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                                                                                                      1/1 
  Installing       : python39-setuptools-wheel-50.3.2-4.module_el8.6.0+2780+a40f65e1.noarch                                                                                                                                               1/6 
  Installing       : python39-pip-wheel-20.2.4-7.module_el8.6.0+2780+a40f65e1.noarch                                                                                                                                                      2/6 
  Installing       : python39-libs-3.9.16-1.module_el8.8.0+3577+f4afbb0e.1.x86_64                                                                                                                                                         3/6 
  Installing       : python39-3.9.16-1.module_el8.8.0+3577+f4afbb0e.1.x86_64                                                                                                                                                              4/6 
  Running scriptlet: python39-3.9.16-1.module_el8.8.0+3577+f4afbb0e.1.x86_64                                                                                                                                                              4/6 
  Installing       : python39-setuptools-50.3.2-4.module_el8.6.0+2780+a40f65e1.noarch                                                                                                                                                     5/6 
  Running scriptlet: python39-setuptools-50.3.2-4.module_el8.6.0+2780+a40f65e1.noarch                                                                                                                                                     5/6 
  Installing       : python39-pip-20.2.4-7.module_el8.6.0+2780+a40f65e1.noarch                                                                                                                                                            6/6 
  Running scriptlet: python39-pip-20.2.4-7.module_el8.6.0+2780+a40f65e1.noarch                                                                                                                                                            6/6 
  Verifying        : python39-3.9.16-1.module_el8.8.0+3577+f4afbb0e.1.x86_64                                                                                                                                                              1/6 
  Verifying        : python39-libs-3.9.16-1.module_el8.8.0+3577+f4afbb0e.1.x86_64                                                                                                                                                         2/6 
  Verifying        : python39-pip-20.2.4-7.module_el8.6.0+2780+a40f65e1.noarch                                                                                                                                                            3/6 
  Verifying        : python39-pip-wheel-20.2.4-7.module_el8.6.0+2780+a40f65e1.noarch                                                                                                                                                      4/6 
  Verifying        : python39-setuptools-50.3.2-4.module_el8.6.0+2780+a40f65e1.noarch                                                                                                                                                     5/6 
  Verifying        : python39-setuptools-wheel-50.3.2-4.module_el8.6.0+2780+a40f65e1.noarch                                                                                                                                               6/6 

Installed:
  python39-3.9.16-1.module_el8.8.0+3577+f4afbb0e.1.x86_64                     python39-libs-3.9.16-1.module_el8.8.0+3577+f4afbb0e.1.x86_64                 python39-pip-20.2.4-7.module_el8.6.0+2780+a40f65e1.noarch                         
  python39-pip-wheel-20.2.4-7.module_el8.6.0+2780+a40f65e1.noarch             python39-setuptools-50.3.2-4.module_el8.6.0+2780+a40f65e1.noarch             python39-setuptools-wheel-50.3.2-4.module_el8.6.0+2780+a40f65e1.noarch            

Complete!

After the installation, you can verify that Python 3.9 is installed by checking its version using any of the following commands:

python3.9 --version
python3 --version

Here is the output:

~]# python3.9 --version 
Python 3.9.16
~]# python3 --version 
Python 3.9.16

Step #4: Installing Python from Source

Installing Python 3.9 from the source involves compiling the Python interpreter on your system. This method can be helpful if you want more control over the installation process or if Python 3.9 is unavailable through your package manager.

To download the source code for Python 3.9 from the Python official website, use the wget command:

wget https://www.python.org/ftp/python/3.9.16/Python-3.9.16.tgz

Here is the output:

~]# wget https://www.python.org/ftp/python/3.9.16/Python-3.9.16.tgz
--2023-09-01 05:04:30--  https://www.python.org/ftp/python/3.9.16/Python-3.9.16.tgz
Resolving www.python.org (www.python.org)... 199.232.28.223, 2a04:4e42:44::223
Connecting to www.python.org (www.python.org)|199.232.28.223|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 26333525 (25M) [application/octet-stream]
Saving to: 'Python-3.9.16.tgz'

Python-3.9.16.tgz                                           100%[=========================================================================================================================================>]  25.11M  75.4MB/s    in 0.3s    

2023-09-01 05:04:31 (75.4 MB/s) - 'Python-3.9.16.tgz' saved [26333525/26333525]

Use the following command to extract the downloaded source code:

tar -xf Python-3.9.16.tgz 

Move into the extracted directory:

cd Python-3.9.16

Use the following commands to configure the build and compile Python:

./configure --enable-optimizations
make 

The ./configure script configures the build configuration and checks your system for dependencies. The make command compiles Python.

Once the compilation is complete, you can install Python 3.9 using the following command:

sudo make install

This command will install Python 3.9 to the default location, usually /usr/local/bin/python3.9.

You can check if Python 3.9 is installed correctly using the following command:

python3.9 --version

Here is the output:

~]# python3.9 --version 
Python 3.9.16 

Congratulations on successfully learning how to install Python on Linux — the AlmaLinux distribution specifically. You can clean up the build files and source code directory after installation:

rm -rf Python-3.9.16

Creating a Python Program

To create a simple Python program on AlmaLinux 8, follow the steps below.

Step #1: Create a Python File

Create a Python file using a text editor like vi. Let's create a file called sample.py as an example:

vi sample.py

Step #2: Write Your Python Code

In the text editor, write a simple Python program. For this example, let's create a program that prints "Hello, How to Install Python on Linux (AlmaLinux)!" to the console. To enter insert mode, press the I key and add the following details:

print("Hello, How to Install Python on Linux (AlmaLinux)!")

Press the Esc key to exit the insert mode after adding the details. Then, type ':wq' to save your changes and exit the vi editor.

Step #3: Run the Python Program

You can run the Python program using the following command:

python3 sample.py

You should see the output "Hello, How to Install Python on Linux (AlmaLinux)!" printed on the terminal.

Here is the output:

~]# python3.9 sample.py
Hello, How to Install Python on Linux (AlmaLinux)!

If you want to learn how to install Python on Windows, see the article How to Install Python on Windows.

How to Uninstall Python on Linux (AlmaLinux)

To uninstall Python 3.9 from AlmaLinux, follow the steps below.

Step #1: Check Dependencies

Before uninstalling Python 3.9, ensure no system components or programs depend on it. This can be handled with the rpm command. Use the following command to find out which packages depend on Python 3.9:

rpm -q --whatrequires python3.9

This command will list packages that depend on Python 3.9. Review the list carefully to understand the potential impact of removing Python.

Step #2: Backup Important Data

Before proceeding, ensure you have a complete backup of any essential data on your system. Uninstalling Python can lead to system instability, so it's crucial to have a backup in case something goes wrong.

Step #3: Uninstall Python 3.9

You can use the following command if you still want to uninstall Python 3.9 despite the potential risks. Keep in mind that this can have adverse effects on your system. Expect a list of dependent packages that could also be removed:

sudo dnf remove python39

Here is the output:

~]# sudo dnf remove python39
Dependencies resolved.
==============================================================================================================================================================================================================================================
 Package                                                       Architecture                               Version                                                                        Repository                                      Size
==============================================================================================================================================================================================================================================
Removing:
 python39                                                      x86_64                                     3.9.16-1.module_el8.8.0+3577+f4afbb0e.1                                        @appstream                                      24 k
Removing unused dependencies:
 python39-libs                                                 x86_64                                     3.9.16-1.module_el8.8.0+3577+f4afbb0e.1                                        @appstream                                      31 M
 python39-pip                                                  noarch                                     20.2.4-7.module_el8.6.0+2780+a40f65e1                                          @appstream                                     8.0 M
 python39-pip-wheel                                            noarch                                     20.2.4-7.module_el8.6.0+2780+a40f65e1                                          @appstream                                     1.1 M
 python39-setuptools                                           noarch                                     50.3.2-4.module_el8.6.0+2780+a40f65e1                                          @appstream                                     3.8 M
 python39-setuptools-wheel                                     noarch                                     50.3.2-4.module_el8.6.0+2780+a40f65e1                                          @appstream                                     550 k

Transaction Summary
==============================================================================================================================================================================================================================================
Remove  6 Packages

Freed space: 45 M
Is this ok [y/N]: y
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                                                                                                      1/1 
  Erasing          : python39-setuptools-50.3.2-4.module_el8.6.0+2780+a40f65e1.noarch                                                                                                                                                     1/6 
  Running scriptlet: python39-setuptools-50.3.2-4.module_el8.6.0+2780+a40f65e1.noarch                                                                                                                                                     1/6 
  Erasing          : python39-3.9.16-1.module_el8.8.0+3577+f4afbb0e.1.x86_64                                                                                                                                                              2/6 
  Running scriptlet: python39-3.9.16-1.module_el8.8.0+3577+f4afbb0e.1.x86_64                                                                                                                                                              2/6 
  Erasing          : python39-pip-20.2.4-7.module_el8.6.0+2780+a40f65e1.noarch                                                                                                                                                            3/6 
  Running scriptlet: python39-pip-20.2.4-7.module_el8.6.0+2780+a40f65e1.noarch                                                                                                                                                            3/6 
  Erasing          : python39-libs-3.9.16-1.module_el8.8.0+3577+f4afbb0e.1.x86_64                                                                                                                                                         4/6 
  Erasing          : python39-pip-wheel-20.2.4-7.module_el8.6.0+2780+a40f65e1.noarch                                                                                                                                                      5/6 
  Erasing          : python39-setuptools-wheel-50.3.2-4.module_el8.6.0+2780+a40f65e1.noarch                                                                                                                                               6/6 
  Running scriptlet: python39-setuptools-wheel-50.3.2-4.module_el8.6.0+2780+a40f65e1.noarch                                                                                                                                               6/6 
  Verifying        : python39-3.9.16-1.module_el8.8.0+3577+f4afbb0e.1.x86_64                                                                                                                                                              1/6 
  Verifying        : python39-libs-3.9.16-1.module_el8.8.0+3577+f4afbb0e.1.x86_64                                                                                                                                                         2/6 
  Verifying        : python39-pip-20.2.4-7.module_el8.6.0+2780+a40f65e1.noarch                                                                                                                                                            3/6 
  Verifying        : python39-pip-wheel-20.2.4-7.module_el8.6.0+2780+a40f65e1.noarch                                                                                                                                                      4/6 
  Verifying        : python39-setuptools-50.3.2-4.module_el8.6.0+2780+a40f65e1.noarch                                                                                                                                                     5/6 
  Verifying        : python39-setuptools-wheel-50.3.2-4.module_el8.6.0+2780+a40f65e1.noarch                                                                                                                                               6/6 

Removed:
  python39-3.9.16-1.module_el8.8.0+3577+f4afbb0e.1.x86_64                     python39-libs-3.9.16-1.module_el8.8.0+3577+f4afbb0e.1.x86_64                 python39-pip-20.2.4-7.module_el8.6.0+2780+a40f65e1.noarch                         
  python39-pip-wheel-20.2.4-7.module_el8.6.0+2780+a40f65e1.noarch             python39-setuptools-50.3.2-4.module_el8.6.0+2780+a40f65e1.noarch             python39-setuptools-wheel-50.3.2-4.module_el8.6.0+2780+a40f65e1.noarch            

Complete!

Step #4: Check System Functionality

Check your system's functionality thoroughly after uninstalling Python 3.9. Some utilities and tools may no longer function properly, and your system could become unstable.

Step #5: Restore or Reinstall Dependencies

If essential applications or packages are no longer working due to Python 3.9 removal, you may need to restore or reinstall them. This process can be complex and depends on the specific applications affected.

Host Your Python Web Application with Liquid Web

With the help of this post, we hope you learned how to install Python on Linux (Almalinux). Liquidweb Hosting solutions will assist you in getting there more quickly if you're prepared to advance your web projects. Liquid Web offers several Web Hosting plans that include Python on AlmaLinux.

You can always reach our Python AlmaLinux experts, who will assist you with appropriate web hosting services — from installation to configuration and beyond.

Avatar for Mohammed Noufal

About the Author: Mohammed Noufal

Mohammed Noufal is a B.Tech graduate with a decade of experience in server administration and web hosting. He is a father to two daughters and finds fulfillment in their growth. In his free time, he enjoys blogging, sharing experiences, and listening to music. With a strong technical background, family commitment, and creative outlets, he represents a well-rounded life journey.

Latest Articles

In-place CentOS 7 upgrades

Read Article

How to use kill commands in Linux

Read Article

Change cPanel password from WebHost Manager (WHM)

Read Article

Change cPanel password from WebHost Manager (WHM)

Read Article

Change the root password in WebHost Manager (WHM)

Read Article