How to Install to Install Python 3.12 on Ubuntu 22.04 | 20.04

Python 3.12, the latest iteration of the popular high-level programming language, brings a host of new features, improvements, and deprecations over its predecessor, Python 3.11. This release continues Python’s tradition of enhancing readability and simplifying programming syntax while also introducing new capabilities that further extend the language’s versatility.

New Grammar Features:

  • PEP 701: This proposal formalizes the syntax of f-strings, lifting some restrictions on their usage. Expression components inside f-strings can now be any valid Python expression, including backslashes, unicode escaped sequences, multi-line expressions, comments, and strings reusing the same quote as the containing f-string.
  • PEP 709: Comprehension inlining has been introduced, offering a more efficient way to handle comprehensions.

New Typing Features:

  • PEP 688: This PEP makes the buffer protocol accessible in Python, which can be beneficial for handling binary data.
  • PEP 695: Type Parameter Syntax has been introduced, providing more flexibility in specifying types.
  • PEP 692: This proposal allows for more precise **kwargs typing using TypedDict.
  • PEP 698: The introduction of an Override Decorator for Static Typing enhances the language’s static typing capabilities.

Important Deprecations, Removals, or Restrictions:

  • PEP 623: This PEP removes wstr from Unicode, which is part of the ongoing effort to remove legacy Unicode APIs.
  • PEP 632: The distutils package has been removed, encouraging the use of more modern packaging and distribution tools.

Improved Error Messages: In Python 3.12, the error messaging system has been significantly enhanced to offer more insightful feedback when issues arise. When a NameError is triggered, the interpreter now has the capability to suggest potential modules from the standard library. Moreover, when an ImportError exception is thrown due to a failed import statement, the system can now provide recommendations for the value of <name>, derived from the available names in the specified <module>. This enhancement aims to streamline the debugging process and improve the overall user experience.

Improved Modules: Various modules in the standard library have received improvements, including but not limited to array, asyncio, calendar, csv, inspect, pathlib, dis, fractions, itertools, json, math, os, os.path, pdb, random, shutil, sqlite3, statistics, threading, tkinter, tokenize, types, unicodedata, unittest, uuid, tempfile, typing, and sys.

Optimizations: Python 3.12 also includes various optimizations and changes to CPython bytecode.

Notable Changes in CPython API: Legacy Unicode APIs based on Py_UNICODE* representation have been removed. Argument parsing functions like PyArg_ParseTuple() no longer support Py_UNICODE* based format. The PyUnicode_FSDecoder() function no longer accepts bytes-like paths, like bytearray and memoryview types: only the exact bytes type is accepted for bytes strings. The interpreter’s error indicator is now always normalized. This means that PyErr_SetObject(), PyErr_SetString() and the other functions that set the error indicator now normalize the exception before storing it.

The guide will demonstrate how to install Python 3.12 on Ubuntu 22.04 Jammy Jellyfish or Ubuntu 20.04 Focal Fossa LTS Linux, but the same methods will work for short-term releases that have not reached end of life status.

Section 1: Install Python 3.12 with APT

Step 1: Updating Ubuntu System

Before we delve into the installation of Python 3.12, it’s crucial to ensure that your Ubuntu system is up-to-date. This step is particularly important as it helps avoid potential conflicts during the installation process, especially when dealing with packages like new kernels and graphic card drivers.

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: Importing Python 3.12 Repository

For Ubuntu users, the most straightforward approach to installing Python 3.12 is to import the Launchpad PPA maintained by the “deadsnakes” team. This repository consistently provides the latest updates for Python and any additional packages that might be required.

To ensure a smooth process, install the necessary packages with the following command. While these packages are likely already installed on your system, it’s a good practice to run the command to confirm:

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

If you’re interested in the latest upstream version of Python 3.12, which is still in active development, you can import the Python Nightly PPA from the same team. This step is optional and only recommended if you want the most recent updates:

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

Step 3: Refreshing the APT Index

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: Installing Python 3.12

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

Section 2: How to Switch Default Python Versions

As a user dealing with several Python versions, it’s beneficial to understand how to switch your default Python version to suit different tasks or requirements. This process can be easily managed by adjusting symbolic links and selecting your preferred version.

Step 1: Creating Symbolic Links for Each Python Version

To begin, symbolic links must be generated for each Python version separately. This involves assigning 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: Listing Available Python Versions

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

Example output:

Step 3: Changing the Default Python Version

To change the default Python version, you need to input the corresponding selection number of your preferred version. For instance, if you wanted to set Python 3.11 as your default, you would enter ‘6’ (according to the example commands provided above). Keep in mind that your selection number might differ based on the versions installed on your system and their associated option numbers.

Once this is done, a successful switch will display the following message:

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

This message indicates that Python 3.11 is now your default Python version. You can confirm this by re-running the command to list the Python versions (sudo update-alternatives --config python), where you’ll see Python 3.11 marked with the asterisk (*) as the default version.


Section 3: Install PIP with Python 3.12

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: Installing PIP for Python 3.12

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: Upgrading PIP

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)

This output indicates that PIP has been successfully upgraded.

Step 3: Verifying the PIP Version

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

Execute the following command:

pip --version

Final Thoughts on Installing Python 3.12 on Ubuntu Linux

In this article, we’ve dived deep into the workings of Python 3.12 on the Ubuntu Linux distribution, focusing on how to seamlessly transition between multiple Python versions and install PIP for Python 3.12. Understanding these techniques is a critical skill set for Python programmers who need to accommodate various project requirements. These clear steps and detailed explanations aim to help you navigate the complexities of managing multiple Python versions and confidently setting up PIP for optimal Python package management.

Additional Resources and Links

As a stepping stone for further exploration, here are some official sources that provide deeper insights into Python 3.12 and related topics:

  • Python 3.12 Documentation: This official Python documentation provides comprehensive information about Python 3.12. It covers the language reference, library reference, and various guides to enhance your Python programming skills.
  • Python 3.12 What’s New: This link takes you to a summary of the new features included in Python 3.12. It provides valuable insights into the latest improvements and changes in the language.
  • Ubuntu Wiki – Python: This official Ubuntu wiki provides extensive information about Python on Ubuntu Linux distribution, giving you a broader understanding of managing Python on this specific platform.

Share to...