Mastering the Command Line: Installing Python on Ubuntu Efficiently and Correctly
No GUI shortcuts here—production deployments and CI/CD runners rarely care about desktop environments. The only way to guarantee a clean, predictable Python installation on Ubuntu is via the command line. Here’s how experienced engineers do it.
Python on Ubuntu: The Situation
Out of the box, Ubuntu 20.04 ships with Python 3.8. Typically, system utilities rely on that version—altering or removing it can break critical tools (add-apt-repository
, lsb_release
). But projects often call for newer interpreters or isolated Python environments for package management, testing, or automation.
Step 1: Inspect the Current Interpreter
First, determine what’s on the system:
python3 --version
Expected output:
Python 3.8.10
If you see command not found
, your base install is missing Python. This is rare, but possible on server minimal images.
Many scripts still call python
directly. On Ubuntu, this often yields:
Command 'python' not found, did you mean:
command 'python3' from deb python3
Unless you explicitly map python
to python3
, this remains unresolved by design.
Step 2: Update System Package Index
Critical step—outdated apt metadata triggers dependency hell, e.g.:
E: Unable to locate package python3.11
Update with:
sudo apt update
Step 3: Install the Default Python 3 Package
Installs the distribution-maintained Python interpreter—safer for automation and expected by system utilities.
sudo apt install python3
Verifying the install:
python3 --version
For most modern Ubuntu LTS releases:
- Ubuntu 20.04 → Python 3.8.x
- Ubuntu 22.04 → Python 3.10.x
Distributions lag upstream, so don't expect the very latest release. Need something newer? See the section on custom versions.
Step 4: Map python
to python3
(if Absolutely Required)
Some legacy tooling and scripts default to python
, not python3
.
To address this without aliasing in ~/.bashrc:
sudo apt install python-is-python3
This installs a symlink at /usr/bin/python
→ /usr/bin/python3
.
Gotcha: Mapping python
globally can break older Python 2 scripts—rare on new projects, but be aware.
Step 5: Install pip3 and Supporting Tools
Most Python workflows depend on pip to manage dependencies. Ubuntu separates python3-pip
from the base interpreter:
sudo apt install python3-pip
Validate with:
pip3 --version
You’ll often see:
pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)
Note: Ubuntu’s pip can lag official pip releases. Consider python3 -m pip install --upgrade pip
within a virtualenv for bleeding-edge features.
Step 6: Install a Specific Python Version (e.g., 3.10 or 3.11)
For projects pinned to a specific interpreter (common in machine learning or API server stacks), Ubuntu's repositories may be behind. Enter the deadsnakes
PPA, a well-maintained source for newer Python builds:
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
Install a specific version:
sudo apt install python3.11 python3.11-venv python3.11-distutils
Check version:
python3.11 --version
Practical example:
Isolate multiple versions for CI pipelines. Use python3.11 -m venv .venv311
to test compatibility.
Side note:
System-level update-alternatives
can switch the global python3, but this practice isn’t recommended; it can disrupt expected behaviors in system scripts.
Cheat Sheet
Task | Command |
---|---|
Update APT | sudo apt update |
Install default Python3 | sudo apt install python3 |
Map python → python3 | sudo apt install python-is-python3 |
Add Deadsnakes PPA | sudo add-apt-repository ppa:deadsnakes/ppa |
Install Python 3.11 and tools | sudo apt install python3.11 python3.11-venv python3.11-distutils |
Install pip for Python3 | sudo apt install python3-pip |
Real-World Example: Clean Environment for Automation
Provision a fresh LXD container for an Ansible bootstrapping test:
lxc launch images:ubuntu/22.04 ansible-test
lxc exec ansible-test -- bash
sudo apt update
sudo apt install python3 python3-pip -y
python3 --version
# Ensure pip can install Ansible's dependencies
python3 -m pip install --user --upgrade pip
Outcome: Dependencies isolated, system Python untouched.
Pro Tips and Non-obvious Details
- Virtual Environments: Always use
python3 -m venv venvdir
to isolate dependencies. Never pip install to the system interpreter—breakage is subtle and cumulative. - Deadsnakes: While reliable, PPA-based interpreters aren’t officially supported by Canonical. Monitor for security updates independently.
- Source Builds: For ultimate control (e.g., compile-time options, linking OpenSSL manually), skip apt and build from python.org tarballs. Trade-off: you own patching and updates.
- Avoid Mixing pip and apt: Installing Python packages globally with pip can overwrite files managed by your distro, leading to broken packages on upgrade.
- Known Issue:
Some minimal cloud images omitpython-is-python3
but include Python itself—plan for deployment scripts accordingly.
Final Take
Efficient, reproducible Python installs on Ubuntu rely on a few well-chosen commands. Skipping any verification step might introduce version mismatches or dependency chaos—always check both interpreter and pip versions after installation. Alternative methods exist (conda, pyenv, dockerized builds), but APT-based workflows remain the lowest friction and easiest to automate for most server classes.
For deeper isolation (multi-project servers, secure builds), pair this workflow with venv and continuous security patching.
No perfect method, but this gets you 95% there—fast.