Ubuntu is an open-source Linux distribution that supports several programming languages such as Python, Java, C++, etc. Python is a multiple-purpose programming language with enriched features and can be installed easily on Ubuntu.

Quick Answer

Install Python on Ubuntu with sudo apt install python3. For a specific version, use the Deadsnakes PPA. To build from source, download the .tgz from python.org and compile with ./configure --enable-optimizations.

How to Install Python on Ubuntu?

Python can be installed on Ubuntu machines using the Official Python Repository, Deadsnakes PPA, and from the Source Code.

Method Key Command Version selection Notes
apt (default) sudo apt install python3 System default Fastest; no custom versions
Deadsnakes PPA sudo apt install python3.X Any version Add PPA first
Source code ./configure --enable-optimizations Any version Compile time required

Method 1: Install Python on Ubuntu From the Official Python Repository

To install Python from the official Python repository, follow the steps listed below:

Step 1: Update the Packages

It is recommended to update system packages before installing any new software/package. Use the command given below to update the system.

$ sudo apt update

It can be noticed that the system packages are updated successfully.

Terminal output of sudo apt update before Python installation on Ubuntu

Step 2: Install Python

Now use the following command to install Python on Ubuntu 22.04 LTS.

$ sudo apt install python3

In our scenario, it’s already installed.

Terminal output of sudo apt install python3 on Ubuntu

Step 3: Use Python

To verify Python installation, use the below-listed command:

$ python3
Terminal showing python3 interactive session starting on Ubuntu

It can be seen that Python3 is installed and working perfectly fine.

Step 4: Verify the Installation

Now check/verify the Python version installed on your Ubuntu machine using the command:

$ python3 --version
Terminal output of python3 --version showing Python 3.10.12 on Ubuntu

In our scenario, it’s 3.10.12

Step 5: Quit Python Session

To end the Python session, execute the following command:

$ quit()
Terminal showing quit() exiting the Python session on Ubuntu

Method 2: Using Deadsnakes PPA

Deadsnakes PPA (Personal Package Archives) allows you to install multiple Python packages on Ubuntu. It offers packages for various Python versions. The step-by-step procedure to install Python on Ubuntu using the Deadsnakes PPA is given below:

Step 1: Manage the Distributions

We first have to manage the distributions. It is required to manage your software packages from independent sources. Use the following command to do so:

$ sudo apt install software-properties-common
Terminal output of sudo apt install software-properties-common on Ubuntu

Step 2: Add Deadsnakes PPA

Now we have to add deadsnakes PPA to Ubuntu’s repository:

$ sudo add-apt-repository ppa:deadsnakes/ppa
Terminal output of sudo add-apt-repository ppa:deadsnakes/ppa on Ubuntu

Step 3: Update the System

Now update the system using the command given below:

$ sudo apt update
Terminal output of sudo apt update after adding Deadsnakes PPA on Ubuntu

Step 4: Install Python

Now use the command for Python installation.

$ sudo apt install python3
Terminal output of sudo apt install python3 showing already newest version on Ubuntu

In our scenario, it is already installed so the terminal displays “Python is already to the newest version“.

Method 3: Python Install Using the Source Code

To install Python in Ubuntu from the source code, follow the step-by-step procedure shown below:

Step 1: Update the System

First, update the packages using the update command given below:

$ sudo apt update
Terminal output of sudo apt update for Method 3 source build on Ubuntu

Step 2: Install Dependencies

Now to build Python in Ubuntu from the source code, we have to install the required dependencies, and to do so, execute the below command.

$ sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget
Terminal output of installing build-essential and Python dependencies on Ubuntu

Step 3: Create a Python Directory

Now create a new folder named Python and move within the folder. Use the following command to do so:

$ sudo mkdir /python && cd /python
Terminal output of sudo mkdir /python creating Python build directory on Ubuntu

Step 4: Download Python Using wget

Now download the latest version of Python with the help of the wget command as shown below:

$ sudo wget https://www.python.org/ftp/python/3.12.0/Python-3.12.0a1.tgz
Terminal output of wget downloading Python 3.12 source archive on Ubuntu

It will take some time to download the Python package.

Step 5: Extract the .tgz File

Now extract the downloaded file with the help of the tar command, and to do that, run the command:

$ sudo tar -xvf Python-3.12.0a1.tgz
Terminal output of sudo tar -xvf extracting Python source archive on Ubuntu

Now move within the extracted folder with the help of the command listed below:

$ cd Python-3.12.0a1
Terminal showing cd Python-3.12.0a1 entering the source directory on Ubuntu

Step 6: Turn on Optimizations

It is essential to turn on optimizations to build Python in Ubuntu, and this can be done by executing the command:

$ ./configure --enable-optimizations
Terminal output of ./configure --enable-optimizations building Python on Ubuntu

The Python is now built on Ubuntu.

Step 7: Install Python

Now for Python installation, use the command:

$ sudo apt install python3
Terminal output of sudo apt install python3 for final installation on Ubuntu

Check the version of Python by executing the command:

$ python3 --version
Terminal output of python3 -version showing installed version on Ubuntu

Related Linux guides