How to Install Python 3.11 on Ubuntu 22.04 or 20.04

Python 3.11 represents a significant update to the Python programming language, offering new features and improvements that enhance its versatility and performance. Whether you’re running Ubuntu 22.04 Jammy Jellyfish or the older stable release of Ubuntu 20.04 Focal Fossa, installing Python 3.11 can provide you with the latest advancements in Python development.

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

Running an update in your terminal before installing Python 3.11 is highly recommended to avoid any potential conflicts during installation. This will ensure that all packages are up-to-date 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

By default, Python is available on Ubuntu distributions, but often, you do not have choices, and it is not updated when new releases come out. The guide will import a well-known third-party PPA that is widely used and respected so you can install the latest version of Python 3.11.

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

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

Before proceeding, run an APT update to ensure that the newly imported PPA is reflected.

sudo apt update

Step 3: Install Python 3.11 on Ubuntu 22.04 or 20.04

Now that the Python 3.11 PPA has been successfully imported, you can install Python 3.11 by running 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

For most users using the Python PPA repository, installing Python 3.11 can be accomplished simply by running the following APT command.

sudo apt install python3-pip

Pip should already be installed, but if you encounter issues and need to reinstall it manually, follow these steps to download get-pip.py using 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

After installation, checking for upgrades is recommended to ensure you have the latest version of Pip.

python3 -m pip install --upgrade pip

You can verify the version of Pip 3.11 that is installed by running 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.Pin
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.Pin
An example is showcasing how to change 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

This indicates that Python 3.11 has been set as the default version. By following these steps, you can easily switch between different versions of Python on your Ubuntu Linux system.

Conclusion

In conclusion, installing Python 3.11 on Ubuntu Linux is a straightforward process that can be achieved using a PPA repository and APT commands. Python 3.11 has numerous improvements, bug fixes, and new features compared to its predecessors. It is ideal for various use cases, including web development, data analysis, scientific computation, and artificial intelligence. With the help of the commands outlined in this guide, users can easily set up and switch between different versions of Python on their Ubuntu Linux systems, enabling them to take advantage of the latest features and improvements of the Python programming language.

Share to...