How to Upgrade to Python 3.13 on Ubuntu 20.04 and 22.04 LTS

Python 3.13 brings many new features, performance improvements, and bug fixes, making it a desirable upgrade for developers. However, upgrading Python on Ubuntu, especially on long-term support (LTS) versions like 20.04 and 22.04, requires careful steps to avoid breaking system dependencies. In this guide, we will walk you through the process of upgrading to Python 3.13 on Ubuntu 20.04 and 22.04 LTS. We’ll cover the installation process, managing multiple Python versions, and ensuring your system remains stable.

Why Upgrade to Python 3.13?

Before diving into the steps, let’s briefly discuss why you might want to upgrade to Python 3.13:

  1. Performance Improvements: Python 3.13 includes optimizations that make your code run faster.
  2. New Features: Enhanced syntax and new modules make it easier to write more efficient and readable code.
  3. Security Fixes: Staying up to date with Python versions ensures that your environment benefits from the latest security patches.
  4. Extended Support: As Python evolves, older versions gradually lose official support, so upgrading keeps you current.

Preparing Your System for the Upgrade

Upgrading the default Python version on Ubuntu requires careful preparation. Python is deeply integrated into Ubuntu, and upgrading recklessly can cause issues with system utilities that depend on Python. Here’s how to prepare:

  1. Backup Your System:
    Always start by creating a backup of your important files or a full system backup. You can use tools like Deja Dup, Timeshift, or rsync to create backups.
  2. Update Your Package List:
    Before installing any new software, ensure your package list is up to date by running:
sudo apt update && sudo apt upgrade
  1. Install Required Dependencies:
    Python requires several build tools and libraries for installation. Install them with:
sudo apt install -y build-essential libssl-dev zlib1g-dev libncurses5-dev libsqlite3-dev libreadline-dev libffi-dev libbz2-dev liblzma-dev

Step 1: Install Python 3.13 from Source

Since Python 3.13 is a newer version, it may not be available in Ubuntu’s official repositories immediately. The best way to install it is by compiling it from source.

  1. Download the Python 3.13 Source Code: Navigate to the /usr/src/ directory and download the Python 3.13 source code:
cd /usr/src/
sudo wget https://www.python.org/ftp/python/3.13.0/Python-3.13.0.tar.xz
  1. Extract the Source Code: Extract the downloaded tarball:
sudo tar -xf Python-3.13.0.tar.xz
cd Python-3.13.0
  1. Configure the Build: Configure the Python build with optimizations. This step ensures that Python 3.13 runs efficiently on your system:
sudo ./configure --enable-optimizations
  1. Compile and Install Python 3.13: Start the compilation process with the make command:
sudo make -j $(nproc)

Once the compilation is complete, install Python 3.13:

sudo make altinstall

The altinstall command is used to prevent overwriting the default Python version that Ubuntu relies on.

  1. Verify the Installation: After installation, verify that Python 3.13 is correctly installed by checking the version:
python3.13 --version

You should see output like:

Python 3.13.0

Step 2: Managing Multiple Python Versions

With Python 3.13 installed, you might have multiple Python versions on your system (e.g., Python 3.8 or 3.9). It’s essential to manage these versions carefully to avoid conflicts.

  1. Update Alternatives: Ubuntu uses the update-alternatives tool to manage multiple versions of the same software. You can use it to manage your Python versions:
sudo update-alternatives --install /usr/bin/python3 python3 /usr/local/bin/python3.13 1

The command above registers Python 3.13 as an alternative version, giving it a lower priority (1) so that it doesn’t replace the default system Python version.

  1. Switch Between Python Versions: If you need to switch between different Python versions, use the update-alternatives command:
sudo update-alternatives --config python3

This command will prompt you to select which Python version you want to use as the default.

  1. Using Virtual Environments: A best practice for working with multiple Python versions is to use virtual environments. With venv, you can create isolated environments that use Python 3.13:
python3.13 -m venv myenv
source myenv/bin/activate

This way, you can use Python 3.13 for specific projects without affecting the system-wide Python version.

Step 3: Test Your Python 3.13 Installation

After setting up Python 3.13, it’s crucial to test it by running some code or installing packages.

  1. Install pip for Python 3.13: Install pip, the Python package manager, if it wasn’t included by default:
python3.13 -m ensurepip --upgrade
  1. Test Installing a Package: Test your Python 3.13 installation by installing a package:
python3.13 -m pip install requests

Run a quick script to check everything is working:

import requests
response = requests.get("https://www.python.org")
print(response.status_code)

If you see 200, your installation was successful.

Step 4: Set Python 3.13 as the Default (Optional)

If you are confident that Python 3.13 is stable and compatible with your system, you might want to make it the default Python version.

  1. Create a Symlink (Caution): To set Python 3.13 as the default, you can create a symlink:
sudo ln -sf /usr/local/bin/python3.13 /usr/bin/python3

However, proceed with caution, as this could break system utilities that rely on the default Python version.

  1. Revert if Necessary: If you encounter issues, revert to the original Python version by restoring the symlink:
sudo ln -sf /usr/bin/python3.x /usr/bin/python3

Replace 3.x with your original Python version, such as 3.8 or 3.9.

Step 5: Keeping Python 3.13 Updated

To keep Python 3.13 up to date, regularly check for new releases and update by repeating the installation process.

  1. Check for Updates: Monitor the official Python website or subscribe to the Python mailing list for announcements about new versions or patches.
  2. Upgrade to the Latest Minor Version: When a new minor version of Python 3.13 is released (e.g., 3.13.1), download the source code and repeat the installation steps.
sudo wget https://www.python.org/ftp/python/3.13.x/Python-3.13.x.tar.xz

Replace x with the minor version number.

Conclusion

Upgrading to Python 3.13 on Ubuntu 20.04 and 22.04 LTS is a straightforward process if you follow these steps carefully. By compiling from source, you ensure that your system remains stable while taking advantage of the latest Python features and improvements. Whether you’re developing new projects or maintaining existing code, Python 3.13 offers the performance and security enhancements necessary for modern software development.

Remember to manage multiple Python versions carefully to avoid conflicts, and use virtual environments for project-specific dependencies. With Python 3.13 installed, you’re now equipped to take your development to the next level on Ubuntu LTS.

Leave a Comment