Key Takeaways
- To install the latest version of Python on Ubuntu, add the deadsnakes PPA to your repository list, update the list, and name the version you want in an apt command like “sudo apt install python3.12”.
- You can also get the latest Python version installed on Ubuntu by building it from source.
- If you use the PPA method, you need to change the default Python version on your system using the update-alternatives command.
Every year, the Python programming language receives major updates and improvements. But the version preinstalled on Ubuntu Linux may not be the latest. If you need a more up-to-date release, here are the steps to getting the latest version of Python on Ubuntu.
For this tutorial, we’re using the Ubuntu 22.04 LTS version. For demonstration, we will install Python 3.12, which was released on October 2, 2023. But this guide is applicable for older versions of Ubuntu and any upcoming Python releases.
Step 1: Check If Python Is Installed (And the Current Version)
Python comes installed on Ubuntu by default. Just to be sure, you can ensure it’s installed by checking its current version. That also shows you which version is installed and whether it’s already in its latest version or not.
To check your Python version on Ubuntu, run the below command:
python3 --version
As you can see in our case, it’s 3.10.12, which is not the latest version. To compare your results, you’ll find all the version numbers and maintenance statuses on the official Python website.
With that out of the way, let’s proceed to install the latest version of Python on our Ubuntu device.
Step 2: Install the Latest Python Version on Ubuntu
We’ll cover two methods here. If you want a quick and simple method, go with the first one. If you’re more comfortable building software from source files, use the second method.
Method 1: Install the Latest Version of Python Using deadsnakes PPA
A Personal Package Archive (PPA) is a third-party repository that contains software packages. You need to add such repositories to your repo list first. Then you’ll be able to install any software from that repository. For the latest version of Python, we’re going to use deadsnakes PPA. It contains many versions of Python. To add that repository, use the below command:
sudo add-apt-repository ppa:deadsnakes/ppa
When asked to confirm, press the Enter button.
To make the changes take effect, you need to update your software repository cache. So update it with this command:
sudo apt update
Now you’re ready to install software from the PPA. So install the latest version of Python by simply inputting this command:
sudo apt install python3.12
Press “y” followed by the Enter button to start the installation. Wait until the installation is finished. After the installation, you can start using the new version right away. For that, you need to use the version number when choosing Python. So in this example, we’re going to use this command:
python3.12
Instead, if you want to use the new version as the default one, then proceed to step 3.
Method 2: Install the Latest Version of Python Using the Source Code
Another method for manually installing Python is using the source file from the official Python website.
Before going into the process, you’ll need some packages installed that are essential for building software from the source. First, update your system with the below command:
sudo apt update
Then install the necessary dependencies with this command:
sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev
Now you’re ready to install Python using the source file.
So now, go to the downloads page. Then press the “Download Python <version>” button to start the download.
Open your terminal and navigate to the location where you downloaded the file. By default, it’s the “Downloads” directory. Go to that directory with this command:
cd ~/Downloads
The source file is an XZ compressed source tarball file. You need to extract this tar file first. Extract the Python source file with this command:
tar -xJf Python-3.12.0.tar.xz
Depending on which version you downloaded, your file name will be different and so will the command. Navigate into the created directory with this command:
cd Python-3.12.0
Now you need to compile the Python source code. For that, we’ll use the provided “configure” script. Compile the source code by running the below command:
./configure --enable-optimizations
We’ve also added the –enable-optimizations option to the script to enable various compile-time optimizations so that Python runs faster. The script will create the necessary Makefiles for us. Use the generated Makefile to build Python with this command:
sudo make install
This will take a while so wait patiently. If you’ve successfully executed all the commands, that should install the latest Python on your Ubuntu system.
Once the process is done, you should have the installed version as the default one on your device. To see that, check the Python version again:
python3 --version
Our system is now using Python 3.12, as expected.
(Optional) Step 3: Change Your Default Python Version
You should’ve successfully installed the latest version of Python by now. However, if you used the PPA method, that’s not enough to start using the new version of Python by default. Your system is still using the old version. You can verify that by checking the version again.
To make sure you’re using the installed version, you must change the symbolic link to point to that version. You can do that using the update-alternatives command. Let us show you how.
We will specify the path to the newly installed Python for the python3 command we use. To do that, run the below command:
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.12 1
Again, depending on the version you install, the command will slightly differ because of the version number. The “1” at the end of the command specifies the priority you want to put the version in. This saves you from any conflict because the higher the priority number, the higher the precedence.
If there is only one alternative in the link group, then this should be enough to make the new version the default one. But, if there are several versions in the group, you need to select the new version. For that, run the below command:
sudo update-alternatives --config python3
Notice the numbers under the “Selection” column. Enter the order number for the Python version you want to use. Then press the Enter button. Now you should be using the latest Python version on Ubuntu. Check the version to confirm one last time.
Should You Upgrade to a New Version of Python?
New versions of programming languages such as Python always come with perks such as improved performance, security patches, more usability, and better system support, among others. So it might seem logical to update to the latest version. And in general, that’s totally fine.
But it does come with some catches. Upgrading to a new version can change the way you use the language if there is a big change in a part of the syntax. So you’ll need some getting used to before it becomes normal again. Some packages may not support the new version right away. Even worse, some new versions can break your existing projects and make a mess. So you must be careful with that.
If the new Python version contains a useful feature you want to use or has important security updates and bug fixes, then feel free to upgrade to that version, providing that it’s compatible with your old code. So, it comes down to your needs and preferences.
Our recommendation would be to use the new Python version in a separate environment to test how it works before you start implementing it on a major project. You can also use pyenv to switch between versions when working on different tasks.
Gear Up With the New Python Version
With the latest version of Python installed on your Ubuntu device, you can start taking advantage of all the new features available. Remember to read the release notes and the official documentation to learn more about that version.
Interested in installing Python on platforms other than Ubuntu? Check out our comprehensive guide on how to install Python on Windows.