OSETC TECH

How to Install the latest Python 3 ( 3.6.x, 3.7.x) on CentOS 7 or RHEL 7

This post will guide you how to install the latest version of Python 3 on CentOS 7 or RHEL 7 Linux. How do I install Python 3.7 from the source code by manually under CentOS Linux 7.

(

Prerequisites


What is Python?


Python is an interpreted, high-level, general-purpose programming language. It is one of the most popular programming languages in the world. And It is very easy to learn for beginners. You can use python to develop websites, automation framework, or analyze data and many more. It can be do anything that you want so far. Especially, you can use it to write crawler scripts.

Python is a general purpose object-oriented programming language and it is designed to be used as a software solution for almost all kinds of requests.
Python 2.7.5 is installed on CentOS 7 by default. And the latest version of Python 3.7 is not installed and it is also not available in the official yum repository of CentOS 7 as well. You need to add inline Upstream Stable(IUS) package repository to install Python 3 by yum package manager.

At writing time of this post, the latest stable version of Python3 is 3.7.3. And this new version give use many new features, including: Postponed evaluation of type annotations, async and await are now reserved keywords, New context vars and data classes library modules, New breakpoint function, and more. If you want to get more new features, just check out the What’s New in Python 3.7 page.

This post will show you three methods of installing the latest version 3.7.3 of Python on your CentOS system.

Method1: Installing Python3 from IUS Package Repository


As the latest Python3 package is not available on the default Yum repository, so you need to add Inline Upstream stable package repository. IUS is a community project and it provides Red Hat Package Manager packages for some newer version of select software, so it can provide the necessary RPM packages, such as: Python3.

Step1: Install IUS package to add IUS repository to your CentOS system, type:

$ sudo yum install https://centos7.iuscommunity.org/ius-release.rpm

Outputs:

[devops@localhost ~]$ sudo yum install https://centos7.iuscommunity.org/ius-release.rpm
Loaded plugins: fastestmirror, langpacks
ius-release.rpm | 8.2 kB 00:00:00
Examining /var/tmp/yum-root-3dzYDD/ius-release.rpm: ius-release-2-1.el7.ius.noarch
Marking /var/tmp/yum-root-3dzYDD/ius-release.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package ius-release.noarch 0:2-1.el7.ius will be installed
--> Processing Dependency: epel-release = 7 for package: ius-release-2-1.el7.ius.noarch
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
--> Running transaction check
---> Package epel-release.noarch 0:7-11 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================================================================================
Package Arch Version Repository Size
================================================================================================================================================
Installing:
ius-release noarch 2-1.el7.ius /ius-release 4.5 k
Installing for dependencies:
epel-release noarch 7-11 extras 15 k

Transaction Summary
================================================================================================================================================
Install 1 Package (+1 Dependent package)

Total size: 19 k
Total download size: 15 k
Installed size: 29 k
Is this ok [y/d/N]: y
Downloading packages:
warning: /var/cache/yum/x86_64/7/extras/packages/epel-release-7-11.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY:--:-- ETA
Public key for epel-release-7-11.noarch.rpm is not installed
epel-release-7-11.noarch.rpm | 15 kB 00:00:00
Retrieving key from http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
Userid : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>"
Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
From : http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
Is this ok [y/N]: y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : epel-release-7-11.noarch 1/2
Installing : ius-release-2-1.el7.ius.noarch 2/2
Verifying : ius-release-2-1.el7.ius.noarch 1/2
Verifying : epel-release-7-11.noarch 2/2

Installed:
ius-release.noarch 0:2-1.el7.ius

Dependency Installed:
epel-release.noarch 0:7-11

Complete!

Step2: Once IUS package is installed, and you can install Python3.6.8 with the following command:

$ sudo yum update
$ sudo yum install python36u

Outputs:

[devops@localhost ~]$ sudo yum install python36u
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* epel: mirrors.tuna.tsinghua.edu.cn
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package python36u.x86_64 0:3.6.8-1.el7.ius will be installed
--> Processing Dependency: python36u-libs(x86-64) = 3.6.8-1.el7.ius for package: python36u-3.6.8-1.el7.ius.x86_64
--> Processing Dependency: libpython3.6m.so.1.0()(64bit) for package: python36u-3.6.8-1.el7.ius.x86_64
--> Running transaction check
---> Package python36u-libs.x86_64 0:3.6.8-1.el7.ius will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================================================================================
Package Arch Version Repository Size
================================================================================================================================================
Installing:
python36u x86_64 3.6.8-1.el7.ius ius 58 k
Installing for dependencies:
python36u-libs x86_64 3.6.8-1.el7.ius ius 8.9 M

Transaction Summary
================================================================================================================================================
Install 1 Package (+1 Dependent package)

Total download size: 8.9 M
Installed size: 40 M
Is this ok [y/d/N]:y
Running transaction
Installing : python36u-libs-3.6.8-1.el7.ius.x86_64 1/2
Installing : python36u-3.6.8-1.el7.ius.x86_64 2/2
Verifying : python36u-3.6.8-1.el7.ius.x86_64 1/2
Verifying : python36u-libs-3.6.8-1.el7.ius.x86_64 2/2

Installed:
python36u.x86_64 0:3.6.8-1.el7.ius

Dependency Installed:
python36u-libs.x86_64 0:3.6.8-1.el7.ius

Complete!

Step3: Once the installation process is completed. And you can simply check if the correct version of Python has been installed by the following command:

$ python3.6 -V

Outputs:

[devops@localhost ~]$ python3.6 -V
Python 3.6.8


[devops@localhost ~]$ python -V
Python 2.7.5

Now the Python 3.6.8 should be installed on your CentOS 7 system.

Method2: Installing Python3 from Source Code


If you want to install the latest version 3.7.3 of Python on your CentOS system, you have to compile the Python source code by manually. And you need to download the Python3.7 source package from their official website. Then you need to compile and install it. Just do the following steps:

Step1: You need to install development tools for compiling Python source code(such as: GCC compiler ). Type:

$ sudo yum groupinstall -y “Developent Tools”

Or

$ sudo yum install gcc openssl-devel bzip2-devel libffi libffi-devel

Step2: downloading the latest version of source code of Python from the Python Official website using the following wget command:

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

Outputs:

$ wget https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tgz
--2019-06-18 10:34:56-- https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tgz
Resolving www.python.org (www.python.org)... 151.101.228.223, 2a04:4e42:36::223
Connecting to www.python.org (www.python.org)|151.101.228.223|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 22973527 (22M) [application/octet-stream]
Saving to: ‘Python-3.7.3.tgz’

Python-3.7.3.tgz 100%[===================================================================>] 21.91M 56.6KB/s in 6m 33s

2019-06-18 10:41:30 (57.0 KB/s) - ‘Python-3.7.3.tgz’ saved [22973527/22973527]

Step3: extract the downloaded Python package with the following command:

$ sudo tar xzf Python-3.7.3.tgz

Step4: Changing the current directory to Python-3.7.3, and then compile Python source code using the following command:

$ cd Python-3.7.3
$ sudo ./configure --enable-optimizations
$ sudo make altinstall

Note: you do not use the standard make install command to compile and install Python source code, as it will overwrite the default system python3 binary. make altinstall can be used to prevent replacing the default python binary file.
–enable-optimizations : Enable expensive, stable optimizations (PGO, etc).

Step5: Python 3.7.3 should be installed on your system, you can test if it is installed successfully by checking the latest version installed with the following command:

$ python3.7 –version

Outputs:

Python 3.7.3

Method3: Installing Python3 with SCL


You can also use the SCL utility to install the most recent version of Python3 on your CentOS 7. Just do the following steps:

Step1: you need to update the package manager to refresh the package index to ensure that yum repository is up to date, type the following command:

$ sudo yum update

Step2: you need to install SCL, which stands for Software Collections, and is a open source project that allows you to build, install, and use multiple versions of software on the same system. To enable SCL, you need to install the centos-release-scl package by running the following command:

$ sudo yum install centos-release-scl

Step3: you can access to the SCL repository to install any python3.x version as you need on your CentOS system. Now it can be used to install the python3.6 by the following command:

$ sudo yum install rh-python36

Step4: Once installed, you need to launch Python36 in an new SCL shell, type:

$ scl enable rh-python36 bash

Note: the above command will execute the shell script /opt/rh/rh-python36/enable, it will change the shell environment variables.

Then you can enter the following command to check the current version of Python:

$ python -V

Outputs:

Python 3.6.3

Creating Your First Python Program


You can create a new Python program with vim text editor, and then add the following codes:

$ sudo vim helloword.py
#!/usr/bin/python3.7
print("hello world!");

Save and close the file. Then run your program on Ubuntu terminal:

$ sudo python3.7 helloworld.py

After executing, it produces the following output to the screen:

$ sudo python3.7 helloworld.py
hello world!

Conclusion


You should know that how to install Postman tool on your Ubuntu or Debian Linux. And you should have a Python3 programming environment setup on your CentOS 7 system. If you want to see more detailed information about postman, you can directly go to its official web site.