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?

co flag

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.

ChanganAuto avatar
us flag
One of the first things you *must* understand is to NOT mess with python because it's an integral part of the OS.
ru flag
***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.
ru flag
I'm looking into an alternative to use IDLE with Python 3.9.x installed via PyENV in userspace, but I don't have that as a solution yet - if I find a way that works I'll let you know and post all this as an answer.
user535733 avatar
cn flag
If you want to use a newer version of Python3, then the easiest way is to simply use a correspondingly newer release of Ubuntu. 20.04 (LTS) = Py3.8. 21.04 (non-LTS) = Py3.9
ru flag
@user535733 Not everyone wants to leave the LTS releases. Which is why other solutions exist. :)
user535733 avatar
cn flag
@ThomasWard certainly.
me flag

Since several people advice against messing the system Python installation, here's how you can actually run Python 3.9 after installing it, instead of running the default Python 3.8:

Just use the python3.9 command rather than the python3 command.

To use Python 3.9 in IDLE, install the IDLE for Python 3.9:

sudo apt install idle-python3.9

And start IDLE with the command:

ru flag

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 | 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.

SBL avatar
co flag
Ok, I just realized that you don't need idle to program python on ubuntu and that you can just use terminal to create and run python files, whilst coding on text editor. But I already installed idle from terminal, before realizing this. Should I uninstall idle now? If so how should I do it?
ru flag
@SBL if you installed with my instructions, then `pip3 uninstall idle`. If you want Python 3.9 though, you still need to use the above instructions i wrote except for installing IDLE. Just don't mess with your system python)
SBL avatar
co flag
Thanks for the warning
John Mee avatar
th flag
Downvote for being alarmist. The OP has installed from the official repo with the official tool. Doing so will not break their system, or if it does so then there is a widespread official problem. All they need is an explanation why setting it as the default is a bad idea and some advice to how invoke python3.9 explicitly.

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.