how to change default python version raspberry pi

How To Install the Latest Python Version on Linux?

New versions of Python are released all the time, and there are generally several series available at the same time. Different applications on your system may have different requirements in terms of Python version, and the goal of this tutorial is to show you how to install and use the one you need (even if not available in your Linux distribution repository).

The latest Python version is available on the official website and can be installed on any Linux distribution from sources. Most systems are a few versions left if you use the package manager.

I’m demonstrating this tutorial by using Ubuntu, but it should work the same way on any Linux distribution (especially if based on Debian, you may need to adjust some commands if it’s not the case).

Linux doesn’t have to be intimidating. With my e-book, Master Linux Commands, you’ll uncover the secrets of the terminal in a fun, step-by-step journey. From basics to scripts, get ready to level up your Linux skills. Oh, and did I mention the handy cheat sheet you get as a bonus?

How to know which Python version is installed

Several Python versions can live in parallel on the same system. To find the exact version number, use the command line “python –version” or “python3 –version”.

Depending on your distribution, you may have different commands available: like “python”, “python2” or “python3”. You can even have several of them working, and returning a different value, as it’s possible to have multiple versions installed on the same system.

The exact version depends on the latest one available in the default repository (which will vary depending on your distribution). If you use Debian, the version you have is probably a couple of years old, and generally less on Ubuntu (still a few months behind).

If you don’t have any Python executable available and don’t care about getting the latest version, the easiest solution is to install it with your package manager. For example:
sudo apt install python3

Your Go-To Linux Command Reference!
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now

But if you need the latest version, continue reading this article.

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.

Join Our Community!

Connect, learn, and grow with other Raspberry Pi enthusiasts. Support RaspberryTips and enjoy an ad-free reading experience. Get exclusive monthly video tutorials and many other benefits.

Learn more

This first table gives you an overview of the latest Python versions. As you can see on the Download page, even if I test this on Ubuntu (which is updated very frequently compared to other distributions), my Python version is still over 6 months old, so it might be required to update it for some applications.

Install the latest Python version on Linux

As official repositories are always a few Python versions late, the only way to install the latest Python version on Linux 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.
  • Step 3: Configure the system to use the latest Python version.

Let’s see how to do this.

Method 1: Use the package manager

On some distributions (like Ubuntu) new versions are quickly available with the package manager (and you can always add custom repositories)

So, you can do a quick search first to ensure the version you need is not already available in the default repository. You can, for example, use something like:
sudo apt search ^python3.11
To see if the 3.11 version is available with APT.

In this example, it’s the case. It’s Python 3.11.0, not 3.11.2, which is the latest one on the website at the time of writing, but it might be enough for your applications (and it’s way easier to install and keep up-to-date if you use APT).

Your Go-To Linux Command Reference!
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now

Method 2: Manual installation

If you can’t find the version you’re looking for with APT, the alternative is to install Python manually by downloading the latest archive from the official website. Here is 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.11.2.
  • 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 or connect via SSH to type a few commands.

  • Download the latest Python file with:
    Replace the URL with the link you pasted in the previous step.
    wget <url>
  • Extract the files with:
    tar -zxvf Python-3.11.2.tgz
    Change the Python version if you downloaded another one.
    tar -zxvf <filename>

I’m giving you the short version in this tutorial, but if you need more details on how to install a tar.gz file on Ubuntu (or other archives formats), it’s probably better to click on this link for more guidance.

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’s latest version

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

  • Go to the folder containing the extracted files:
    cd Python-3.11.2
  • Run the configuration command:
    ./configure --enable-optimizations
    If you have an error, you probably need to install or update the missing components.

    In this case, I needed the packages related to GCC to compile Python, I fixed it with:
    sudo apt install build-essential
  • Once done, run this command to install it:
    sudo make altinstall

The difference between “make install” and “make altinstall” is that the latter won’t change the default version on your system (so it won’t break anything). I’ll explain how to switch after that (once you have tested everything with the new version).

Change the default Python version on Linux

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 now have:

  • python3.10: The default Python 3 version I had before testing this tutorial.
  • python3.11: The one I installed from sources.

But when I use “python3 –version”, I still use Python 3.10.
To choose the version you want to run, you have two choices:

  • Always run a Python script with the exact version you intend to use, for example:
    Which is probably the safest 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 install:

Here is how to change this link:

  • Go to /usr/bin:
    cd /usr/bin
  • Remove the current link:
    sudo rm python3
  • Link the version you intend to use instead:
    sudo ln -s /usr/local/bin/python3.11 python3
  • Check that everything is fine:
    python3 --version
    It should now display the version you just installed (3.11.2 for me).

Feel free to create a link for “python” instead of “python3” if you want a shorter command to use.

Your Go-To Linux Command Reference!
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now

Final thoughts

I hope this post helped you understand how Python versions work on Linux. You now know that you can have 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-library and python3-library 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 it.

Your Go-To Linux Command Reference!
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now

🛠 This tutorial doesn't work anymore? Report the issue here, so that I can update it!

If you are looking for exclusive tutorials, I post a new course each month, available for premium members only. Join the community to get access to all of them right now!

Additional Resources

Overwhelmed with Linux commands?
My e-book, “Master Linux Commands”, is your essential guide to mastering the terminal. Get practical tips, real-world examples, and a bonus cheat sheet to keep by your side.
Grab your copy now.

VIP Community
If you just want to hang out with me and other Linux 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?
Python is a great language to get started with programming on any Linux computer.
Learn the essentials, step-by-step, without losing time understanding useless concepts.
Get the e-book now.

Similar Posts