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
.
Table of contents
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
:
sudo apt update
sudo apt install -y make build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm \
libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev \
python3-openssl git
Then, install pyenv
:
curl https://pyenv.run | bash
Add pyenv
to your shell:
echo 'export PATH="$HOME/.pyenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init --path)"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc
source ~/.bashrc
Step 2: Install Python Versions
With pyenv
installed, you can now install multiple versions of Python:
pyenv install 3.9.1
pyenv install 3.8.6
pyenv install 2.7.18
You can list all available versions with the following command:
pyenv install --list
Step 3: Set Global and Local Versions
Set a global version to use:
pyenv global 3.9.1
Set a local version for a specific directory:
cd my_project
pyenv local 3.8.6
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
:
# Add the repo for downloading python
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install python3.8
sudo apt install python3.9
Step 2: Configure update-alternatives
Add the installed versions to the update-alternatives
system:
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2
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:
sudo update-alternatives --config python3
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.