Linux, Web Hosting, and Everything Else in Between
Linux, Web Hosting, and Everything Else in Between

How to Install Python 3 on Ubuntu

How to Install Python on Ubuntu

In this tutorial, we’re going to show you how to install Python on Ubuntu. We’ll include step-by-step, beginner-friendly instructions for different versions of Python.

Requirements

This is what you’ll need to follow this tutorial:

  • An Ubuntu system (it can be your desktop or a server). This tutorial installs Python from the command line (terminal). If you need a server, you can get one from Vultr or Linode. If you want the hosting provider to install Python for you, then get a managed server from SolaDrive. This tutorial was written and tested for Ubuntu 20.04 but it will work fine on any other version of Ubuntu or Ubuntu-based distro.
  • A root/sudo user.
  • Basic knowledge of using the CLI in Ubuntu. We’ll include all the commands that you can copy and explain every step in detail, but a very basic knowledge of using the terminal/CLI is preferred.

Update Ubuntu

As always, the first step is to update your system. Run the following commands to do so:

sudo apt-get update
sudo apt-get upgrade

When prompted, in this step and others below, if you would like to continue with the installation, you need to enter “y”, as the prompt explains.

Check if Python is already installed

Since you’re looking for this tutorial, we’re assuming it’s not, but just in case, you can check if Python is installed (and what version you’re using) with the following command:

python3 -V

An example output of that command is:

Python 3.8.10

This means that’s the Python version you have installed on your Ubuntu.

If Python is not already installed (which in your case it probably isn’t), then you should get a result similar to this:

-bash: /usr/bin/python3: No such file or directory

If Python is already installed, it will probably be a 3.x version of Python because Python 2 has already reached its EOL (End Of Life).

If by any chance your system has Python 2.x installed, you can check so by running:

python -V

or

python2 -V

In which case you should install Python 3 and update.

You can check what versions of Python are available on your system with the following command:

ls /usr/bin/python*

Install Python on Ubuntu (default version)

This step installs the default Python 3 version that’s included in Ubuntu’s repositories. As of writing, it’s Python 3.8.10.

Run the following command:

sudo apt-get install python3

After the installation is done, verify that Python 3 is installed by running:

python3 -V

Install the latest version of Python on Ubuntu

If you want to use a newer version of Python other than the one that’s already included in Ubuntu’s repositories by default, run the following commands:

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:deadsnakes/ppa

The second command will prompt you to just hit Enter to continue, unlike the previous commands where you needed to enter “y”.

This will add a new repository to your system, so update the repositories list:

sudo apt-get update

Now you can install just about any version of Python, even older than 3.8.

As of writing, the latest version of Python is 3.10. To install it, run:

sudo apt-get install python3.10

To verify that it’s installed, run:

python3.10 -V

Which should return the exact version of Python 3.10 on your system:

Python 3.10.2

Change the default version of Python

It’s fine if you have multiple versions of Python on your system. You can use each version by adjusting your commands like

python3.9 <command>

or

python3.10 <command>

If you want to use Python 3.9 when running the command “python3”, you need to change the default version.

To do so, first, run the following commands to add both versions (or update the commands if you have more versions on your system):

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

You can now check if the default python3 version is 3.9 by running:

python3 -V

You can change the default version and priorities at any time by running the following command:

sudo update-alternatives --config python

That’s all.

How do I upgrade to a newer version of Python on Ubuntu?

The upgrade process is similar to the installation process. First, install a newer version of Python, and then change the default version or keep using both versions. The process is similar if you want to downgrade to a different version.

If you’d like to install Python packages, you need to use pip. Follow our tutorial here.

Leave a comment

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