How to Install Python on Ubuntu

In this tutorial, we’re going to show you how to install the latest version of Python on Ubuntu. Step-by-step, beginner-friendly instructions.

Alternative tutorial: How to install Python on CentOS.

Prerequisites

This is what you’ll need for this Python on Ubuntu tutorial:

  • Root user or a user with sudo privileges
  • SSH access to the server (or just open Terminal if you’re on a desktop)
  • An Ubuntu system. We’re using an Ubuntu 20.04 server from Linode, but these instructions will work on any other Ubuntu distro. You can get a Linux VPS from any provider.

Before following the tutorial, you should check if Python is already installed on your system. You can do so by running the following command:

python3 -V

If it’s already installed, you will get an output similar to this:

Python 3.8.10

In which case, you don’t need to install it, so don’t follow this tutorial. If you prefer a newer version or if you don’t have Python installed, follow the tutorial.

On Ubuntu 20.04, the default Python version is 3.8. On Ubuntu 22.04, it’s 3.10.

You can still follow the tutorial below to install Python 3.10 or 3.11 on any Ubuntu. The instructions are similar for other versions too.

Recommended read: Best Linux Distro for Programming

Step 1: Update Ubuntu

The first step, as always, is to update the system. Run the following commands:

apt-get update
apt-get upgrade

Step 2: Add deadsnake’s repo

You can use the deadsnake repository to get a newer release of Ubuntu, aside from the ones that are in Ubuntu’s default repositories.

First, run the following command:

apt-get install software-properties-common

Then, add the deadsnake PPA:

add-apt-repository ppa:deadsnakes/ppa

Update your repos again:

apt-get update

Step 3: Install Python

Now that you’ve added the PPA, you can install any newer version of Python.

To install Python 3.10, run the following command:

apt-get install python3.10

To install Python 3.11, run the following command:

apt-get install python3.11

You can also install and use multiple versions of Python on the same system.

Now, we can verify if they were installed.

For 3.10, run the following command:

python3.10 -V

Which should give you a similar output to:

Python 3.10.2

For 3.11, run the following command:

python3.11 -V

And you should get an output similar to:

Python 3.11.0a4

Note: as of writing, Python 3.11 is still in development (alpha release)

Step 4: Change the default Python version (optional)

If you installed multiple versions of Python, follow these steps to change the default version you’re using with the python3 command.

First, create symbolic links for the Python versions:

update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 1
update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 2

Then, choose what version will be the default one:

update-alternatives --config python3

And that’s it. You can verify if you changed the default version by running

python3 -V

Leave a comment

Your email address will not be published. Required fields are marked *