How to Install Multiple Python Versions on Ubuntu Using Pyenv

Python is a popular programming language known for its simplicity and versatility. However, installing and managing multiple Python versions on your system can be a bit challenging, especially if you are using Ubuntu. Fortunately, there is a tool called Pyenv that makes it easy to install and switch between different Python versions on Ubuntu.

Pyenv is a simple yet powerful tool that allows you to easily install and manage multiple versions of Python on your system. It provides a convenient way to switch between Python versions and create isolated Python environments for different projects.

In this article, we will guide you through the steps to install and use Pyenv on Ubuntu to manage multiple Python versions.

Step 1: Install Pyenv
First, you need to install Pyenv on your Ubuntu system. You can do this by running the following commands in your terminal:

$ sudo apt update
$ sudo apt install git
$ git clone https://github.com/pyenv/pyenv.git ~/.pyenv
$ echo ‘export PYENV_ROOT=”$HOME/.pyenv”‘ >> ~/.bashrc
$ echo ‘export PATH=”$PYENV_ROOT/bin:$PATH”‘ >> ~/.bashrc
$ echo ‘eval “$(pyenv init -)”‘ >> ~/.bashrc
$ exec $SHELL

This will download and install Pyenv on your system. Once the installation is complete, you can verify that Pyenv is installed by running the following command:

$ pyenv –version

Step 2: Install Python versions
Now that Pyenv is installed, you can use it to install multiple Python versions on your system. To install a specific Python version, you can use the following command:

$ pyenv install

For example, to install Python 3.8.2, you would run:

$ pyenv install 3.8.2

You can check which Python versions are available for installation by running:

$ pyenv install –list

Step 3: Set a global Python version
Once you have installed multiple Python versions using Pyenv, you can set a global Python version that will be used by default. To set a global Python version, you can run the following command:

$ pyenv global

For example, to set Python 3.8.2 as the global version, you would run:

$ pyenv global 3.8.2

You can also set a local Python version for a specific directory by running:

$ pyenv local

Step 4: Verify Python version
To verify the Python version you are currently using, you can run:

$ python –version

This will show you the Python version that is currently active on your system.

With Pyenv installed on your Ubuntu system, you can easily manage multiple Python versions and switch between them as needed. This can be extremely useful when working on different projects that require different Python versions. By following the steps outlined in this article, you can install and use Pyenv to simplify the process of managing Python versions on Ubuntu.