Copyright 2017-2024 Jason Ross, All Rights Reserved

The version of Python installed on the Raspberry Pi operating system is usually a few versions behind the latest. If this matters to you there's a good chance you will want to update it.

You can't use apt to install the latest version of Python, because it's not available as a package. It's not available as a pre-built installation package from either, so you'll have to built it. It's not too difficult to do this, although there is a problem with the build that I describe how to fix here, so here's how to do it.

Downloading And Building A New Version of Python

On your Raspberry Pi, use your browser to go to the Python downloads page.

Look at the second table on the page, "Looking for a specific release?", and find the release you want - usually the latest. Remember the releases are in date order, not version order, so don't just grab the top one.

Specific release of Python

Click on the Download link for the version you want. Scroll to the bottom of the next page, and right click on the link to the "Gzipped source tarball". Choose "Copy link address" from the right-click menu.

List of Python source files

Open a Terminal window and, in your home directory (the one Terminal opens in), type wget and paste the URL you copied from the Python site. If, for example, you chose Python 3.10.11, your command would look like:


Once this archive has downloaded, you need to extract the files:

tar -zxvf Python-3.10.11.tgz

Then you need to build and configure your new version of Python - change to the directory containing the source files, and then build them:

cd Python-3.10.11
./configure --enable-optimizations

Then, unless you're running this on a Raspberry Pi 4, grab lunch. Then a cup of tea. Then probably dinner. It's going to take a while!

As is the way of these things, there's a good chance this process will fall over, with an error that looks something like one of the following:

ModuleNotFoundError: No module named ‘zlib'


zipimport.ZipImportError: can't decompress data; zlib not available

You'll be totally unsurprised that finding out how to fix this is a nightmare. There are lots of fixes for the problem on other versions of Linux, but not much of any use for the Raspberry Pi.

If you try to install zlib using:

sudo apt install zlib1g zliblg-dev

rebuilding it might work, but probably won't.

The best thing to do is, assuming you're still in the directory you changed into earlier, open the file Modules/Setup. Use the text editor of your choice, open the file - this example uses vi:

vi ./Modules/Setup

Run through the file and find the line that refers to zlib - it looks something like this:

# zlib  zlibmodule.c -lz

Uncomment the line so it looks like:

zlib  zlibmodule.c -lz

and save the file.

Try running the build and configure step again:

./configure --enable-optimizations

When this is running, you can wander off and grab a cup of tea, maybe have dinner, possibly go to bed for the night and come back in the morning. To be fair though, I was running the build on a Raspberry Pi 3, so it's not fast.

Finally, when your new version of Python is built, you need to install it. From the same directory that you ran the build in, run the installation script:

sudo make altinstall

Now, you can check that your new version of Python is available by running the following - assuming you installed a version of Python 3.10:

cd ~
python3.10 --version

You should see the full three-part version of the Python installation you built.

This Seems Like a Lot Of Effort

If you've got this far and you're wondering whether it's worth all of the typing to install a new version of Python, don't worry. I've created a GitHub repository called installpy, which contains the script you need to get you this far in the process. If you just want the script, run wget to get it:


Don't forget to look at the GitHub README for the instructions though.

I've Installed A New Version Of Python - How Do I Use It?

Even though you've just built a new version of Python, running the following command will still give the same output it did before you started:

python3 --version

This happens because when you call python3 it doesn't directly call an executable called python3. Instead, the system makes use of a symbolic link, which links to the executable that ACTUALLY gets called. On my Raspberry Pi 3, it calls /usr/bin/python3.9.

The way the links are organized looks something like:

/usr/bin/python -> /usr/local/bin/python2.7
/usr/bin/python2 -> python2.7
/usr/bin/python3 -> /usr/bin/python3.9

The links are in the /usr/bin directory, but they're found from anywhere because this directory is in the PATH environment variable. You can list all of the links in /usr/bin with the following command:

find /usr/bin -maxdepth 1 -type l -ls

If this is all a bit confusing, just remember this:

When you run python or python3 from the terminal, the operating system redirects you to the actual version of Python that gets run. If you want to change the version of Python that gets run when you type either of these commands, you need to change where these commands get redirected to.

To do this, you need to do three things:

1. Find out where your new version of Python is. Linux has a command to help you do this; assuming you're still working with a version of Python 3.10 as shown in the example above:

which python3.10

Note that you only need the major and minor version numbers of the Python interpreter you installed.

2. Delete the existing symbolic links. This looks like you're deleting a directory, but it's only a link:

sudo rm python

3. Recreate the python link to the version of Python you want to use. Use the path from step 1 - assuming this was returned as /usr/local/bin/python3.10:

sudo ln -s /usr/local/bin/python3.10 python

Now check to make sure it all worked:

python --version

If you want to map python3 to the new version of Python, just repeat this with the python3 alias, e.g.:

sudo rm python3
sudo ln -s /usr/local/bin/python3.10 python3
python3 --version


If you followed the steps above, you'll have a new version of Python running on your Raspberry Pi. You should have avoided the zlib problen that seems to affect building Python of the Pi. All of the links that used to point to the original version of Python now point to the new version, and any new virtual environments you create will now use that version instead.

If you need to add a new version after this, just run the script again, and the re-assign the symbolic links.

Made In YYC

Made In YYC
Made In YYC

Hosted in Canada by CanSpace Solutions