How to Install Python 2/3 on Rocky Linux/Centos 8

Python is among the most popular programming languages. Because of this, most scripts and tools used in linux are written on python.
Its often described as easy to learn and therefore often recommendd for beginners.

Its not installed by default on centos 8. This guide will show you how to go about installing it.

Ensure you have the latest packages before proceeding:

sudo dnf -y update

Python 3

To install python3, run this command as a sudo user:

sudo dnf install python3

Check python version:

python3 --version

To run Python, you need to explicitly type python3. If you want to use python instead, do the following.

Check where the python3 executable is located:

which python3

Output:

$ which python3
/usr/bin/python3

To have python3 by default when you type python, you need create a symlink /usr/local/bin/python pointing to this /usr/bin/python3 with this command:

sudo ln -s /usr/bin/python3 /usr/local/bin/python

Installing Python 2

Use this command to install python2:

sudo dnf install -y python2

Check that python2 is installed:

python2 --version

Output:

$ python2 --version
Python 2.7.18

Setting the Default Python Version

If you have both versions installed, and you have an app that expects a specific version, use this command to set the correct one in the system path:

To set Python3 as the default:

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

To set Python2 as the default:

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

The alternatives command creates a symlink python that points to the specified python version.

Type python –version in your terminal, and you should see the default Python version.

If you want to remove the unversioned python command, type:

sudo alternatives --auto python
comments powered by Disqus
Citizix Ltd
Built with Hugo
Theme Stack designed by Jimmy