The Ultimate Guide to Installing the Latest Python Version on Ubuntu Linux

The Ultimate Guide to Installing the Latest Python Version on Ubuntu Linux

Upgrade your Python game on Ubuntu Linux with the latest version Discover two simple methods to install it, change your default version if needed, and gear up as a Pythonista

Key Takeaways

To ensure you have the most up-to-date version of Python on your Ubuntu system, follow these steps: add the deadsnakes PPA to your repository list, update the list, and then execute the apt command "sudo apt install python3.12" to select and install the desired version.

To get the most recent Python version on Ubuntu, you can install it from source. If you opt for the PPA method, you will have to modify the default Python version on your system using the update-alternatives command.

Each year, the Python programming language undergoes significant updates and enhancements, although the version included in Ubuntu Linux may not always be the most recent. If you require a more current release, follow these instructions to obtain the latest version of Python on Ubuntu.

In this tutorial, we will be utilizing Ubuntu 22.04 LTS. As an example, we will install Python 3.12, which became available on October 2, 2023. However, this guide is also applicable to previous versions of Ubuntu and any future Python releases.

Step 1: Check If Python Is Installed (And the Current Version)

By default, Ubuntu is equipped with pre-installed Python. To confirm its presence and determine the current version, you can check it. This process not only displays the installed version but also indicates whether it requires an update to the latest version.

To check your Python version on Ubuntu, run the below command:

python3 --version

The Ultimate Guide to Installing the Latest Python Version on Ubuntu Linux

The current version we have is 3.10.12, which is not the most up-to-date version. To compare your outcomes, you can refer to the official Python website for a list of all version numbers and their maintenance statuses.

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 will discuss two methods. If you prefer a fast and simple approach, choose the first one. If you have more experience in developing software from source files, opt for the second method.

Method 1: Install the Latest Version of Python Using deadsnakes PPA

To access software packages from third-party repositories, you must add them to your repo list. This allows you to install any desired software from said repositories. One such repository, deadsnakes PPA, provides various versions of Python, including the latest release. To add deadsnakes PPA to your repo list, execute the following command:

sudo add-apt-repository ppa:deadsnakes/ppa

The Ultimate Guide to Installing the Latest Python Version on Ubuntu Linux

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

The Ultimate Guide to Installing the Latest Python Version on Ubuntu Linux

To initiate the installation process, press "y" and then press Enter. Please wait until the installation is completed. Once the installation is finished, you can immediately begin utilizing the updated version. To do so, you will need to specify the version number when selecting Python. In this particular example, execute the following command:

python3.12

The Ultimate Guide to Installing the Latest Python Version on Ubuntu Linux

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

To manually install Python, you can use the source file provided on the official Python website.

Before proceeding with the installation process, ensure that you have the necessary packages installed, as they are vital for building software from the source. Begin by updating your system using the command outlined below:

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.

The Ultimate Guide to Installing the Latest Python Version on Ubuntu Linux

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 Ultimate Guide to Installing the Latest Python Version on Ubuntu Linux

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

The Ultimate Guide to Installing the Latest Python Version on Ubuntu Linux

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

The Ultimate Guide to Installing the Latest Python Version on Ubuntu Linux

To enhance the performance of Python, we have incorporated the --enable-optimizations flag into the script. This flag enables different compile-time optimizations, resulting in faster execution of Python. Moreover, the script will generate the required Makefiles automatically. To build Python, utilize the provided Makefile and execute the following command:

```

make

```

Remember to keep the

The Ultimate Guide to Installing the Latest Python Version on Ubuntu Linux

intact in its original position.

sudo make install

The Ultimate Guide to Installing the Latest Python Version on Ubuntu Linux

Please wait patiently as this will take some time. If all the commands have been executed successfully, the latest version of Python should be installed on your Ubuntu system.

Once the process is finished, the installed version should become the default one on your device. To confirm this, check the Python version again.

python3 --version

The Ultimate Guide to Installing the Latest Python Version on Ubuntu Linux

Our system is now using Python 3.12, as expected.

(Optional) Step 3: Change Your Default Python Version

By now, you should have installed the latest version of Python successfully. However, if you utilized the PPA method, simply installing the new version is insufficient to begin using it by default. Your system is still configured to utilize the old version. You can confirm this by checking the version once more.

In order to ensure that you are utilizing the installed version, you need to modify the symbolic link to point to that specific version. This can be achieved by employing the update-alternatives command. Allow us to demonstrate how to do so.

Specify the path to the newly installed Python for the python3 command we are using by running the following command:

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

The Ultimate Guide to Installing the Latest Python Version on Ubuntu Linux

The command may differ slightly based on the version you install. The number "1" at the end of the command determines the priority of the version. This helps avoid conflicts, as higher priority numbers take precedence.

If there is only one option in the link group, it should automatically become the default version in the new release. However, if there are multiple versions available, you will need to choose the new version. To do so, please execute the following command:

sudo update-alternatives --config python3

The Ultimate Guide to Installing the Latest Python Version on Ubuntu Linux

Please take note of the numbering in the "Selection" column. Input the respective order number for the Python version you desire to utilize. Proceed by pressing the Enter button. At this point, you should be actively utilizing the most recent Python version on your Ubuntu system. To confirm, kindly verify the version once again.

Should You Upgrade to a New Version of Python?

Upgrading to the latest versions of programming languages like Python brings numerous advantages, including enhanced performance, strengthened security measures, improved user-friendliness, and better compatibility with systems. Therefore, it is advisable to consider updating to the most recent version. Overall, this decision is highly recommended.

However, there are certain drawbacks to consider. Switching to a new version of the language can alter your usage, especially when there are significant changes in the syntax. Therefore, it will take some time for you to adjust before it becomes routine again. Additionally, certain packages may not initially support the upgraded version, and in some cases, new versions can disrupt your existing projects and create chaos. Consequently, caution is essential when dealing with these situations.

Nevertheless, if the latest Python version offers valuable features that you wish to utilize or includes crucial security updates and bug fixes, then it is recommended to upgrade, as long as it is compatible with your existing code. Ultimately, your decision should be based on your specific requirements and preferences.

Our suggestion is to utilize the latest Python version in a distinct setting to evaluate its functionality prior to commencing the integration into a significant project. Additionally, pyenv can be employed to alternate between versions while undertaking different tasks.

Gear Up With the New Python Version

Once you have installed the most recent version of Python on your Ubuntu device, you can begin making the most of its latest features. It is advisable to review the release notes and official documentation for further insights into this version. If you are interested in installing Python on alternative platforms besides Ubuntu, our comprehensive guide on Windows installation is available for your reference.