installing Python3 alongside the default python2 on CentOS (RHEL)
Tags: webdev, showdev, linux, devops
Published: 2022-06-18 19:12:22

---
title: installing Python3 alongside the default python2 on CentOS (RHEL) 
published: true
description: will be installing python3 without replacing the default system python2, so it's safer!
tags: #webdev #showdev #linux #devops
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4cui47ajdw9ywg92ddm0.png
 
 
# Use a ratio of 100:42 for best results.
---
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:

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

```


## 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`:

```bash
[root@centos7 ~]# curl -O https://www.python.org/ftp/python/3.9.13/Python-3.9.13.tgz
```

once the download is done, extract the file:

```bash
[root@centos7 ~]# tar -xzf Python-3.9.13.tgz
```

Now change to directory to the new file created:

```bash
[root@centos7 ~]# cd Python-3.9.13
```

let's prepare to compile Python from source:

```bash
[root@centos7 Python-3.9.13]# ./configure --enable-optimizations
```
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.

```bash
[root@centos7 Python-3.9.13]# make altinstall
```
Cheer you did it! now verify the version:

```bash
[root@centos7 Python-3.9.13]# python3.9 --version
Python 3.9.13
```

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:

```bash
alias python3="python3.9"
```
Now refresh the current session:

```bash 
[root@centos7 Python-3.9.13]# source ~/.bashrc
```

after it, you can just use `python3`:

```bash 
[root@centos7 Python-3.9.13]# python3 --version
Python 3.9.13
```

## 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:

```bash
[root@centos7 Python-3.9.13]# vim Modules/Setup
```

Now look for the these four lines and uncomment it:
```bash
#SSL=/usr/local/ssl
#_ssl _ssl.c \
#    -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
#    -L$(SSL)/lib -lssl -lcrypto
```
and re-install it

```bash
[root@centos7 Python-3.9.13]# ./configure --enable-optimizations
[root@centos7 Python-3.9.13]# make altinstall
```


## 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