How to Install Python 3.11 on Ubuntu 22.04 or 20.04

Python 3.11 constitutes a notable update to the Python programming language, introducing new features and improvements that bolster its versatility and performance. For developers aiming to install Python 3.11 on Ubuntu 22.04 (Jammy Jellyfish) or the older, stable release of Ubuntu 20.04 (Focal Fossa), it’s crucial first to understand the key features introduced with this release:

Key Features in Python 3.11

  • Structural Pattern Matching: Simplifies the process of matching complex data structures.
  • Parenthesized Context Managers: Introduces a more readable syntax for using context managers.
  • Improved Error Messages: Makes debugging easier with more informative and user-friendly error messages.
  • Runtime Audit Hooks: Provides a new API for monitoring and modifying Python program behavior.
  • Additional Standard Library Modules: Includes new modules like ‘zoneinfo’ for better time zone support and ‘HTTP core’ for high-level HTTP client functionality.

Python 3.11’s new features and improvements make it a compelling update for developers and system administrators. Our upcoming guide will walk you through the steps to install Python 3.11 on Ubuntu 22.04 or Ubuntu 20.04, ensuring you can take advantage of the latest Python offers.

Step 1: Update Ubuntu Before Python 3.11 Installation

We highly recommend running an update in your terminal before installing Python 3.11 to avoid potential conflicts during installation. Updating your terminal ensures that all packages are current using the following command:

sudo apt update

You can upgrade any outdated packages by using the following command.

sudo apt upgrade

Step 2: Import Python PPA on Ubuntu 22.04 or 20.04

Ubuntu distributions typically include Python by default but often lack options for updates with new releases. To install the latest version of Python 3.11, this guide will use a reputable and widely recognized third-party PPA.

First, import the Python repository with the most up-to-date stable releases.

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

Run an APT update before proceeding to ensure reflection of the newly imported PPA.

sudo apt update

Step 3: Install Python 3.11 on Ubuntu 22.04 or 20.04

Once you’ve successfully imported the Python 3.11 PPA, install Python 3.11 by executing the following command in your terminal:

sudo apt install python3.11

Use the following command to verify the installation and build version of Python 3.11.

python3.11 --version

If desired, you can also install the following additional extras for Python 3.11:

  • Debug module:
sudo apt install python3.11-dbg
  • Developer (dev) module:
sudo apt install python3.11-dev
  • VENV (virtual environment) module:
sudo apt install python3.11-venv
  • Distutils module:
sudo apt install python3.11-distutils
  • lib2to3 utility module:
sudo apt install python3.11-lib2to3
  • DBM.GNU module:
sudo apt install python3.11-gdbm
  • Tkinter module:
sudo apt install python3.11-tk

To install all the extras in one go, run the following command.

sudo apt install python3.11-full

Step 4: Install PIP with Python 3.11 on Ubuntu 22.04 or 20.04

Most users can easily install Python 3.11 from the Python PPA repository by running the following APT command:

sudo apt install python3-pip

Ensure Python Pip is installed. If issues arise, reinstall it manually by following these steps to download get-pip.py with the wget command:

wget https://bootstrap.pypa.io/get-pip.py

After downloading the file, the next step is to install it.

python3 get-pip.py

We recommend you check for upgrades after installing Pip to ensure you have the latest version. This will help you stay up-to-date with the latest features and improvements.

python3 -m pip install --upgrade pip

To verify the installed version of Pip 3.11, run the following command:

pip --version

With these commands, you can install and upgrade Pip for Python 3.11, enabling you to install additional Python packages and libraries.

Additional Command: Switch Default Python Versions on Ubuntu

If you have multiple versions of Python installed on your system and want to set a particular version as the default, you can follow these steps to switch between them.

Firstly, you need to add symbolic links for each Python version separately. To do this, run the following commands:

Here’s an example (you can customize this or copy it):

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
Screenshots showing how to configure alternative Python versions on Ubuntu 22.04 or 20.04 LTS.
Screenshots demonstrating the process of configuring alternative Python versions, such as Python 3.11, on Ubuntu 22.04 or 20.04 LTS.

Note that the number at the end of each command (i.e., 1, 2, 3, etc.) represents the priority of the version. A higher number means a higher priority. In this example, Python 3.12 has the highest priority (7), and Python 2.7 has the lowest priority (1).

To switch to a different version of Python, you can enter the corresponding selection number in the following command:

sudo update-alternatives --config python
Example of changing the default Python version from 3.12 to 3.11 on Ubuntu 22.04 or 20.04 LTS.
An example is changing the default Python version from 3.12 to 3.11 on Ubuntu 22.04 or 20.04 LTS.

For instance, if you want to set Python 3.11 as the default, you would enter the number 3. The output of the command should look like this:

If the command is successful, you will see the following output:

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

These steps will help you set Python 3.11 as the default version on your Ubuntu Linux system. By doing so, you will be able to switch between various versions of Python effortlessly.

Conclusion

To conclude, one can easily install Python 3.11 on Ubuntu 22.04 or 20.04 by utilizing a PPA repository and APT commands. Python 3.11 offers a range of new features, bug fixes, and improvements compared to its predecessors, making it an ideal choice for various use cases such as web development, data analysis, scientific computation, and artificial intelligence. By following the commands outlined in this guide, users can effortlessly set up and switch between different Python versions on their Ubuntu Linux systems, thus leveraging the latest features and enhancements of the Python programming language.