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