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

Install and Manage Python on Ubuntu

Install Python with APT, the Deadsnakes PPA or from source, then use virtual environments and write a first program.

Check what you have

python3 --version

Ubuntu includes Python 3. There is no python command in a clean install of Ubuntu 20.04 or newer. Install python-is-python3 if you want python to run Python 3.

Install Python 3

With APT

sudo apt update
sudo apt install python3

With the Deadsnakes PPA (for newer versions)

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install python3.11

From source

Download the source code from python.org, extract it, and in the folder run:

./configure --enable-optimizations
make -j$(nproc)
sudo make altinstall

Use altinstall, so the system Python stays untouched.

Verify

python3 --version
python3.11 --version

Manage Python with virtual environments

A virtual environment keeps the packages of one project apart from others.

sudo apt install python3-venv
python3 -m venv my_venv
source my_venv/bin/activate

The prompt starts with (my_venv). Install packages with pip. Leave the environment with:

deactivate

Write a first program

Create hello.py:

print("Hello, World!")

Run it:

python3 hello.py

More configuration

Install pip

sudo apt install python3-pip

**Choose which Python python3 points to** (be careful: Ubuntu tools expect the system version, so only do this if you know why):

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 1

Make an alias for your user only. Add this line to ~/.bashrc:

alias python=python3

Then run source ~/.bashrc.

Quick recap

  • sudo apt install python3 is the easy way.
  • Use the Deadsnakes PPA or altinstall for newer versions.
  • Use virtual environments for projects.