Python is an open source, powerful, high-level, general-purpose programming language first released in 1991 by Guido van Rossum. Python has since become one of the most popular programming languages in the world. It is often used in the development of web applications, data analysis, scientific computing, artificial intelligence, game development, scripting & automation, and many other applications.
Python is one of the programming languages with a large and active community, which means that there are many resources available for learning to enable new and experienced developers learn and troubleshoot issues. There are plenty of libraries and frameworks as well that can be used to simplify development.
In this article we will show you how to install Python 3 or Python 2.7 on Rocky Linux 8 |AlmaLinux 8. It is worth noting that Python 2.7 is no longer maintained and its support ended on January 1, 2020. On RHEL 8 based systems it is recommended to use Python 3.x instead of 2.x. But if your application must use Python 2.7 you can use this article as reference.
Install Python 3 on Rocky Linux 8 |AlmaLinux 8
Python 3 is recommended for new projects and for migrating existing projects from Python 2.x. The packages of varying versions of Python 3 are available on AppStream repository and can be checked with the commands below.
$ sudo dnf module list | grep -i python
python27 2.7 [d][e] common [d] [i] Python programming language, version 2.7
python36 3.6 [d][e] build, common [d] Python programming language, version 3.6
python38 3.8 [d] build, common [d] Python programming language, version 3.8
python39 3.9 [d] build, common [d] Python programming language, version 3.9
Pass the module version to the dnf command, example to install Python 3.9 run the following commands:
$ sudo dnf install python3.9
...
Transaction Summary
======================================================================================================================================================================================================
Install 6 Packages
Total download size: 13 M
Installed size: 45 M
Is this ok [y/N]: y
Python 3 modules have the python3 prefix in their names. For example, the PyMySQL module can be installed using:
sudo dnf install python3X-<packagename>
#Example
$ sudo dnf install python39-PyMySQL
Once Python 3 is installed on the system, use the commands below to use it in an interactive shell.
$ python3.9
Python 3.9.13 (main, Nov 16 2022, 15:31:39)
[GCC 8.5.0 20210514 (Red Hat 8.5.0-15)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
Install Python 2.7 on Rocky Linux 8 |AlmaLinux 8
You can install Python 2.7 on Rocky Linux 8 |AlmaLinux 8 in parallel with Python 3 as modular package.
sudo dnf module install python27
Verify the installation by checking the version of Python 2.
$ which python2
/usr/bin/python2
$ python2.7 -V
Python 2.7.18
To use Python 2.7 in an interactive session type the command python2
:
$ python2 Python 2.7.18 (default, Nov 8 2022, 17:12:04) [GCC 8.5.0 20210514 (Red Hat 8.5.0-15)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> exit()
Setting default version of Python using alternatives
We can create a symbolic link from /usr/bin/python
to /usr/bin/python3
or /usr/bin/python2.7
. By default the command will return an error command not found.
$ python
bash: python: command not found...
Setting Python 3 as default
Set Python 3 as default version of Python on the system.
# Default Python 3
sudo alternatives --set python /usr/bin/python3
# Default Python 3.9
sudo alternatives --set python /usr/bin/python3.9
sudo alternatives --set python3 /usr/bin/python3.9
Validate your settings with the commands give here:
$ python -V
Python 3.9.6
$ python3 -V
Python 3.9.6
Setting Python 2 as default
Setting Python 2 as default can be done with alternatives
similar to steps use for Python 3.
sudo alternatives --set python /usr/bin/python2
Running python -V
should show default Python version configured
$ python -V
Python 2.7.18
Use Python 2 via python:
$ python
Python 2.7.18 (default, Dec 11 2021, 23:19:43)
[GCC 8.5.0 20210514 (Red Hat 8.5.0-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.version)
2.7.18 (default, Nov 8 2022, 17:12:04)
[GCC 8.5.0 20210514 (Red Hat 8.5.0-15)]
>>> exit()
Unsetting default Python version
To reset this configuration and remove the unversioned python command, run the following command:
sudo alternatives --auto python
This marks the end to our article on how to install Python 3 / Python 2.7 on Rocky Linux 8 |AlmaLinux 8 Linux system.
More articles on Rocky / AlmaLinux 8:
- Install and Configure Redis Server on Rocky Linux 8 / AlmaLinux 8
- Install Flask with Gunicorn on Rocky Linux 8 / AlmaLinux 8
- Install Nessus Scanner on Rocky Linux 8 / AlmaLinux 8