Site icon Techolac – Computer Technology News

RHEL 8 install Python 3 or Python 2 using yum

I need to install Python 3 for Ansible IT automation tool on RHEL 8. How do I install Python 3 on RHEL 8? Is it possible to install Python 2 for legacy apps on RHEL 8?

Red Hat Enterprise Linux (RHEL 8) does not install Python 3 or 2 by default as Red Hat didn’t want to set a default. However, system administrators can install Python 3 or 2 as per needs using various methods. This page shows how to install Python 3 or Python 2.7 on RHEL 8 using yum command.

Adblock detected 😱
My website is made possible by displaying online advertisements to my visitors. I get it! Ads are annoying but they help keep this website running. It is hard to keep the site running and producing new content when so many people block ads. Please consider donating money to the nixCraft via PayPal/Bitcoin, or become a supporter using Patreon.

 

How to Install Python 3 / Python 2.7 on RHEL 8

The procedure for installing Python 3 on RHEL 8 is as follows:

  1. Open the Terminal application or window.
  2. Search for python package in RHEL 8, run: sudo yum search python3
  3. To install python 3 on RHEL 8, run: sudo yum install python3
  4. To install python 2 on RHEL 8, run: sudo yum install python2
  5. Upgrade python 3 in RHEL 8, run: sudo yum upgrade python3
  6. Upgrade python 2 in RHEL 8, run: sudo yum upgrade python2

Let us see all commands and example in details.

How to find out Python package names on RHEL 8

Try any one of the following syntax along with grep command:
sudo yum search python3 | more
sudo yum search python2 | more
sudo yum search python36
sudo yum search python2 | grep 'python2.x86_64'

You can show detailed information before installing package as well:
sudo yum info python2.x86_6
sudo yum info python36

RHEL 8 install Python 3

Type the following yum command to install Python 3 on Red Hat Enterprise Linux version 8:
sudo yum install python3
OR
sudo yum module install python36

Verify installation by typing the type command/command command:
$ type -a python3
python3 is /usr/bin/python3
$
command -V python3
python3 is hashed (/usr/bin/python3)
$
python3 –version
Python 3.6.6

A sample script in Python 3

Create a file named test.py:

#!/usr/bin/python3
import os, time
print("This is a test code for Python3")
print("Hello %s, let us be friends!" % os.environ["USER"])
print("Today is %s" % time.strftime("%c"))

#!/usr/bin/python3
import os, time
print(“This is a test code for Python3”)
print(“Hello %s, let us be friends!” % os.environ[“USER”])
print(“Today is %s” % time.strftime(“%c”))

Run it as follows:
$ chmod +x test.py
$ ./test.py

A note for Ansible users

Set up your hosts file as follows on your control node to use Ansible as automation tool for RHEL 8 server:

[all:vars]
ansible_user=vivek
ansible_port=22
ansible_python_interpreter='/usr/bin/env python3'

[all:vars]
ansible_user=vivek
ansible_port=22
ansible_python_interpreter=’/usr/bin/env python3′

Now run it as usual:
ansible-playbook -i hosts tasks/7-secure-rhel8-vm.yml

RHEL 8 install Python 2

Let us see how to install Python 2.7 on RHEL 8:
sudo yum install python27
OR
sudo yum module install python27

Let us find out information about installed version of Python 2.7 in RHEL 8:
$ type -a python2
$ command -V python2
$ python2 --version

Can I install both Python 3.6 and Python 2.7 on RHEL 8?

Yes. It is possible to install both versions simultaneously:
sudo yum module install python36 python27
Sample outputs:

Updating Subscription Management repositories.
Updating Subscription Management repositories.
Last metadata expiration check: 0:09:22 ago on Tuesday 05 March 2019 02:09:48 PM EST.
Dependencies resolved.
========================================================================================
 Package Arch   Version                     Repository                             Size
========================================================================================
Installing group/module packages:
 python36
         x86_64 3.6.6-17.el8+2102+a4bbd900  rhel-8-for-x86_64-appstream-beta-rpms  22 k
 python2 x86_64 2.7.15-15.el8+2103+c6cdb4cb rhel-8-for-x86_64-appstream-beta-rpms 106 k
 python2-pip
         noarch 9.0.3-10.el8+2056+8b2f0fde  rhel-8-for-x86_64-appstream-beta-rpms 2.0 M
 python2-setuptools
         noarch 39.0.1-10.el8+1958+74bcdd68 rhel-8-for-x86_64-appstream-beta-rpms 643 k
 python2-libs
         x86_64 2.7.15-15.el8+2103+c6cdb4cb rhel-8-for-x86_64-appstream-beta-rpms 6.0 M
Installing module profiles:
 python27/default
 
 python36/default
 
 
Transaction Summary
========================================================================================
Install  5 Packages
 
Total download size: 8.7 M
Installed size: 36 M
Is this ok [y/N]:

Updating Subscription Management repositories.
Updating Subscription Management repositories.
Last metadata expiration check: 0:09:22 ago on Tuesday 05 March 2019 02:09:48 PM EST.
Dependencies resolved.
========================================================================================
Package Arch Version Repository Size
========================================================================================
Installing group/module packages:
python36
x86_64 3.6.6-17.el8+2102+a4bbd900 rhel-8-for-x86_64-appstream-beta-rpms 22 k
python2 x86_64 2.7.15-15.el8+2103+c6cdb4cb rhel-8-for-x86_64-appstream-beta-rpms 106 k
python2-pip
noarch 9.0.3-10.el8+2056+8b2f0fde rhel-8-for-x86_64-appstream-beta-rpms 2.0 M
python2-setuptools
noarch 39.0.1-10.el8+1958+74bcdd68 rhel-8-for-x86_64-appstream-beta-rpms 643 k
python2-libs
x86_64 2.7.15-15.el8+2103+c6cdb4cb rhel-8-for-x86_64-appstream-beta-rpms 6.0 M
Installing module profiles:
python27/default
python36/default Transaction Summary
========================================================================================
Install 5 Packages Total download size: 8.7 M
Installed size: 36 M
Is this ok [y/N]:

How do I set default version of Python on RHEL 8?

One can set Python 3 as default by running the simple command:
sudo alternatives --set python /usr/bin/python3
python --version

Sample outputs:

Python 3.6.6

Similarly one can set Python 2 as default version for RHEL 8 based desktop or server environment from the bash shell:
sudo alternatives --set python /usr/bin/python2
python --version

How do I remove python default on RHEL 8?

The syntax is simple:
sudo alternatives --remove python /usr/bin/python2
OR
sudo alternatives --remove python /usr/bin/python3

Conclusion

This page showed how to install Python 3.6 or Python 2.7 on RHEL 8 using the yum command. For more information please see this page here and here.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

[ad_2]

Exit mobile version