DEV Community

Cover image for installing Python3 alongside the default python2 on CentOS (RHEL)
HasOne
HasOne

Posted on

installing Python3 alongside the default python2 on CentOS (RHEL)

if you're thinking of installing Python3 without replacing the default System python, it's for you!

Prerequisites

installing python from the source, We're gonna need to ensure that some prerequisites packages are installed on our system:

[root@centos7 ~]# yum install gcc openssl-devel bzip2-devel libffi-devel -y

Enter fullscreen mode Exit fullscreen mode

Installation

We'll be using Source installation as we want the latest version of python above 3.6 as the yum give us python3.6, look for the version you intended to install https://www.python.org/ftp/python/, in my case I'm gonna install 3.9 so here is the URL https://www.python.org/ftp/python/3.9.13/Python-3.9.13.tgz. make sure to select the tgz extension from the list.

Download Python

let's download it by curl:

[root@centos7 ~]# curl -O https://www.python.org/ftp/python/3.9.13/Python-3.9.13.tgz
Enter fullscreen mode Exit fullscreen mode

once the download is done, extract the file:

[root@centos7 ~]# tar -xzf Python-3.9.13.tgz
Enter fullscreen mode Exit fullscreen mode

Now change to directory to the new file created:

[root@centos7 ~]# cd Python-3.9.13
Enter fullscreen mode Exit fullscreen mode

let's prepare to compile Python from source:

[root@centos7 Python-3.9.13]# ./configure --enable-optimizations
Enter fullscreen mode Exit fullscreen mode

Now let's finish off the installation without replacing the default System Python on our system, if your system is using any version of python it won't get replaced.

[root@centos7 Python-3.9.13]# make altinstall
Enter fullscreen mode Exit fullscreen mode

Cheer you did it! now verify the version:

[root@centos7 Python-3.9.13]# python3.9 --version
Python 3.9.13
Enter fullscreen mode Exit fullscreen mode

let's create alias for python3.9, open up the ~/.bashrc or ~/.zshrc and put the below line the end of file and save it:

alias python3="python3.9"
Enter fullscreen mode Exit fullscreen mode

Now refresh the current session:

[root@centos7 Python-3.9.13]# source ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

after it, you can just use python3:

[root@centos7 Python-3.9.13]# python3 --version
Python 3.9.13
Enter fullscreen mode Exit fullscreen mode

Pip TLS/SSL Error

If you got error by using pip "WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available."

then you have to do a little modification to Modules/Setup file and re-compile python & re-install, so let's open the file in vim:

[root@centos7 Python-3.9.13]# vim Modules/Setup
Enter fullscreen mode Exit fullscreen mode

Now look for the these four lines and uncomment it:

#SSL=/usr/local/ssl
#_ssl _ssl.c \
#    -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
#    -L$(SSL)/lib -lssl -lcrypto
Enter fullscreen mode Exit fullscreen mode

and re-install it

[root@centos7 Python-3.9.13]# ./configure --enable-optimizations
[root@centos7 Python-3.9.13]# make altinstall
Enter fullscreen mode Exit fullscreen mode

Final thought

I had to install python3.9 alongside python2.7 as there was an application which was using python2.7 so I hadn't to replace the default System python version with the lastest. I tried to install it by yum but the yum was giving me the python3.6 version so I had to install it from the source.

Hope you find it helpful and thank you so much for reading!

Stay blessed
Stay safe

Top comments (0)