How To Install Python3 or Python2 on CentOS 8/RHEL 8

This post will guide you how to install Python3 on your Latest CentOS 8 or RHEL 8 Linux system. How do I install both Python3 and python2.7 on my CentOS Linux 8 or RHEL Linux 8.

By default, CentOS 8 or RHEL 8 will not install Python3 or python2, so you need to install it using dnf or yum commmand by manually.

Install Python3 or Python2 on CentOS 8 1

Installing Python3 on CentOS 8


Python3 is already available in the default CentOS 8 repository, so you just need to use yum or dnf command to install it. and you can search for python3 package to get more deailted info using dnf search or dnf info command. type:

$ sudo dnf search python36
$ sudo dnf info python36

Outputs:

[devops@mydevops ~]$ dnf search python36
Last metadata expiration check: 0:00:55 ago on Fri 04 Oct 2019 04:26:39 AM EDT.
============================================================================== Name Exactly Matched: python36 ===============================================================================
python36.x86_64 : Interpreter of the Python programming language
python36.x86_64 : Interpreter of the Python programming language
================================================================================== Name Matched: python36 ===================================================================================
python36-debug.x86_64 : Debug version of the Python runtime
python36-devel.x86_64 : Libraries and header files needed for Python development
python36-rpm-macros.noarch : RPM macros for building RPMs with Python 3.6
[devops@mydevops ~]$ dnf info python36
Last metadata expiration check: 0:00:12 ago on Fri 04 Oct 2019 04:26:39 AM EDT.
Installed Packages
Name : python36
Version : 3.6.8
Release : 2.module_el8.0.0+33+0a10c0e1
Arch : x86_64
Size : 13 k
Source : python36-3.6.8-2.module_el8.0.0+33+0a10c0e1.src.rpm
Repo : @System
From repo : AppStream
Summary : Interpreter of the Python programming language
URL : https://www.python.org/
License : Python
Description : Python is an accessible, high-level, dynamically typed, interpreted programming
: language, designed with an emphasis on code readibility.
: It includes an extensive standard library, and has a vast ecosystem of
: third-party libraries.
:
: The python36 package provides the "python3.6" executable: the reference
: interpreter for the Python language, version 3.
: The package also installs the "python3" executable which is user configurable
: using the "alternatives --config python3" command.
: For the unversioned "python" command, see manual page "unversioned-python".
:
: The python36-devel package contains files for dovelopment of Python application
: and the python36-debug is helpful for debugging.
:
: Packages containing additional libraries for Python 3.6 are generally named
: with the "python3-" prefix.

You can now install python 3 by running the following command:

$ sudo dnf install python3

Outputs:

[devops@mydevops ~]$ sudo dnf install python3
Last metadata expiration check: 0:06:41 ago on Fri 04 Oct 2019 04:23:51 AM EDT.
Dependencies resolved.
=============================================================================================================================================================================================
Package Arch Version Repository Size
=============================================================================================================================================================================================
Installing:
python36 x86_64 3.6.8-2.module_el8.0.0+33+0a10c0e1 AppStream 19 k
Installing dependencies:
python3-pip noarch 9.0.3-13.el8 AppStream 18 k
python3-setuptools noarch 39.2.0-4.el8 BaseOS 162 k

Transaction Summary
=============================================================================================================================================================================================
Install 3 Packages

Total download size: 200 k
Installed size: 466 k
Is this ok [y/N]: y
Downloading Packages:
(1/3): python3-pip-9.0.3-13.el8.noarch.rpm 106 kB/s | 18 kB 00:00
(2/3): python36-3.6.8-2.module_el8.0.0+33+0a10c0e1.x86_64.rpm 110 kB/s | 19 kB 00:00
(3/3): python3-setuptools-39.2.0-4.el8.noarch.rpm 412 kB/s | 162 kB 00:00
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 43 kB/s | 200 kB 00:04
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : python3-setuptools-39.2.0-4.el8.noarch 1/3
Installing : python36-3.6.8-2.module_el8.0.0+33+0a10c0e1.x86_64 2/3
Running scriptlet: python36-3.6.8-2.module_el8.0.0+33+0a10c0e1.x86_64 2/3
Installing : python3-pip-9.0.3-13.el8.noarch 3/3
Running scriptlet: python3-pip-9.0.3-13.el8.noarch 3/3
Verifying : python3-pip-9.0.3-13.el8.noarch 1/3
Verifying : python36-3.6.8-2.module_el8.0.0+33+0a10c0e1.x86_64 2/3
Verifying : python3-setuptools-39.2.0-4.el8.noarch 3/3

Installed:
python36-3.6.8-2.module_el8.0.0+33+0a10c0e1.x86_64 python3-pip-9.0.3-13.el8.noarch python3-setuptools-39.2.0-4.el8.noarch

Complete!

Once Python3 is installed on your CentOS 8 Linux, and you can verify the installation by running the following command:

$ python3 --version

Outputs:

[devops@mydevops ~]$ python3 --version
Python 3.6.8

From the above outputs, you can see the python3.6 is the default version in your CentOS or RHEL 8 Linux.

Installing Python2 On CentOS 8


If you want to install python 2 in your CentOS 8 system, and you can run the following command, type:

$ sudo dnf install python2

Outputs:

[devops@mydevops ~]$ sudo dnf install python2
Last metadata expiration check: 0:09:46 ago on Fri 04 Oct 2019 04:23:51 AM EDT.
Dependencies resolved.
=============================================================================================================================================================================================
Package Arch Version Repository Size
=============================================================================================================================================================================================
Installing:
python2 x86_64 2.7.15-22.module_el8.0.0+32+017b2cba AppStream 107 k
Installing dependencies:
python2-libs x86_64 2.7.15-22.module_el8.0.0+32+017b2cba AppStream 6.0 M
python2-pip noarch 9.0.3-13.module_el8.0.0+32+017b2cba AppStream 2.0 M
python2-setuptools noarch 39.0.1-11.module_el8.0.0+32+017b2cba AppStream 643 k
Enabling module streams:
python27 2.7

Transaction Summary
=============================================================================================================================================================================================
Install 4 Packages

Total download size: 8.7 M
Installed size: 36 M
Is this ok [y/N]:
Downloading Packages:
(1/4): python2-2.7.15-22.module_el8.0.0+32+017b2cba.x86_64.rpm 544 kB/s | 107 kB 00:00
(2/4): python2-pip-9.0.3-13.module_el8.0.0+32+017b2cba.noarch.rpm 1.7 MB/s | 2.0 MB 00:01
(3/4): python2-libs-2.7.15-22.module_el8.0.0+32+017b2cba.x86_64.rpm 2.6 MB/s | 6.0 MB 00:02
(4/4): python2-setuptools-39.0.1-11.module_el8.0.0+32+017b2cba.noarch.rpm 283 kB/s | 643 kB 00:02
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 2.2 MB/s | 8.7 MB 00:03
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : python2-libs-2.7.15-22.module_el8.0.0+32+017b2cba.x86_64 1/4
Installing : python2-pip-9.0.3-13.module_el8.0.0+32+017b2cba.noarch 2/4
Installing : python2-setuptools-39.0.1-11.module_el8.0.0+32+017b2cba.noarch 3/4
Installing : python2-2.7.15-22.module_el8.0.0+32+017b2cba.x86_64 4/4
Running scriptlet: python2-2.7.15-22.module_el8.0.0+32+017b2cba.x86_64 4/4
Verifying : python2-2.7.15-22.module_el8.0.0+32+017b2cba.x86_64 1/4
Verifying : python2-libs-2.7.15-22.module_el8.0.0+32+017b2cba.x86_64 2/4
Verifying : python2-pip-9.0.3-13.module_el8.0.0+32+017b2cba.noarch 3/4
Verifying : python2-setuptools-39.0.1-11.module_el8.0.0+32+017b2cba.noarch 4/4

Installed:
python2-2.7.15-22.module_el8.0.0+32+017b2cba.x86_64 python2-libs-2.7.15-22.module_el8.0.0+32+017b2cba.x86_64 python2-pip-9.0.3-13.module_el8.0.0+32+017b2cba.noarch
python2-setuptools-39.0.1-11.module_el8.0.0+32+017b2cba.noarch

Complete!

Then you can verify the installation by running the following command:

$ python2 --version

Outputs:

[devops@mydevops ~]$ python2 --version
Python 2.7.15

Installing Both Python3 and Python2 on CentOS 8


If you want to install both python3 and python2 on your CentOS 8 Linux system, and you can type the following command to install them at the same time, type:

$ sudo dnf module install python36 python27

Outputs:

[devops@mydevops ~]$ sudo dnf module install python36 python27
Last metadata expiration check: 0:13:59 ago on Fri 04 Oct 2019 04:23:51 AM EDT.
Dependencies resolved.
=============================================================================================================================================================================================
Package Arch Version Repository Size
=============================================================================================================================================================================================
Installing group/module packages:
python2 x86_64 2.7.15-22.module_el8.0.0+32+017b2cba AppStream 107 k
python2-libs x86_64 2.7.15-22.module_el8.0.0+32+017b2cba AppStream 6.0 M
python2-pip noarch 9.0.3-13.module_el8.0.0+32+017b2cba AppStream 2.0 M
python2-setuptools noarch 39.0.1-11.module_el8.0.0+32+017b2cba AppStream 643 k
python36 x86_64 3.6.8-2.module_el8.0.0+33+0a10c0e1 AppStream 19 k
Installing dependencies:
python3-pip noarch 9.0.3-13.el8 AppStream 18 k
python3-setuptools noarch 39.2.0-4.el8 BaseOS 162 k
Installing module profiles:
python27/common
python36/common

Transaction Summary
=============================================================================================================================================================================================
Install 7 Packages

Total download size: 8.9 M
Installed size: 37 M
Is this ok [y/N]:

Setting Default Python Version on CentOS 8


Once python3 or python2 is installed on your CentOS 8 system, and you have to run it by the following commands:

$ python3

or

$ python2

Outputs:

[devops@mydevops ~]$ python3
Python 3.6.8 (default, May 21 2019, 23:51:36)
[GCC 8.2.1 20180905 (Red Hat 8.2.1-3)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
[devops@mydevops ~]$ python2
Python 2.7.15 (default, Jun 22 2019, 03:44:59)
[GCC 8.2.1 20180905 (Red Hat 8.2.1-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

If you want to run a certain version of python while executing python or /usr/bin/python, you can use alternatives command to make python or /usr/bin/python to point to the correct location of python version to set as default python version.

For example, you want to set python3 as the default python version, just type:

$ sudo alternatives --set python /usr/bin/python3

then you can type the following command to check if the default version is python3.6, type:

$ python --version

outputs:

[devops@mydevops ~]$ python --version
Python 3.6.8

If you want to set the default python version as python2, just type the following command:

$ sudo alternatives --set python /usr/bin/python2

Removing Default Python Version


If you want to remove the default python version, and you can use the following command:

$ sudo alternatives --remove python /usr/bin/python2

Or

$ sudo alternatives --remove python /usr/bin/python3

Conclusion


You should know that how to install python3 and python2 pacakges and setting default python version in your CentOS or RHEL 8 Linux system.

 

You might also like:

Sidebar



back to top