How to Install Python 3.10 on Ubuntu (24.04, 22.04, 20.04)

Python 3.10 introduces several new features, including structural pattern matching, precise error reporting, and more consistent formatting of tracebacks. While Ubuntu 24.04 may come pre-installed with a newer version of Python, some users may want to specifically install Python 3.10 for compatibility purposes or to maintain consistent development environments across systems. This guide provides a step-by-step process to install Python 3.10 on Ubuntu 24.04, 22.04, and 20.04.

Why Install Python 3.10 on Ubuntu?

Ubuntu’s default repositories may not always include the latest Python versions. Installing Python 3.10 allows developers to:

  • Ensure compatibility with applications or scripts requiring this exact version.
  • Test code on different Python versions for broader support.
  • Utilize new features introduced in the 3.10 release.

Step 1: Prepare Your System

Before installing Python 3.10, ensure your system is up to date. Open the terminal and run the following commands:

sudo apt update
sudo apt upgrade -y

Next, install the required dependencies:

sudo apt install -y software-properties-common

Step 2: Add the Deadsnakes PPA

Python 3.10 is not included in the official Ubuntu repositories for some versions. To install it, you’ll need to add the Deadsnakes PPA, a trusted source for newer Python versions:

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

Adding this repository ensures you can access Python 3.10 packages safely.

Step 3: Install Python 3.10

Once the PPA is added, install Python 3.10 using the following command:

sudo apt install -y python3.10

You can verify the installation by checking the version:

python3.10 --version

Step 4: Set Python 3.10 as the Default Version (Optional)

If you want to set Python 3.10 as the default when you type python3, you can use the update-alternatives tool.

First, add Python 3.10 as an alternative:

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 1

To configure the default version:

sudo update-alternatives --config python3

You will be prompted to select the version you’d like to use. Choose the line corresponding to Python 3.10.

Step 5: Verify Everything Works

Check that Python 3.10 is now the default version:

python3 --version

You can also run the interactive shell:

python3

And confirm Python 3.10 is displayed at the top of the shell window.

Installing pip for Python 3.10

To install pip, the Python package manager for Python 3.10, use the following command:

sudo apt install -y python3.10-venv python3.10-dev
curl -sS https://bootstrap.pypa.io/get-pip.py | sudo python3.10

Now, you can use pip for Python 3.10 using:

python3.10 -m pip --version

Troubleshooting Common Issues

1. get-pip.py not working: Ensure curl is installed (sudo apt install curl) or manually download the script before running.

2. Python3 still points to an older version: Double-check the configuration using update-alternatives and that Python 3.10 is correctly installed in /usr/bin.

3. Compilation errors when installing packages: Make sure you’ve installed python3.10-dev which provides necessary header files.

Conclusion

Installing Python 3.10 on Ubuntu (24.04, 22.04, or 20.04) is straightforward when using the Deadsnakes PPA. Whether you’re a developer needing a specific version for a project, or simply exploring new Python features, following these steps ensures a clean and dependable setup.

Remember to manage your multiple Python versions using virtual environments and always test applications thoroughly after switching interpreter versions.