×


Install and Run Python on CentOS 8 - How to do it ?

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.

Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to install Python packages on CentOS Systems.

In this context, we shall look into how to install python2 and python3 on your CentOS 8 Linux environment.


How to install Python3 on CentOS 8 ?

To begin, ensure that you log into the system as a root user or should have to run the 'sudo' command privileges.

Python has two versions python2 and python3.

If you want to install both on your system then, you need to install them explicitly. 

i. Open the terminal from your desktop. 

ii. To access the terminal, select the 'Activities' section and then from the left sidebar click on the terminal icon in CentOS 8 distribution.

iii. Now, type the 'su' command on the terminal and enter the administrative password to log in as a root user.

iv. 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.

v. To verify the installation, run the below-mentioned command to check the installed version:

# python3 –version


How to 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.


How to run python3 and print 'hello'?

To run the python3, type the ‘python3’ on the terminal and print 'hello' on the screen:

>>> print (‘hello’)

To exit from the current displaying python3 environment, type the following command on the terminal:

>>> quit ()


How to install Python2 on CentOS 8 ?

You can also install ‘python2’ packages from the default CentOS 8 repository by using the following command:

# dnf install python2

To check the installed python2 version, use the following command:

# python2 --version


How to use unversioned python commands (Set python default version) ?

You can use the following unversioned python commands, you can set python2 and python3 as the default version.

1. For python3, use the following command:

# alternatives --set python /usr/bin/python3

2. 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


[Need urgent assistance to install missing packages on CentOS system? We are available. ]


Conclusion

This article covers how to install python2 and python3 on CentOS 8. 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. Also, you can run python2 and python3 environments on your system. 

The 'alternatives --auto python' command is used to set any python version as the default. 


To run Python in Linux:

A widely used way to run Python code is through an interactive session. 

To start a Python interactive session, just open a command-line or terminal and then type in python, or python3 depending on your Python installation, and then hit Enter .


Python comes preinstalled on most Linux distributions, and is available as a package on all others. 

However there are certain features you might want to use that are not available on your distro's package. 

You can easily compile the latest version of Python from source.


Installing Python 3 on Linux:

1. To see which version of Python 3 you have installed, open a command prompt and run

$ python3 --version

2. If you are using Ubuntu 16.10 or newer, then you can easily install Python 3.6 with the following commands:

$ sudo apt-get update

$ sudo apt-get install python3.6

3. If you're using another version of Ubuntu (e.g. the latest LTS release) or you want to use a more current Python, we recommend using the deadsnakes PPA to install Python 3.8:

$ sudo apt-get install software-properties-common

$ sudo add-apt-repository ppa:deadsnakes/ppa

$ sudo apt-get update

$ sudo apt-get install python3.8

4. If you are using other Linux distribution, chances are you already have Python 3 pre-installed as well. If not, use your distribution's package manager. For example on Fedora, you would use dnf:

$ sudo dnf install python3


To see if pip is installed, open a command prompt and run:

$ command -v pip