Install multiple versions of Python on CentOS and Ubuntu

setup python 3 and 2 on centos 7 8 almalinux ubuntu 18.04 20.04 22.04 debian redhat linux

Python is one of the most popular programming languages used today. Many Linux systems come with Python pre-installed, but often it’s an older version like Python 2.7. As Python has advanced over the years, new versions like Python 3.8, 3.7, etc have been released with significant new features and improvements.

However, this can cause issues if you need a specific version of Python for your applications or scripts. For example, some applications may require Python 2.7 while others need Python 3.8.

The solution is to install multiple Python versions side-by-side on your Linux system. This allows you to switch between different versions for compatibility with various software.

In this comprehensive guide, we will cover the steps to install multiple Python versions on CentOS and Ubuntu Linux. The instructions work for any Python version including the latest 3.x and 2.x releases.

Prerequisites

Before we get started, make sure your system is up to date with the latest packages:

# CentOS/RHEL
$ sudo yum update
# Ubuntu/Debian 
$ sudo apt update
$ sudo apt upgrade

Python versions 2.x and 3.x can co-exist peacefully on the same system. We just need to make sure they are installed into separate directories.

The examples below will use Python 3.8 and 2.7, but you can substitute any versions you want.

Installing Python on CentOS

Recent versions of CentOS and RHEL come with Python 2 installed by default. We will use the Software Collections (SCL) repository to install other Python versions as needed.

Step 1 – Install Development Tools

We need git and development tools to build Python from source:

$ sudo yum groupinstall "Development Tools"
$ sudo yum install git

Step 2 – Install Python 3 using SCL

First enable the SCL repository:

$ sudo yum install centos-release-scl

Now install Python 3.8:

$ sudo yum install rh-python38

This will install Python 3.8 into the /opt/rh/rh-python38 directory.

To use this Python version, we need to enable it with:

$ scl enable rh-python38 bash

Verify you now have Python 3.8:

$ python --version

Exit back to system Python 2.7 with:

$ exit

Step 3 – Install Python 2 from IUS

The IUS repository provides updated versions of Python 2.x for CentOS.

Enable IUS with:

$ sudo yum install https://repo.ius.io/ius-release-el7.rpm

Now install Python 2.7:

$ sudo yum install python27

Check your Python 2.7 version:

$ python2.7 --version

We now have Python 3.8 and Python 2.7 installed on CentOS!

Installing Python on Ubuntu

Ubuntu comes with Python 3 installed by default. We will use the deadsnakes PPA to install other Python versions.

Step 1 – Install Development Tools

We need build tools and other dependencies to compile Python:

$ sudo apt update
$ sudo apt install build-essential libssl-dev libffi-dev python3-dev

Step 2 – Install Python Versions

Add the deadsnakes PPA which provides multiple Python versions:

$ sudo add-apt-repository ppa:deadsnakes/ppa

Now install Python 3.8 and 2.7:

$ sudo apt install python3.8 python2.7

Verify you have multiple Python versions:

$ python3.8 --version
$ python2.7 --version 

We now have Python 3.8 and 2.7 ready to use!

Using Virtual Environments

It’s recommended to create virtual environments for your projects rather than use system Python. This keeps dependencies separated between applications.

To create a Python 3 virtual environment:

$ python3.8 -m venv myprojectenv

Activate it with:

$ source myprojectenv/bin/activate

When finished, deactivate the environment:

$ deactivate

Virtual environments can also be created for Python 2:

$ python2.7 -m virtualenv myprojectenv

This allows you to easily switch between Python versions per project, without affecting the whole system.

Setting the Default Python Version

After installing multiple Python versions, python may still reference the system Python.

To change the default for all users, adjust the symlinks:

$ sudo rm /usr/bin/python
$ sudo ln -s /usr/bin/python3.8 /usr/bin/python

Now python will run Python 3.8.

To switch back to Python 2:

$ sudo rm /usr/bin/python
$ sudo ln -s /usr/bin/python2.7 /usr/bin/python

For a single user, symlinks can be adjusted in ~/.local/bin instead:

$ rm ~/.local/bin/python
$ ln -s /usr/bin/python3.8 ~/.local/bin/python

This will override the system default just for that user.

Conclusion

In this guide, we covered installing and managing multiple Python versions on CentOS and Ubuntu. Here’s a quick recap of the major points:

  • Use SCL and IUS repositories on CentOS to install additional Python releases
  • Add the deadsnakes PPA on Ubuntu for multiple Python versions
  • Create virtual environments to isolate dependencies between Python projects
  • Adjust symlinks to change the default python version globally or per user

Now you have the power to use the right Python version for your different applications and scripts on Linux! The ability to install multiple versions side-by-side provides compatibility and flexibility.

LEAVE A COMMENT