How to Install Python 3.10 on Ubuntu 24.04, 22.04 or 20.04

Python 3.10, though not the latest release, remains essential for many developers, especially those who need it for compatibility with specific software or ongoing projects. With Python 3.12 and the upcoming Python 3.13 on the horizon, Python 3.10 still offers a range of enhancements that make it a solid choice for Ubuntu users.

Below are some of the major key points when Python 3.10 was released:

  • Structural Pattern Matching: Simplifies matching complex data structures with concise and expressive syntax.
  • Parenthesized Context Managers: Allows multiple context managers within a single ‘with’ statement, enhancing code readability and compactness.
  • Precise Line Numbers in Tracebacks: Enhanced error reporting by displaying exact line numbers in tracebacks, aiding in quick issue identification and resolution.
  • New Syntax Additions: Introduces ‘match’ and ‘case’ statements and the ‘|’ operator for union types, contributing to streamlined and more readable code.
  • Performance Improvements: Faster function calls and attribute access facilitates a smoother and more efficient development experience.
  • Deprecated/Removed Features: Cleans up the language by removing outdated features like ‘collections.ChainMap.new_child’ and deprecating methods like ‘threading.Thread.isAlive()’.

These features make Python 3.10 a reliable option for those who need it for specific software requirements or ongoing projects. Now, let’s proceed with installing Python 3.10 on Ubuntu.

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

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

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

Finalize Python 3.10 Installation

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

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

  • 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 to work with the GNU dbm database library. The gdbm module provides a Python interface to the gdbm library, which stores and retrieves 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

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

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

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 (Optional Steps)

To enhance your Python 3.10 experience on Ubuntu, you must 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

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 for installing, upgrading, and uninstalling Python packages using Pip.

Install a PIP Package 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 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 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 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 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 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 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 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

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

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

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

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 straightforward and enhances your development experience. Following this guide, you’ve successfully installed and configured Python 3.10 and learned how to manage multiple Python versions. If you need this version for specific projects or software compatibility. Happy coding!

Leave a Comment