Managing Multiple Versions of Python on Ubuntu 20.04
Easily install and manage multiple versions of Python on Ubuntu 20.04 or newer.
There are a number of subtle differences in managing versions of Python on older distributions of Ubuntu. If you're running Ubuntu 18.04 or older, go here.
One of my earliest frustrations with Python development had nothing to do with Python itself but rather the needlessly esoteric act of deploying a Python app. Code boot camps and tutorials do a fine job of teaching students how to run Python code locally, but the most meaningful applications don't run on local machines: they run on servers, on the internet, because that's the point, isn't it? Maybe I'm taking crazy pills here.
Ubuntu 20.04 is the first LTS version of Ubuntu to drop Python2, coming fresh out of the box with Python 3.8.5. But what if you've written apps intended for a newer version of Python? If you're like me, you might have tried to replace your system's default installation and destroyed your machine in the process. If nobody has warned you, I'll do the honors: don't do that.
The risk of unintended destruction is only one of many unintuitive details that complicate the seemingly simple task of using an updated version of Python:
apt upgradedoes not apply to versions of Python. In fact...
- Newer Python distributions aren't even visible to
apt; thus, we can't even use
apt updateto find a more recent version of Python without the help of a third party.
- Versions of Python installed on the same machine do not share the same core features (such as pip), which leads to a lot of confusion.
What do we do?
Using Ubuntu's built-in
alternative install is optimal for several reasons:
- We can install a new version of Python in parallel to the version of Python Ubuntu depends on, so we don't ruin our machine.
- It's best to avoid messing around with your Python PATH whenever possible.
- We can easily switch the active version of Python on our machine via a convenient CLI.
We're going to walk through how to install the latest version of Python alongside Ubuntu's system Python versions safely and (relatively) easily.
Python via Deadsnakes
Deadsnakes PPA is an actively maintained repository of Python distributions available to Ubuntu. Deadsnakes carries the burden of hosting versions of Python that have been tried and tested to work on Ubuntu, (their Github organization is essentially a collection of Ubuntu-friendly Python versions).
By adding the Deadsnakes PPA, we're making these Python versions visible to our Ubuntu machines:
Upon adding this repository, you'll immediately receive a prompt explaining this in verbose terms. Press enter to move on.
To pick up the versions of Python that Deadsnakes makes visible to us, we still need to run a quick update:
Now check to see if the version of Python you're looking for is available for download like so:
If available, you'll see an output like so:
That's our green light! Go on and proceed to install Python:
Managing Alternative Python Installations
We now have two versions of Python installed on our machine: the system default Python 3.8.5, and our newly added Python 3.9.2. We want to leave our system default Python installation alone, but we want to run our apps written in Python 3.9... so how do we manage this?
Linux has us covered in this scenario with the update-alternatives command. We can tell Ubuntu that we have many alternative versions of the same software on our machine, thus giving us the ability to switch between them easily. Here's how it works:
update-alternatives twice: once for Python 3.8, and once for Python 3.9. So now we can use
update-alternatives --list [package name] to list all the alternative installations we have installed:
Now we can swap between versions of Python! Run the following:
You should be hit with a prompt like the one below. This will list all the versions of Python your system recognizes. Select the version of Python you'd like to use by providing the "selection" number to the prompt:
To switch Python versions, all you need is to respond to the above prompt with the selection number representing the Python version you want to use.
We've done the "hard" part, but there's a bit of housekeeping to take care of.
The version of Python we just enabled is a brand new installation, meaning there are a few critical pieces we still need to set up. For whatever reason, Python does not ship with its package manager (pip) included. Nor does it ship with distutils (necessary for installing older Python modules) or venv, Python's virtual environment manager. Let's take care of it.
First, we'll need to reinstall python3-apt with our new version of Python active:
Next we'll add distutils:
And of course, pip:
Lastly, venv will need to be reinstalled as well:
You Did It
As absurd as it sounds, successfully updating Python on Ubuntu is a legitimate accomplishment. I've witnessed software developers of all backgrounds struggle with dumb things like "setting up Python." Some tasks are unintuitive and lack any conventional patterns or logic. Updating Python on Ubuntu is one of those tasks.
You've managed to prevail, so congratulations are in order. If you're new to Python, please don't be discouraged by how weirdly complicated this was. The miserable journey we've just embarked on is no indication of what software development is like... or any related profession, for that matter. From here on out, it's sunshine, rainbows, and snakes 🐍.