How to Install Python 3.10 on Ubuntu 22.04 or 20.04

Python 3.10 brings forth a suite of enhancements and new features, making it a noteworthy upgrade for developers working with this popular programming language. If you aim to install Python 3.10 on Ubuntu 22.04 Jammy Jellyfish or its older stable release of Ubuntu 20.04 Focal Fossa, understanding its key features is essential.

Key Features of Python 3.10:

  • Structural Pattern Matching: This innovative feature simplifies the process of matching complex data structures, providing a syntax that is both concise and expressive.
  • Parenthesized Context Managers: Python 3.10 allows multiple context managers within a single ‘with’ statement, enhancing code readability and compactness.
  • Precise Line Numbers in Tracebacks: Enhanced error reporting is achieved by displaying exact line numbers in tracebacks, aiding developers in quickly identifying and resolving issues.
  • New Syntax Additions: Introducing the ‘match’ and ‘case’ statements and the ‘|’ operator for union types contribute to streamlined and more readable code.
  • Performance Improvements: Expect faster function calls and attribute access with Python 3.10, facilitating a smoother and more efficient development experience.
  • Deprecated/Removed Features: Python 3.10 sees the removal and deprecation of specific features for a cleaner and more consistent language, such as the removal of ‘collections.ChainMap.new_child’ and the deprecation of the ‘threading.Thread.isAlive()’ method.

With these enhancements, Python 3.10 stands as a robust tool for developers, offering improved performance and innovative features that cater to the needs of modern programming. This guide will walk you through the steps to successfully install Python 3.10 on Ubuntu 22.04 Jammy Jellyfish or Ubuntu 20.04 Focal Fossa, preparing you for an optimized coding experience.

Import Python PPA on Ubuntu 22.04 or 20.04

In this section, we’ll guide you through importing a Python PPA (Personal Package Archive) to install the latest version of Python 3.10 on your Ubuntu system. PPAs are widely used by the Linux community to distribute software packages that may not be available in the official repositories or to provide newer versions of existing packages.

Update Ubuntu Linux Before Installing Python 3.10

To import the stable PPA for Python 3.10, open a terminal and enter the following command:

sudo apt update && sudo apt upgrade

Import Python PPA on Ubuntu

If you prefer to use the nightly PPA to access the latest development changes, enter the following command instead:

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

Refresh APT Sources List for Python PPA on Ubuntu

After importing the desired Python PPA, you’ll need to update your APT sources list to fetch information about the new packages available from the PPA. To do this, execute the following command:

sudo apt update

Install Python 3.10 on Ubuntu 22.04 or 20.04 via APT

After you’ve imported the Python PPA and updated your APT sources list, install Python 3.10 on your Ubuntu system. This section will guide you through the installation process and help you verify the installed version of Python 3.10. Additionally, we’ll discuss optional extras that can be installed to enhance your Python development experience.

Install Python 3.10 on Ubuntu via APT Command

To install Python 3.10, open a terminal and enter the following command:

sudo apt install python3.10

Verifying the Python 3.10 Installation on Ubuntu

After the installation is complete, you can verify the version and build of Python 3.10 by running the following command:

python3.10 --version

Optional Extras for Python 3.10 on Ubuntu

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

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

sudo apt install python3.10-full

For users new to Python who may not know what the packages above are, here is a summary:

  • python-tk: This package provides the Tkinter library for Python 3.10, which is used for creating graphical user interfaces (GUIs) in Python. Tkinter is a set of Python modules that provides a convenient way to create GUI applications.
  • python-dev: This package provides the Python development headers and libraries needed for building Python extensions and other Python-related development tasks. This package is required for building and installing Python modules that require access to the Python header files and libraries.
  • python-dbg: This package provides the debug symbols for Python 3.10. Debug symbols are used by debuggers to help developers identify and fix bugs in their code. Installing this package can help debug Python applications and is recommended for developers working on complex Python projects.
  • python-venv: This package provides the virtual environment management tools for Python 3.10. Virtual environments are isolated Python environments that allow you to install and use different versions of Python and Python packages without affecting other Python installations on your system.
  • python-gdbm: This package provides the gdbm module for Python 3.10, which is used for working with the GNU dbm database library. The gdbm module provides a Python interface to the gdbm library, which is used for storing and retrieving data in a key-value store.
  • python-distutils: This package provides the distutils library for Python 3.10, which is used for building and distributing Python packages. The distutils library provides a standard way of building and distributing Python packages, making it easier for developers to share their code with others.

Test Python 3.10 Shell on Ubuntu 22.04 or 20.04

After installing Python 3.10 on your Ubuntu system, ensuring everything works as expected is essential. This section will walk you through opening the Python 3.10 shell and exiting it when you’re done.

Opening the Python 3.10 Shell on Ubuntu

To open the Python 3.10 shell, enter the following command in your terminal:

python3.10

This will launch the interactive Python 3.10 shell, where you can start typing Python commands, test your code, or explore the new features of Python 3.10.

Exit the Python 3.10 Shell on Ubuntu

When you’ve finished working with the Python 3.10 shell, you can exit it by entering the following command:

exit()

Use the keyboard shortcut Ctrl+D to exit the shell.

Install Pip for Python 3.10 on Ubuntu 22.04 or 20.04 (Optional Steps)

To enhance your Python 3.10 experience on Ubuntu, it’s essential to have Pip (the Python Package Installer) installed. Pip allows you to install and manage additional Python packages and libraries easily. This section will guide you through installing and upgrading Pip for Python 3.10 on Ubuntu.

Installing Python Pip on Ubuntu via Python 3.10

Pip should already be installed for most users who have installed Python 3.10 using the Python PPA repository. However, if you need to install it manually, follow these steps:

First, run the following APT command to install Pip:

sudo apt install python3-pip

Downloading and Installing Pip Manually on Ubuntu via Python 3.10

If you encounter issues with the previous method, you can also download and install Pip manually:

Download the get-pip.py file using the wget command:

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

After downloading the file, run the following command to install Pip:

python3 get-pip.py

Upgrade Pip to the Latest Version on Ubuntu via Python 3.10

After installing Pip, it’s recommended to check for upgrades to ensure you have the latest version:

python3 -m pip install --upgrade pip

Verifying the Installed Version of Pip on Ubuntu via Python 3.10

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

pip --version

Install Python Packages with Pip on Ubuntu 22.04 or 20.04

Now that you have Pip installed and verified, you can install Python packages to enhance your Python 3.10 experience. This section will discuss the basic commands to install, upgrade, and uninstall Python packages using Pip.

Install a PIP Package on Ubuntu via Python 3.10

To install a Python package, use the following command:

pip install package_name

Replace package_name with the desired package name, such as numpy for the NumPy package.

Upgrade a PIP Package on Ubuntu via Python 3.10

To upgrade an installed Python package to the latest version, use the following command:

pip install --upgrade package_name

Replace package_name with the name of the package you want to upgrade.

Remove a PIP Package on Ubuntu via Python 3.10

If you no longer need a Python package, you can uninstall it using the following command:

pip uninstall package_name

Replace package_name with the name of the package you want to uninstall.

Create a Test Python Virtual Environment on Ubuntu via Python 3.10

Using virtual environments to isolate project-specific dependencies is a good practice when working with multiple Python projects. In this section, we’ll guide you through setting up a Python virtual environment using the venv module for Python 3.10.

Create a Virtual Environment on Ubuntu via Python 3.10

To create a new virtual environment, run the following command:

python3.10 -m venv my_venv

Replace my_venv with the desired name for your virtual environment.

Activate the Virtual Environment on Ubuntu via Python 3.10

After creating the virtual environment, you need to activate it before you can use it. To activate the virtual environment, run the following command:

source my_venv/bin/activate

Replace my_venv with the name of your virtual environment.

Deactivate the Virtual Environment on Ubuntu via Python 3.10

When you’re done working in the virtual environment, you can deactivate it using the following command:

deactivate

This command will return you to the global Python environment.

This section will cover additional commands and tips for managing Python 3.10 on your Ubuntu system, including updating, removing, and switching between the nightly and stable PPA repositories.

Update Python 3.10 on Ubuntu via APT

To update Python 3.10, follow these steps:

Update your APT package list:

sudo apt update

If an update for Python 3.10 is available, it will appear with the rest of your APT package upgrades. Proceed with the upgrade by running the following:

sudo apt upgrade

Remove Python 3.10 on Ubuntu

If you wish to remove Python 3.10 from your system, follow these steps:

Use the following command to remove Python 3.10 using the APT method:

sudo apt remove python3.10

Remove Python PPA on Ubuntu

Next, if you wish to remove the Python PPA that was imported, run the following command:

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

Switch Default Python Versions while working with Python 3.10 on Ubuntu 22.04 or 20.04

To specify a preferred Python version for users with multiple versions of Python installed on their system, you can follow these steps to change the default Python version.

First, you must create symbolic links for each Python version installed on your system. When creating the symbolic link, you should include the group name “python” followed by the version number.

Example (you can customize this or copy):

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

It is important to note that you do not need to have all versions listed up front. You can copy the command, and the symbolic links will be created for the versions of Python that you have installed.

To view the available Python versions, you can use the following command:

sudo update-alternatives --config python
Screenshots showcasing how to switch to Python 3.12 as an alternative version on Ubuntu 22.04 or 20.04.
Demonstration of how to set Python 3.10 as the default version of Python 3.12 on Ubuntu 22.04 or 20.04 Linux via listing –config options in the terminal.

As previously mentioned, the machine used in the tutorial has multiple versions of Python installed, including 3.12, 3.11, 3.9, 3.8, and 3.7. Currently, Python 3.12 is set as the default version, and the asterisk can identify (*) next to the version number.

In this example, you must enter the corresponding selection number to set a different version of Python as the default, such as Python 3.12. The selection number may vary based on the number of Python versions installed on your system.

To switch to Python 3.10, enter the number 2. Upon successful completion, you should expect to see the following output:

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

If you list the alternative options again, you will see that Python 3.10 has been set as the default version, indicated by the asterisk (*) symbol.

Conclusion

In conclusion, installing Python 3.10 on Ubuntu is a straightforward process that enhances your Python development experience. Following this guide’s steps, you can successfully install and configure Python 3.10 and manage multiple Python versions and related tools.

Leave a Comment


Your Mastodon Instance
Share to...