How To Install the Latest Python Version on Raspberry Pi?


Raspberry Pi and Python work well together, and Python comes pre-installed on your Raspberry Pi OS. But as often with computers and programming, it’s not always that simple. In this article, I’ll tell and show you everything you need to know about the Python versions on your Raspberry Pi.

The only way to install the latest Python version on Raspberry Pi OS is to download it from the official website and install it from sources. Raspberry Pi OS repositories are generally late from a few versions.

As always, I’m doing this on my Raspberry Pi, so you won’t have to face bugs and errors yourself. Follow my recommendations below and everything should work on the first try!

By the way, if you get overwhelmed as soon as Python is required for a project, I recommend checking out my e-book “Master Python on Raspberry Pi“. It will guide you step-by-step to learn the essential concepts (and only the essential concepts) required to achieve any project in the future. Raspberry Pi without Python is like a car without an engine, you miss all the fun parts. Get 10% off by downloading it today!

I answer your questions - Raspberry...
I answer your questions - Raspberry Pi FAQ

How to know which Python version is installed

Two versions of Python come preinstalled on Raspberry Pi OS: Python 2 and Python 3. To find the exact version number, use the command line “python –version” and “python3 –version”.

It can be a little disturbing, but yes there are two versions already installed on your Pi. When you use the command “python” to run a script, you are running it with Python 2. And the “python3” command will do the same thing with version 3.

The exact version depends on the latest one available in the Raspberry Pi OS repository. In most cases with Debian based distribution, these versions are a bit dated. At the time of writing, Python 3.7.3 is two years old, and it’s the one preinstalled in Raspberry Pi OS.

Grab my Python cheat sheet!
If like me, you always mix the languages syntax, download my cheat sheet for Python here!

If you were wanting to learn how to use Python 3 on your Raspberry Pi, you have the answer: use the “python3” command instead of “python”. And if you want to install a more recent version, keep reading to learn how.

By the way, if it is unclear to you why Python is used on Raspberry Pi, you should click on that link to know everything about it.

Find the latest Python version available

The easiest way to find the latest Python release available is to go to the official Python website. On the download page, the latest versions are listed with their release date and maintenance status.

This first table gives you an overview of the latest Pythons versions. As you can see on the Download page, Python 3.7 was released in 2018. In the next section, we’ll learn how to update it on your Raspberry Pi.

Install the latest Python version on Raspberry Pi

As Raspberry Pi OS is always a few Python versions late, the only way to install the latest Python version on your Raspberry Pi is to download the source code from the official website and install it manually:

  • Step 1: Download the latest version of Python from the official website.
  • Step 2: Extract the files on the Raspberry Pi.
  • Step 3: Configure the system to use the latest Python version.

Let’s see how to do this.

Download and extract the latest Python version

  • Go to the Python download page.
  • Look for the second table on that page “Looking for a specific release?”:
  • Click on the “Download” link corresponding to the version you want to install.
    In my case, I will install Python 3.9.5.
  • Scroll to the bottom of the next page, and find the list of download links:
  • Right click on “Gzipped source tarball” and choose “Copy link address” from the browser contextual menu.

For the following, you need to open a terminal on Raspberry Pi OS, or connect via SSH to type a few commands. If you need help with the SSH part, you can read my tutorial here which has all the information you might need.

  • Download the latest Python file with:
    wget https://www.python.org/ftp/python/3.9.15/Python-3.9.15.tgz
    Replace the URL with the link you paste in the previous step.
    wget <url>
  • Extract the files with:
    tar -zxvf Python-3.9.15.tgz
    Change the Python version if you downloaded another one.
    tar -zxvf <filename>

Are you a bit lost in the Linux command line? Check this article first, for the most important commands to remember, and a free downloadable cheat sheet so you can have the commands at your fingertips.

Configure and install Python latest version

Now we need to compile the source code to install this Python version on your Raspberry Pi:

  • Move to the folder containing the extracted files:
    cd Python-3.9.15
  • Run the configuration command:
    ./configure --enable-optimizations
    As Python is already installed on your Raspberry Pi, it should work directly. But if you have an error, you probably need to install or update the missing components.
  • Once done, run this command to install it:
    sudo make altinstall

This should take a few minutes depending on your Raspberry Pi model and version (5 to 10 min on Raspberry Pi 4).

Grab my Python cheat sheet!
If like me, you always mix the languages syntax, download my cheat sheet for Python here!

Make Python 3 the default version on Raspberry Pi OS

Each installed version of Python on your system adds a new executable in /usr/local/bin that you can use to run a program. For example, in my case I know have:

  • python2.7 : The current Python 2 version by default.
  • python3.7 : The default Python 3 version on Raspberry Pi OS at the time of writing.
  • python3.9 : The one I installed from sources.

But when I use “python –version”, I’m still using Python 2.7.
To choose the version you want to run, you have two choices:

  • Always run a Python script with the exact version you want to use, for example:
    python3.9 myscript.py
    Which is probably the safer option if you switch from one version to another regularly.
  • Or you can replace the link in your /usr/local/bin folder to point to the version you want to use as default.

Here is what it looks like on a fresh RPI OS install:

Here is how to change this link:

  • Go to /usr/bin:
    cd /usr/bin
  • Remove the current link:
    sudo rm python
  • Link the version you intend to use instead:
    sudo ln -s /usr/local/bin/python3.9 python
  • Check that everything is fine:
    python --version
    It should now display the version you just installed (3.9.5 for me).
Note: If you are using Thonny to code in Python, it uses "/usr/bin/python3" by default, which links to the latest version installed by Raspberry Pi OS (3.7.3 in my case). If you want to use the latest Python version with Thonny, you have to update this link as well.

Update Python on Raspberry Pi

We have seen how to install a specific Python version on Raspberry Pi OS, but how to update it from there?

To update Python on Raspberry Pi, start by making sure your whole system is up-to-date:
sudo apt update
sudo apt upgrade

Even if Raspberry Pi OS is always a few versions behind the latest Python version available, you can still get updates with apt, as for any other software on your device.

Once done, check the currently installed version with:
python --version
python3 --version

If it doesn’t show the version you need to use, you will have to follow this tutorial from the beginning once again. Downloading the sources and compiling it for the desired version is the only solution each time you want to update, there isn’t a magic command to do it automatically.

And it’s important to know which version you are using when you install additional libraries for Python on your Raspberry Pi.

Get My Commands Cheat Sheet!
Grab your free PDF file with all the commands you need to know on Raspberry Pi!

Reminder: Remember that all the members of my community get access to this website without ads, exclusive courses and much more. You can become part of this community for as little as $5 per month & get all the benefits immediately.

Final thoughts

I hope this post made how the Python versions work on Raspberry Pi clear for you. You now know that there are at least two versions installed and that you can easily switch between them by changing the command you use.

Also, make sure to install the modules related to the Python version you use (python-gpiozero and python3-gpiozero are two different packages). PIP also has two versions (pip and pip3). It can be misleading for beginners, but it’s really useful once you are used to.

Let me know in the community if you have any related question about Python on Raspberry Pi.
Meanwhile, feel free to check my other tutorials on Python on this website:

I also have a list of 15 projects with Python on Raspberry Pi that you can check if you need some inspiration.

Get My Commands Cheat Sheet!
Grab your free PDF file with all the commands you need to know on Raspberry Pi!

Additional Resources

Not sure where to start?
Understand everything about the Raspberry Pi, stop searching for help all the time, and finally enjoy completing your projects.
Watch the Raspberry Pi Bootcamp course now.

Master your Raspberry Pi in 30 days
Don’t want the basic stuff only? If you are looking for the best tips to become an expert on Raspberry Pi, this book is for you. Learn useful Linux skills and practice multiple projects with step-by-step guides.
Download the e-book.

VIP Community
If you just want to hang out with me and other Raspberry Pi fans, you can also join the community. I share exclusive tutorials and behind-the-scenes content there. Premium members can also visit the website without ads.
More details here.

Need help building something with Python?
Create, understand and improve any Python script for your Raspberry Pi.
Learn the essentials, step-by-step, without losing time understanding useless concepts.
Get the e-book now.

You can also find all my recommendations for tools and hardware on this page.

Patrick Fromaget

I'm the lead author and owner of RaspberryTips.com. My goal is to help you with your Raspberry Pi problems using detailed guides and tutorials. In real life, I'm a Linux system administrator with a web developer experience.

Recent Posts