In Ubuntu 20.04, how do I update Python 3.8 to 3.9 and get to use IDLE Shell 3.9 instead of 3.8 as my default?

I'm on Ubuntu 20.04. I was wondering why even after entering

sudo apt install python 3.9

in terminal, but I still get python 3.8.10 when I enter python3 --version. How do I make it 3.9, so that when I open IDLE, I get "IDLE Shell 3.9.6" instead of "IDLE Shell 3.8.10"?

I've just just started to use Ubuntu and Linux and may not be clearly understanding some things just yet.


DO NOT mess with Python that's installed on the system! THIS WILL DESTROY YOUR ABILITY TO DO ANYTHING ON THE SYSTEM! Package management, updates, everything has a Python dependent component that's dependent on the system-installed Python versions.


There is a way to install newer Python to work with it and use IDLE on it, but it's not straightforward, and relies on an external project (of which I have no affiliation with) called pyenv on GitHub which lets you run multiple Python installations side by side and uses shims to make sure that the version you WANT to use is available.

You must adjust your repository settings here now! We need the source repos (defined by deb-src lines in /etc/apt/sources.list) enabled for the repos you have enabled.

If you are a GUI user, then refer to this page on the Ubuntu wiki for managing repositories in the GUI. You'll need to enable the "Source Code" option under the "Ubuntu Software" tab.

If you are using command line, you shouldn't be using IDLE anyways, but you can edit the sources via the Command Line by referring to this page on the Ubuntu help docs for CLI management of sources. In /etc/apt/sources.list, for every line you have a deb ... line that is not commented out, there shoudl be a commented out deb-src line to match - uncomment that deb-src line and then run sudo apt update. If there are no deb-src lines, then for every uncommented deb line duplicate that line but change the new entry (the duplicated line) to start with deb-src.

Once you've done this, then we need to get pyenv dependencies set up.

Install pyenv in your environment.

PyENV provides an automatic installer - here on GitHub - along with instructions to use it. Those instructions, basically, are this:

curl https://pyenv.run | bash

DO NOT run this with superuser, it isn't needed! Once the install runs it'll instruct you to add some lines to your ~/.bashrc file. Do what it says and then close out and reopen your terminal.

Once that's there, make sure pyenv works by running pyenv versions and see what is listed (it should just list 'system').

Once that's done, we need to get you the requisite build dependencies for your system to install and build Python!

FIRST, you need some build dependencies. These are all adapted for the various core Python versions in each release, only execute the lines relevant to your release.

Get the build dependencies. For Python and IDLE, you need the TK libraries too, so we'll get all the dependencies first.

For 18.04:

sudo apt build-dep python3.6 python3-tk

For 20.04:

sudo apt build-dep python3.7 python3-tk

For 21.04 (and 21.10 once it's released):

sudo apt build-dep python3.9 python3-tk

This will install a LOT of libraries. You need to permit them to be installed. This installs ALL the build dependencies needed to let Python build and operate; this pulls the dependencies for the system installed Python versions but should work fine for the later Python versions that pyenv will install in userspace.

Now, once these're all installed, we need to set up your pyenv and have it install a Python version in userspace!

Assuming that pyenv versions shows you output, then we can move on to getting your stuff set up.

Run pyenv install 3.9.7. This will download the Python 3.9.7 tarball from Python upstream and then build and compile and install Python 3.9.7 into the userspace for pyenv. This will not harm the Python on your system.

Once it's done, run pyenv local 3.9.7. This will set up the shims which point the Python executables to your local versions for your own shell. Make sure however that pyenv global still shows 'system' as the result - otherwise you'll break apt and other things.

Now, you can get IDLE and begin to use it.

Firstly, verify pip3 is in the .pyenv/shims path of your home directory. That will mean that pyenv is properly set up. Run pip3 --version, and make sure that it's showing pip ... from (long path in /home/$USER/.pyenv/...) (python 3.9) (replacing the 'long path' bits with the actual path for your reference.

If all that looks good and it shows 'python 3.9' there, then you can run pip3 install --upgrade --user idle and install IDLE. You can then execute from the command line idle and launch the IDLE editor. You will have to manually make a shortcut on the GUI for this to work directly (beyond the scope of this post sorry!) as a GUI application without opening it on the Python side of things on the Command LIne first. However, this will properly get IDLE running.

(However, it being a TK application, it looks a little... ugly... just an FYI.)

Tested on a 20.04 system, but pyenv should work fine with later Ubuntu releases.