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

Python 3.12 is the latest release in the Python programming language series, bringing new features, optimizations, and security improvements. Python is widely used in various fields, from web development and data science to automation and scripting. With each new release, Python continues to evolve, offering more efficient and powerful tools for developers. Python 3.12 introduces several enhancements that improve the language’s performance and capabilities, making it an attractive option for developers who want to stay up-to-date with the latest features.

On Ubuntu 24.04, 22.04, or 20.04, you can install Python 3.12 via the deadsnakes PPA, a well-maintained repository by the deadsnakes team that provides the latest Python versions for Ubuntu. This PPA ensures that you have access to Python 3.12 even if it’s not yet available in the official Ubuntu repositories. Additionally, this guide will include tips on how to manage multiple Python versions on your system, allowing you to switch between different versions as needed using tools like update-alternatives or virtual environments.

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

Finalize Installation of Python 3.12

With the Python 3.12 PPA now part of your system’s repositories, you can install Python 3.12 by 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 give you 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

Installing Python 3.12 on your Ubuntu system via the deadsnakes PPA allows you to leverage the latest features and improvements in Python. With this setup, you can easily manage multiple Python versions, giving you the flexibility to run different projects that may require different versions of Python. By using tools like update-alternatives or virtual environments, you can seamlessly switch between Python versions, ensuring that your development environment is both versatile and up-to-date.

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.

2 thoughts on “How to Install Python 3.12 on Ubuntu 24.04, 22.04 or 20.04”

Leave a Comment