Install multiple versions of Python

Installing Multiple Versions of Python on Ubuntu

Estimated reading time: 2 minutes

Managing multiple versions of Python on your Ubuntu machine can be incredibly beneficial. Whether you’re a developer testing applications across different Python versions or transitioning from an older version to the latest, this guide will help you set up and manage multiple versions of Python using two methods: pyenv and update-alternatives.

Method 1: Using pyenv to manage multiple Python versions

I found pyenv to be one of the easiest ways to manage various versions of Python. Here’s a step-by-step guide:

Step 1: Install pyenv

First, we need to install the dependencies required for pyenv:

Then, install pyenv:

Add pyenv to your shell:

Step 2: Install Python Versions

With pyenv installed, you can now install multiple versions of Python:

You can list all available versions with the following command:

Step 3: Set Global and Local Versions

Set a global version to use:

Set a local version for a specific directory:

Method 2: Using update-alternatives to manage multiple Python versions

The update-alternatives method provides a way to set different versions of Python and switch between them.

Step 1: Install Python Versions

First, install the desired Python versions using apt:

Step 2: Configure update-alternatives

Add the installed versions to the update-alternatives system:

Here, I have set the highest priority to python3.9, so the default executable will be python3.9.

Step 3: Switch Between Versions

To switch between different versions, use:

You’ll see a list of installed versions and can select the one you want to use.

Conclusion

By following these methods, you can efficiently manage multiple versions of Python on your Ubuntu machine. The pyenv method provides a flexible, user-friendly approach, while the update-alternatives method integrates more closely with the system’s package management.

By managing multiple versions of Python, you can quickly test and develop applications across different environments, ensuring compatibility and smooth transitions between versions.

Suggested reading