Install Multiple Versions of Python on CentOS and Ubuntu

Posted on

Install Multiple Versions of Python on CentOS and Ubuntu

Install Multiple Versions of Python on CentOS and Ubuntu

Python has become a cornerstone of modern software development, powering everything from web applications to data science projects. While many Linux distributions come with Python pre-installed, the default version often lags behind the latest releases. This can create compatibility issues, as some applications require specific Python versions to function correctly. For example, legacy projects might rely on Python 2.7, while newer applications demand the features and improvements of Python 3.x.

The solution lies in installing multiple Python versions side-by-side on your Linux system. This approach allows you to seamlessly switch between different versions, ensuring compatibility without disrupting your system’s core functionalities.

This guide provides a detailed walkthrough of installing multiple Python versions on CentOS and Ubuntu Linux, covering both the latest 3.x and older 2.x releases. These instructions for Install Multiple Versions of Python on CentOS and Ubuntu will enable you to manage your Python environment effectively.

Prerequisites

Before proceeding, ensure your system is up-to-date:

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

Python versions 2.x and 3.x are designed to coexist on the same system without conflicts. The key is to install them into separate directories.

The examples below focus on Python 3.8 and 2.7, but you can easily adapt the instructions to install any versions you need.

Installing Python on CentOS

CentOS and RHEL typically include Python 2 by default. We’ll leverage the Software Collections (SCL) repository to install additional Python versions.

Step 1 – Install Development Tools

Essential tools for building Python from source:

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

Step 2 – Install Python 3 using SCL

Enable the SCL repository:

$ sudo yum install centos-release-scl

Install Python 3.8:

$ sudo yum install rh-python38

This installs Python 3.8 in /opt/rh/rh-python38.

Activate Python 3.8:

$ scl enable rh-python38 bash

Verify the installation:

$ python --version

Exit the SCL environment to return to the system Python:

$ exit

Step 3 – Install Python 2 from IUS

The IUS repository offers updated Python 2.x versions for CentOS.

Enable IUS:

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

Install Python 2.7:

$ sudo yum install python27

Confirm the installation:

$ python2.7 --version

You now have both Python 3.8 and Python 2.7 installed on your CentOS system. With Install Multiple Versions of Python on CentOS and Ubuntu, it’s key to verify your installation.

Installing Python on Ubuntu

Ubuntu comes with Python 3 pre-installed. The deadsnakes PPA simplifies installing other Python versions.

Step 1 – Install Development Tools

Required build tools and dependencies:

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

Step 2 – Install Python Versions

Add the deadsnakes PPA:

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

Install Python 3.8 and 2.7:

$ sudo apt install python3.8 python2.7

Verify the installations:

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

Python 3.8 and 2.7 are now ready for use on your Ubuntu system.

Using Virtual Environments

Virtual environments are highly recommended to isolate project dependencies from the system Python installation. This prevents conflicts and ensures reproducibility.

Create a Python 3 virtual environment:

$ python3.8 -m venv myprojectenv

Activate the environment:

$ source myprojectenv/bin/activate

Deactivate the environment when finished:

$ deactivate

Create a Python 2 virtual environment:

$ python2.7 -m virtualenv myprojectenv

Virtual environments allow you to manage project-specific Python versions without impacting the entire system.

Setting the Default Python Version

After installing multiple Python versions, the python command might still point to the system Python.

To change the default for all users, adjust the symbolic links:

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

Now, python will execute Python 3.8.

To revert to Python 2:

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

For a single user, modify symbolic links in ~/.local/bin instead:

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

This overrides the system default only for that user.

Alternative Solutions for Managing Multiple Python Versions

While the above methods are effective, other solutions offer different advantages. Here are two alternative approaches for managing multiple Python versions:

1. Using pyenv

pyenv is a powerful tool specifically designed for managing multiple Python versions. It allows you to easily install, switch between, and manage different Python versions on a per-user basis. It avoids modifying system-wide symbolic links, providing a cleaner and more isolated environment.

Explanation:

pyenv works by intercepting Python commands and redirecting them to the appropriate Python version based on the currently active environment. It uses shims (small executable files) that are placed in your PATH to achieve this. When you run a Python command, pyenv determines which Python version should be used based on a hierarchy of configuration files (local, global, and shell-specific).

Installation and Usage:

  1. Install pyenv: The installation process varies depending on your operating system. Refer to the official pyenv documentation for detailed instructions: https://github.com/pyenv/pyenv

    For example, on Ubuntu:

    sudo apt update
    sudo apt install -y make build-essential libssl-dev zlib1g-dev libbz2-dev 
    libreadline-dev libsqlite3-dev wget curl llvm libncursesw5-dev 
    xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev
    curl https://pyenv.run | bash
  2. Configure your shell: Add the following lines to your shell configuration file (e.g., .bashrc or .zshrc):

    export PYENV_ROOT="$HOME/.pyenv"
    export PATH="$PYENV_ROOT/bin:$PATH"
    eval "$(pyenv init --path)"

    Restart your shell or source the configuration file:

    source ~/.bashrc
  3. Install Python versions: Use pyenv install to install the desired Python versions:

    pyenv install 3.9.7
    pyenv install 2.7.18
  4. Set the Python version: You can set the Python version globally, locally (per-directory), or for a specific shell session.

    • Global: pyenv global 3.9.7 (sets the default Python version for all projects)
    • Local: pyenv local 3.9.7 (creates a .python-version file in the current directory, specifying the Python version for that directory and its subdirectories)
    • Shell: pyenv shell 3.9.7 (sets the Python version for the current shell session only)

Code Example:

# Install pyenv (example for Ubuntu - adapt for your system)
sudo apt update
sudo apt install -y make build-essential libssl-dev zlib1g-dev libbz2-dev 
libreadline-dev libsqlite3-dev wget curl llvm libncursesw5-dev 
xz-utils tk-dev libxml2-dev libxmlsec1-dev libxmlsec1-openssl
curl https://pyenv.run | bash

# Add pyenv configuration to .bashrc (or .zshrc)
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init --path)"' >> ~/.bashrc
source ~/.bashrc

# Install Python versions
pyenv install 3.9.7
pyenv install 2.7.18

# Set the global Python version
pyenv global 3.9.7

# Verify the Python version
python --version

2. Using Docker Containers

Docker provides a containerization solution that allows you to create isolated environments for your applications. Each container can have its own Python version and dependencies, eliminating conflicts and ensuring consistency across different environments.

Explanation:

Docker containers are lightweight, portable, and self-contained. They package an application and its dependencies into a single unit, making it easy to deploy and run the application on any system that has Docker installed. By using Docker, you can create separate containers for projects that require different Python versions, ensuring that each project has its own isolated environment.

Installation and Usage:

  1. Install Docker: Follow the instructions on the official Docker website to install Docker on your system: https://docs.docker.com/get-docker/

  2. Create a Dockerfile: A Dockerfile is a text file that contains instructions for building a Docker image. Create a Dockerfile for each project, specifying the desired Python version and dependencies.

    Example Dockerfile (for a Python 3.9 project):

    FROM python:3.9-slim-buster
    
    WORKDIR /app
    
    COPY requirements.txt .
    
    RUN pip install --no-cache-dir -r requirements.txt
    
    COPY . .
    
    CMD ["python", "your_script.py"]

    Example Dockerfile (for a Python 2.7 project):

    FROM python:2.7-slim
    
    WORKDIR /app
    
    COPY requirements.txt .
    
    RUN pip install --no-cache-dir -r requirements.txt
    
    COPY . .
    
    CMD ["python", "your_script.py"]
  3. Build the Docker image: Use the docker build command to build a Docker image from the Dockerfile:

    docker build -t my_python_39_app .  # For the Python 3.9 project
    docker build -t my_python_27_app .  # For the Python 2.7 project
  4. Run the Docker container: Use the docker run command to run a Docker container from the image:

    docker run my_python_39_app  # Run the Python 3.9 application
    docker run my_python_27_app  # Run the Python 2.7 application

Code Example:

Assume you have a file called requirements.txt in the same directory as the Dockerfile above, and a python script your_script.py. Here’s how you would build and run the docker container:

# Create a requirements.txt file (example)
echo "requests" > requirements.txt

# Create a simple python script (example)
echo "import requests; print('Hello from Python 3.9 inside Docker!')" > your_script.py

# Build the Docker image
docker build -t my_python_39_app .

# Run the Docker container
docker run my_python_39_app

These alternative solutions provide more advanced and flexible ways to manage multiple Python versions. pyenv offers a streamlined approach for per-user version management, while Docker provides complete isolation and portability for your applications.

Conclusion

This guide covered installing and managing multiple Python versions on CentOS and Ubuntu, as well as offering alternative solutions. Key takeaways include:

  • The primary method of installing multiple Python versions using package managers and repositories.
  • The importance of virtual environments for isolating project dependencies.
  • Alternative solutions like pyenv and Docker for more advanced version management and isolation.

Now you can confidently use the appropriate Python version for your projects, ensuring compatibility and flexibility on your Linux system. With the knowledge of how to Install Multiple Versions of Python on CentOS and Ubuntu, you’ll be able to manage your development environment much more effectively.