Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

Install Python 3 on Ubuntu

Install Python 3 from the Ubuntu repository, from the Deadsnakes PPA, or compile it from source.

VPS and Cloud Servers2 min read15 screenshots
Python 3 on Ubuntu

Requirements

  • Ubuntu 18.04 or newer.
  • sudo or root access.
  • An internet connection.

Check if Python 3 is installed

Ubuntu usually comes with Python 3.

python3 --version
Checking the Python version
which python3
The Python path

Method 1: APT

Step 1: Update

sudo apt update && sudo apt upgrade -y

Step 2: Install Python 3

sudo apt install python3
Installing Python 3

Step 3: Check it

python3 --version
The installed Python version

Also install pip and the venv module:

sudo apt install python3-pip python3-venv

Method 2: The Deadsnakes PPA (newer versions)

Step 1: Update

sudo apt update && sudo apt upgrade -y

Step 2: Install the tools

sudo apt install software-properties-common
Installing software-properties-common

Step 3: Add the Deadsnakes PPA

sudo add-apt-repository ppa:deadsnakes/ppa
Adding the Deadsnakes PPA
sudo apt update

Step 4: Install the version you want (for example Python 3.12)

sudo apt install python3.12 python3.12-venv
Installing Python 3.12

Check it with python3.12 --version. The new version is installed beside the system Python. Do not replace the system python3, because Ubuntu tools depend on it.

Method 3: Compile from source

Step 1: Install the build tools

sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev libsqlite3-dev wget libbz2-dev
Installing the dependencies

Step 2: Download the source from python.org/ftp/python/, for example:

wget https://www.python.org/ftp/python/3.12.4/Python-3.12.4.tgz
Copying the download link
Downloading the source

Step 3: Extract it

tar -xf Python-3.12.4.tgz
cd Python-3.12.4
Extracting the archive

Step 4: Configure and build

./configure --enable-optimizations
make -j$(nproc)
Preparing the build

Step 5: Install without replacing the system Python

sudo make altinstall
Installing Python

Step 6: Check it

python3.12 --version
Confirming the Python version

Quick recap

  • sudo apt install python3 is enough for most people.
  • Use the Deadsnakes PPA for newer versions.
  • Use altinstall when you compile from source.