How to Install Latest Python on Linux

In this blog post, we will show you how install latest python on Linux.

Python is a high-level general-purpose programming language used in various domains including data science, games apps, and web and mobile app development. In this tutorial, we look at how to install the latest Python version on Linux.

Install the latest Python On Ubuntu using PPA

By default, Ubuntu comes with Python 3.10.12 already pre-installed. We can verify this by executing the command:

$ python3 -V

Default-Python-Version-Ubuntu-Linux

At the time of writing this post, the latest Python LTS release is Python 3.12.. To install this version, you need to install it from the deadsnakes PPA. This is a repository that provides the latest Python versions for Ubuntu systems. Currently, it supports Ubuntu 20.04 & 22.04 releases. Check out the release notes for more information.

To add the deadsnakes PPA, run the following command:

$ sudo add-apt-repository ppa:deadsnakes/ppa

Information regarding the PPA and supported Ubuntu and Python versions will be printed on the terminal.

ADD-PPA-for-Python-Ubuntu-Linux

To proceed, hit ENTER. The PPA repository entry will be added to the /etc/apt/sources.list.d/ directory and the GPG key to the /etc/apt/trusted.gpg.d/ directory.

Press-Enter-To-Add-PPA-for-Python-On-Ubuntu

Next, refresh the local package index to notify the system of the newly added repository.

$ sudo apt update

Run-Apt-Update-Post-Adding-PPA-Python-Ubuntu

To install Python 3.12, run the following command:

$ sudo apt install python3.12

Install-Latest-Python-Ubuntu-Linux-Apt-Command

The newly installed Python version will now become the default version on your system. To confirm this, verify the Python version.

$ python -V

Check-Latest-Python-Version-Ubuntu-Linux

Managing Multiple Python Versions

In some situations, you will have multiple Python versions installed on the same system, and you might be required to switch from one default version to another.

The update-alternatives command is used to set the priority of different versions of an application such as PHP or Python residing on Ubuntu. The version with the highest priority becomes the default version.

In the example below, we will set Python 3.12 as the default Python version by assigning it the highest priority value of 2  while Python3.10 is assigned the priority of 1.

$ sudo update-alternatives --install /usr/bin/python3 python3  /usr/bin/python3.10 1
$ sudo update-alternatives --install /usr/bin/python3 python3  /usr/bin/python3.12 2

To switch between different Python versions run the following command:

$ sudo update-alternatives --config python3

You will be prompted to provide the selection number of your preferred default Python version. You can press ENTER to keep the current choice or provide a selection number corresponding to the Python version.

Select-Python-Version-Update-Alternatives-Command

Install Latest Python on Linux from Source 

The other alternative to installing Python is doing it from source. To demonstrate this, we will use Rocky 9 as our Linux environment. By default, Rocky Linux comes with Python 3.9 installed.

$ python3 -V

Defualt-Python-Version-in-Rocky-Linux

To successfully build Python from source, you need a set of packages that are provided by the ‘Development Tools’ package. These include rpmbuild, autoconf, automake, GCC, make, libtool among others.

So, to install the ‘Development Tools’ package, run the command:

$ sudo dnf groupinstall "Develpoment Tools" -y

Install-Development-Tools-RockyLinux-dnf-Command

Next, install, the following set of prerequisite packages.

$ sudo dnf install vim openssl-devel wget libffi-devel bzip2-devel -y

Dependecies-for-Latest-Pyhon-RockyLinux

To download the latest Python version, head to the Python download page and use the wget command to download the tarball file.

$ wget https://www.python.org/ftp/python/3.12.0/Python-3.12.0.tgz

Download-Python-Tar-File-Wget-Command

Once downloaded, extract the tarball file with tar command

$ tar -xvf Python-3.12.0.tgz

Next, navigate to the Python folder

$ cd Python-3.12.0

Then run the configuration script with enabled profile-guided optimization (PGO).

$ sudo ./configure --enable-optimizations

Profile-Guided-Optimization-Python-Linux

Next, compile and install Python3.

$ sudo make altinstall

Makeinstall-Python-Linux

Finally, verify that the latest Python version has been installed.

$ python3.12 -V

From the following output, you can see that Python 3.12 has been installed.

Verify-Latest-Python-Version-RockyLinux

Conclusion

And that’s it. In this post, we have demonstrated how to install the latest version of Python on Linux. We hope that you found this insightful. Your feedback is welcome.

Leave a Comment