How to Install Python 3.12 on Ubuntu 22.04 or 20.04

Python 3.12, the newest version of the renowned programming language, offers an array of enhancements and features tailored to modern development needs. For those aiming to install Python 3.12 on Ubuntu 22.04 Jammy Jellyfish or its older stable release of Ubuntu 20.04 Focal Fossa, this introduction provides a comprehensive overview of its capabilities.

Key Enhancements:

  • Grammar Features: Python 3.12 refines f-string syntax, allowing for more complex expressions. Additionally, comprehension inlining offers a streamlined approach to handle comprehensions.
  • Typing Upgrades: The language now supports buffer protocol access, precise **kwargs typing using TypedDict, and introduces an Override Decorator for static typing.
  • Deprecations and Removals: Developers have phased out Legacy Unicode APIs and the distutils package, allowing modern tools and practices to take precedence.
  • Error Messaging: Enhanced error messages now provide insightful feedback, suggesting potential modules from the standard library during NameErrors and offering recommendations during ImportError exceptions.
  • Module Improvements: Numerous standard library modules, such as asyncio, pathlib, json, and more, have received significant updates.
  • Optimizations: Python 3.12 introduces changes to CPython bytecode, ensuring efficient execution.
  • CPython API Changes: The release removes Legacy Unicode APIs based on Py_UNICODE* representation and introduces changes in argument parsing functions.

Python 3.12 is a testament to the language’s commitment to innovation, readability, and efficiency. The subsequent guide will detail the steps to seamlessly install Python 3.12 on Ubuntu 22.04 Jammy Jellyfish or Ubuntu 20.04 Focal Fossa, ensuring developers have the latest Python tools for their projects.

Install Python 3.12 on Ubuntu 22.04 or 20.04 via APT PPA

Step 1: Update Ubuntu Before Python 3.12 Installation

Before delving into 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

Step 2: Import Python 3.12 PPA on Ubuntu

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

Step 3: 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

Step 4: Install Python 3.12 on Ubuntu via APT Command

With the Python 3.12 PPA now part of your system’s repositories, you can proceed with the installation of Python 3.12. Execute the following command to install Python:

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 about setting default Python.

Switch Default Python Versions with Python on Ubuntu 22.04 or 20.04

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.

Step 1: Create Symbolic Links for Each Python Version on Ubuntu

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

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.

Step 2: List Available Python Versions on Ubuntu

Once you’ve established your symbolic links, the next step involves listing the Python versions to verify their installation and observing 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.

Step 3: Change Python 3.12 Version to Alternative with Ubuntu

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.

Screenshot showing Python 3.12 successfully switched to Python 3.11 on Ubuntu 22.04 or 20.04.Pin
Confirmation of Python 3.12 being changed to Python 3.11 on Ubuntu 22.04 or 20.04.

Install PIP with Python 3.12 on Ubuntu 22.04 or 20.04

As part of managing multiple Python versions on your system, it’s also essential to equip each version with Python’s package installer, PIP. This guide will particularly focus on Python 3.12 and detail how to install and upgrade PIP for this version.

Step 1: Install PIP for Python 3.12 on Ubuntu

Installing PIP for Python 3.12 is straightforward, especially if you’re using the Python PPA repository. The Advanced Packaging Tool (APT) command is a reliable method for this task.

Here’s the command you should execute:

sudo apt install python3-pip

This command will install PIP for Python 3.12.

Step 2: Upgrade PIP on Ubuntu

After successful installation, it’s advisable to upgrade PIP to ensure that you have the latest features, improvements, and security fixes. To do this, you will utilize Python’s -m flag, which allows Python to run library modules as scripts.

Run the following command:

python -m pip install --upgrade pip

This command initiates the upgrade process for PIP. The typical output should resemble the following:

joshua@ubuntu-linux:~$ python -m pip install --upgrade pip
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: pip in ./.local/lib/python3.12/site-packages (22.3.1)

The output shows that we have successfully upgraded PIP.

Step 3: Verifying the PIP Version on Ubuntu

Finally, it’s a good practice to confirm the installed PIP version. This step ensures that the installation and upgrade process is successful.

Execute the following command:

pip --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.

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

Leave a Comment

Your Mastodon Instance
Share to...