How to Install Python 3 on Ubuntu 20.04

Select distribution:
Traducciones al Español
Estamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
Create a Linode account to try this guide with a $ credit.
This credit will be applied to any valid services used during your first  days.

Python is a popular programming language created in 2000, by Guido van Rossum. It’s useful for writing everything from small scripts to full-scale software. Python is also a commonly adopted programming language by people entering into the field of software development. A lot of its popularity is based on Python’s high level of abstraction. This abstraction makes writing and reading the code easier than other languages.

As of January 1, 2020, the official version of Python is Python 3. Python 2 is no longer a supported language. This guide walks you through installing the latest version of Python 3 on Ubuntu 20.04. If you are interested in porting your already existing Python 2 code to Python 3, please refer to the official documentation on how to do so.

Before You Begin

  1. This guide assumes that you have access to a server or workstation running Ubuntu 20.04 LTS. To provision a Linode running Ubuntu 20.04 LTS, follow our Getting Started guide.

  2. This guide uses sudo wherever possible. Complete the sections of our Setting Up and Securing a Compute Instance to create a standard user account, harden SSH access, and remove unnecessary network services.

  3. Update your system:

    sudo apt update && sudo apt upgrade
    

How to Install Python 3

On brand new Ubuntu 20.04 installations, Python 3 is installed by default. You can verify by typing:

python3 --version
Python 3.7.3

You can also launch the Python Interpreter. The Python Interpreter, sometimes referred to as the Python Shell or the Python Interactive Shell, is a tool that lets you interact with Python from the command line. Try it by typing python3 into the shell:

python3
Python 3.8.2 (default, Jul 16 2020, 14:00:26)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

The interpreter outputs the version number, the version of the C compiler that Python uses on Linux, and some initial commands to get started. The installed version of Python is 3.8.2.

In the interpreter you can write Python code in real time. Try it by typing the following print statement:

print('hello world')

The interpreter instantly returns the following output:

hello world

You can exit the interpreter by typing the exit command:

exit()

Additional Information

When writing this guide, Ubuntu 20.04 was the latest LTS version of Ubuntu. It was also the first version where the previous version of Python, Python 2, was not installed by default. You may run into compatibility issues when installing applications that still use Python 2. These incompatibility issues mention errors such as:

This Package depends on python; however:
  Package python is not installed.

This is because packages that depended on Python 2 labeled the Python 2 binary as python, and the Python 3 binary as python3.

On Ubuntu 20.04, the binary for Python 3 is located at /usr/bin/python3. In previous versions of Ubuntu, there was a symbolic link between /usr/bin/python and /usr/bin/python3. You can restore this symbolic link to help fix compatibility issues by installing the python-is-python3 package with the following command:

sudo apt install python-is-python3

Verify the installation worked by using the python command in the shell. This launches the interpreter:

Python 3.8.2 (default, Jul 16 2020, 14:00:26)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

This allows you to use Python with both the python and python3 commands. Most packages that rely on python to be installed in location /usr/bin/python should now be able to be installed and run without error.

This page was originally published on


Your Feedback Is Important

Let us know if this guide was helpful to you.


Join the conversation.
Read other comments or post your own below. Comments must be respectful, constructive, and relevant to the topic of the guide. Do not post external links or advertisements. Before posting, consider if your comment would be better addressed by contacting our Support team or asking on our Community Site.