Python is the most growing programming language in many fields such as software development, machine learning, AI, and Deep Learning in this world. Python is suitable for all users, beginners as well as experienced developers due to its simplicity and easy to learn syntax. It is not installed on CentOS 8 environment by default. As we know that python has two different versions python 2 and python 3. Python3 is using the latest python programming language. But, most of the applications still support the python2 version.
This article will show you how you can install python2 and python3 on your CentOS 8 Linux environment. We have implemented some set of commands in the Terminal for this purpose which is mentioned below:
The user should be logged in as a root user or should have to run the ‘sudo’ command privileges.
Python has two versions python2 and python3 which we discussed above. If you want to install both on your system then, you need to install them explicitly. Open the terminal from your desktop. To acccess the terminal, select the ‘Activities’ section and then from the left sidebar click on the terminal icon in CentOS 8 distribution which is also displayed in the following image:
Now, type the ‘su’ command on the terminal and enter the administrative password to log in as a root user:
Installing Python3 on CentOS 8
You can install python3 from the CentOS 8 yum or dnf repositories, type the following command on the terminal with root privileges to install the python3:
# dnf install python3
Type ‘y’ and then hit the ‘Enter’ key in order to complete the installation python3.
To verify the installation, run the below-mentioned command to check the installed version:
# python3 –version
As you can see in the above-displayed image, the latest available python3 version 3.6.8 has been installed on your CentOS 8 system.
Install python3 packages
If you want to install python packages then, use python3 as a prefix with the name of packages as follows:
# dnf install python3-pip
The above command will install the remaining pip packages on your system.
Run python3 and print ‘hello’
To run the python3, type the ‘python3’ on the terminal which is also shown in the following screenshot, and print ‘hello’ on the screen.
>>> print (‘hello’)
To exit from the current displaying python3 environment, type the following command on the terminal:
>>> quit ()
Install Python2 on CentOS 8
You can also install ‘python2’ packages from the default CentOS 8 repository by using the following command:
# dnt install python2
To check the installed python2 version, use the following command:
# python2 --version
You can see in the above-displayed screenshot that the python 2.7.17 version is installed on your CentOS 8 system.
Similarly, type ‘python2’ on the terminal which also mentioned in the following screenshot:
Use unversioned python commands (Set python default version)
Using the following unversioned python commands, you can set python2 and python3 as the default version.
For python3, use the following command:
# alternatives --set python /usr/bin/python3
For python2, type the following command:
# alternatives --set python /usr/bin/python2
Now, enter the following command on the terminal to check the default set python version on your system:
# python --version
To set or change the python default version, you can use any above-mentioned commands. If you don’t want to use the explicitly above command then, use the command as follows:
# alternatives --auto python
By default, python2 and python3 are not installed on CentOS 8. To install both, you need to install all python packages separately according to python versions. We have also shown, how you can run python2 and python3 environments on your system. The ‘alternatives command is used to set any python version as the default. To explore more about python installation, you can get help from different internet resources. I hope you really enjoyed this tutorial.