How to Install Python 3.12 on Ubuntu 24.04, 22.04 or 20.04

Python 3.12 brings a host of enhancements and refinements to the programming language, catering to both usability and performance. This latest stable release integrates significant language and standard library changes, ensuring a cleaner and more efficient coding experience.

Below are some of the key points of the Python 3.12 release:

  • Improved Usability:
    • Enhanced f-strings: Removal of many limitations, making them more flexible.
    • ‘Did you mean …’ Suggestions: Continued improvements to assist developers in debugging.
  • Language Enhancements:
    • New Type Parameter Syntax: Simplifies the use of generic types.
    • Type Statement: Facilitates the creation of type aliases, improving code clarity.
  • Standard Library Updates:
    • Deprecated APIs Cleanup: Streamlined library with obsolete functions removed.
    • Removed distutils Package: Encourages the use of more modern alternatives.
    • Filesystem Improvements: Enhanced support in os and pathlib modules.
  • Performance Boosts:
    • Various modules have undergone performance optimizations, ensuring faster execution times and better resource management.

Python 3.12 sets the stage for a more intuitive and powerful coding environment. Its refinements aim to streamline development processes, making it easier for developers to write clean, efficient, and maintainable code. Now, we will delve into installing Python 3.12 utilizing the well-known “deadsnakes” Python PPA that maintains the latest builds.

Install Python 3.12 via APT PPA

Update Ubuntu Before Python 3.12 Installation

Before installing Python 3.12, ensure your Ubuntu system is up-to-date. Taking this step helps prevent potential conflicts during installation.

To update your system, execute the following command in your terminal:

sudo apt update

Next, upgrade any outdated packages on your system with the command:

sudo apt upgrade

Import Python 3.12 PPA

For Ubuntu users, the best way to install Python 3.12 is to use the Launchpad PPA that the “deadsnakes” team maintains. This team consistently updates this repository with the latest Python versions and any necessary additional packages.

Run the following command to import the stable PPA for Python 3.12:

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

If you want the latest upstream version of Python 3.12, which is still under active development, import the Python Nightly PPA from the same team. We only recommend this step if you seek the most recent updates:

sudo add-apt-repository ppa:deadsnakes/nightly -y

Refresh the APT Cache After Python 3.12 PPA Import

After importing the PPA, it’s necessary to update the APT index to reflect the newly added repository. You can do this by running the following command:

sudo apt update

Install Python 3.12 via APT Command

With the Python 3.12 PPA now part of your system’s repositories, you can install Python 3.12 with executing the following command:

sudo apt install python3.12 -y

To confirm the successful installation and check the build version of Python, use the command:

python3.12 --version

The output should resemble:

Python 3.12.x

Optionally, you can install additional modules for Python 3.12. The following command includes a broad range of modules, but you can remove any that you don’t require:

sudo apt install python3.12-{tk,dev,dbg,venv,gdbm,distutils}

If you’re looking for a specific module not listed above, you can search for it in your terminal using the apt search Command, which will list all the available packages for Python 3.12:

apt search python3.12-*

For a comprehensive installation, you can install all extras with the command:

sudo apt install python3.12-full

Finally, if you need to install multiple versions of Python alongside Python 3.12, you can do so using the following command:

sudo apt install python{2.7,3.7,3.8,3.9,3.10,3.11,3.12}

Remember to remove the version numbers you don’t need from the command. Ideally, you should avoid installing all these versions, especially those that have reached their end-of-life (EOL) status. With Python 3.12 successfully installed, you can now learn how to set the default Python.

Switch Default Python Versions with Python

As a user of several Python versions, it’s beneficial to understand how to switch your default Python version to suit different tasks or requirements. Adjusting symbolic links and selecting your preferred version can easily manage this process.

Create Symbolic Links for Each Python Version

To start, generate symbolic links for each Python version individually. Assign each version a group name (in this case, ‘python’) and an associated option number.

For instance, you might use the following commands:

sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.7 2
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 3
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.9 4
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.10 5
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.11 6
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.12 7

These commands create symbolic links for the Python versions. Note that these commands are customizable according to the Python versions available on your system, and you don’t need to execute all the commands if you don’t have every version installed. Only the versions present on your machine will have their symbolic links created.

List Available Python Versions

Once you’ve established your symbolic links, the next step is to list the Python versions to verify their installation and observe the default version.

You can achieve this by running the following command:

sudo update-alternatives --config python

Running this command will present you with an output detailing the Python versions installed on your system. The default Python version will be marked with an asterisk (*) next to its selection number.

Change Python 3.12 Version to Alternative

To change the default Python version, input the selection number of your desired version. For example, to set Python 3.11 as your default, enter ‘2’ (based on the example commands mentioned earlier). Remember, the option numbers for versions on your system might vary, so always refer to the list provided.

Once you complete this, the message displayed will be:

update-alternatives: using /usr/bin/python3.11 to provide /usr/bin/python (python) in manual mode

The message shows that you’ve set Python 3.11 as the default version. To verify this, rerun the command (sudo update-alternatives –config python). You’ll see an asterisk (*) next to Python 3.11, marking it as the default version.

Final Thoughts

In this article, we dive deep into the workings of Python 3.12 on the Ubuntu Linux distribution. We focus on transitioning seamlessly between multiple Python versions and installing PIP for Python 3.12. Python programmers need to understand these techniques to meet various project requirements. We provide clear steps and detailed explanations to help you manage multiple Python versions and set up PIP confidently for optimal Python package management.

Useful Links

Here are some valuable links related to using Python on an Ubuntu system:

  • Python Official Website: Visit the official Python website for information about the programming language, its features, and download options.
  • Python Documentation: Access comprehensive documentation for detailed guides on installing, configuring, and using Python.
  • Python Community: Join the Python community to connect with other developers, participate in discussions, and find support.
  • Python FAQ: Find answers to frequently asked questions about using Python.
  • Python GitHub Repository: Visit the Python GitHub repository to view the source code, report issues, and contribute to the development.
  • PyPI (Python Package Index): Explore the Python Package Index to find and install packages and libraries for Python.
Joshua James
Follow me
Latest posts by Joshua James (see all)