Installing/Upgrading Python 3.9 on Ubuntu 20.04 LTS

When writing this, Ubuntu's default python package is 3.8, I like running the latest version of everything so this is how I do it.

My preference is to have the latest version of python run via the python command. In order to achieve this on a fresh install of Ubuntu 20.04 LTS follow these simple steps.

To install the latest version of Python 3.9 on Ubuntu 20.04:

Firstly we'll need to add the deadsnakes ppa to our repo list:

sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository ppa:deadsnakes/ppa -y
sudo apt update
sudo apt install python3.9

Once that is completed python 3.9 should be installed & you should be able to run python3.9 -V to verify:

Scotty$ python3.9 -V
Python 3.9.6

Now let's install pip for 3.9

wget https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3.9 get-pip.py

Defaulting to user installation because normal site-packages is not writeable
Collecting pip
  Using cached pip-21.2.2-py3-none-any.whl (1.6 MB)
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 21.2.2
    Uninstalling pip-21.2.2:
      Successfully uninstalled pip-21.2.2
  WARNING: The scripts pip, pip3 and pip3.9 are installed in '/home/scotty/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed pip-21.2.2

For now we can ignore the PATH warning as we're going to fix it by adding the alias to the .bashrc file.

Now to create an alias in ~/.bashrc

Let's edit the .bashrc file and add some alias lines

sudo nano ~/.bashrc

I added the following code below # some more ls aliases in the file:

# Python / pip Alias
alias python='/usr/bin/python3.9'
alias pip='~/.local/bin/pip3.9'

This will point the command python to our installed 3.9 version and same with pip.

Save that and then run the following to reload bashrc:

source ~/.bashrc

That's it!

We should now get the following when we run python -V & pip -V

Scotty$ python -V
Python 3.9.6
Scotty$ pip -V
pip 21.2.2 from /home/scotty/.local/lib/python3.9/site-packages/pip (python 3.9)

Post Install Notes

If after you've done this, you run sudo apt update and you get an error with apt-pkg. You may have an issue with how it's handling python3 commands. Check your alternatives:

scotty$ update-alternatives --config python3
There is only one alternative in link group python3 (providing /usr/bin/python3): /usr/bin/python3.9
Nothing to configure.

Just fix it by adding python3.8 back into the alternatives:

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

Now configure by choosing python3.8, in my case below it was number 1:

scotty$ sudo update-alternatives --config python3

There are 2 choices for the alternative python3 (providing /usr/bin/python3).

  Selection    Path                Priority   Status
------------------------------------------------------------
* 0            /usr/bin/python3.9   2         auto mode
  1            /usr/bin/python3.8   1         manual mode
  2            /usr/bin/python3.9   2         manual mode

Press <enter> to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/bin/python3.8 to provide /usr/bin/python3 (python3) in manual mode