
September 11, 2026
13 min read
By Kokil Thapa | Last reviewed: September 2026
You need to install Python on Ubuntu before you can run automation scripts, data tools, or AI integrations on a server. Ubuntu ships Python 3 in its repositories, but the default version may not match your project. On production Ubuntu 22.04 and 24.04 LTS boxes I maintain alongside PHP and Laravel stacks, Python often arrives as a dependency for deployment tools, monitoring agents, or client automation—not as the main application runtime. This guide walks through apt, the deadsnakes PPA, and pyenv so you pick the right path and avoid breaking system tools that rely on the distro Python.
sudo apt update && sudo apt install python3 python3-venv python3-pip for the distro version. Use the deadsnakes PPA or pyenv when you need a specific release side by side without replacing /usr/bin/python3.What is the best way to install Python on Ubuntu?
There is no single winner. The best method depends on whether Python is a system dependency or your primary runtime. Ubuntu expects /usr/bin/python3 to stay stable. Replacing it with a custom build can break apt, cloud-init, and unattended upgrades.
For most server work—cron scripts, small utilities, CI runners—use the repository package. For application development that pins a minor version, use deadsnakes or pyenv. For teams juggling many projects, pyenv plus per-project virtual environments is the cleanest long-term setup.
| Method | Best for | Pros | Cons |
|---|---|---|---|
apt install python3 | Server scripts, apt-managed tools | Fast, security updates via Ubuntu | Version tied to release; may lag upstream |
| deadsnakes PPA | One extra 3.x on LTS without pyenv | Clean packages, easy uninstall | PPA trust; not every micro-release |
| pyenv | Developers, many projects, CI-like parity | Any CPython version per directory | Build deps; compile time on first install |
| Source compile | Custom builds, embedded systems | Full control over flags | Manual patching; you own upgrades |
I treat Python on Ubuntu servers the same way I treat PHP version management: keep the system interpreter alone, isolate application dependencies, and document which binary each cron job uses. That pattern matches how I run mixed stacks described in our Linux system administration work and on booking platforms like Adventure Third Pole Trek where Python utilities sit next to Laravel services.
How do you install Python 3 from apt on Ubuntu?
The fastest path to install Python on Ubuntu is the default repository. Ubuntu 24.04 LTS ships Python 3.12; Ubuntu 22.04 LTS ships Python 3.10. Both are fine for general scripting if your libraries support them.
Step 1: Update package indexes
Refresh indexes before installing anything. Stale metadata causes confusing "package not found" errors.
sudo apt update
sudo apt upgrade -y See what apt update actually does if you want the mechanics behind that command.
Step 2: Install Python and tooling
sudo apt install -y python3 python3-venv python3-pip python3-dev The python3-dev package headers are required when pip builds wheels from source—common with older libraries on fresh servers.
Step 3: Verify the install
python3 --version
which python3
pip3 --version Expected output on Ubuntu 24.04 looks like Python 3.12.x. On 22.04 you should see Python 3.10.x.
Step 4: Install the venv package explicitly on minimal images
Ubuntu Server minimal installs sometimes omit python3-venv. Without it, python3 -m venv fails with a clear but annoying error about ensurepip.
sudo apt install -y python3-venv
python3 -m venv ~/venvs/demo
source ~/venvs/demo/bin/activate
python --version Never run sudo pip install into the system site-packages. That breaks apt-managed Python modules and creates undeclared dependencies. Use a virtual environment for every project, the same way you would isolate Node versions after you install Node.js on Ubuntu.
How do you install multiple Python versions on Ubuntu?
When your app requires Python 3.12 but the server runs 22.04 with 3.10, add a parallel interpreter. Two reliable options exist: the deadsnakes PPA and pyenv.
Option A: deadsnakes PPA (recommended for one extra version)
The deadsnakes PPA publishes newer CPython builds for supported Ubuntu LTS releases. Install Python 3.12 on Ubuntu 22.04 like this:
sudo apt install -y software-properties-common
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install -y python3.12 python3.12-venv python3.12-dev
python3.12 --version
python3.12 -m venv ~/venvs/myapp-312
source ~/venvs/myapp-312/bin/activate Binaries land as python3.12, not as a replacement for python3. System tools keep using the distro default. That is exactly what you want on a web server also running Apache, PHP-FPM, and MySQL—you do not want apt pulling packages against the wrong interpreter.
Manage PPA trust carefully. Only add PPAs you expect to maintain. Document them in your server runbook alongside other repos from our Ubuntu repository management guide.
Option B: pyenv (recommended for developers and many versions)
pyenv installs CPython under your home directory and switches versions per shell or per project directory.
- Install build dependencies:
sudo apt install -y build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev curl \
libncursesw5-dev xz-utils tk-dev libxml2-dev \
libxmlsec1-dev libffi-dev liblzma-dev - Install pyenv via the official installer script from the pyenv GitHub repository.
- Add pyenv to your shell startup file.
- Install and select a version.
curl https://pyenv.run | bash
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
source ~/.bashrc
pyenv install 3.12.4
pyenv global 3.12.4
python --version For per-project pinning, run pyenv local 3.12.4 inside the repo root. That writes a .python-version file teammates and CI can read.
On shared production servers I prefer deadsnakes over pyenv. Package names are explicit in Ansible or shell scripts. pyenv shines on developer laptops and build agents where you mirror production loosely.
How do you set up pip and a virtual environment on Ubuntu?
Installing Python is half the job. Isolating dependencies with venv is what keeps servers predictable.
Create and activate a venv
mkdir -p ~/projects/my-api && cd ~/projects/my-api
python3.12 -m venv .venv
source .venv/bin/activate
pip install --upgrade pip
pip install requests fastapi uvicorn Your prompt should show (.venv). While active, python and pip point inside the project—not at system paths.
Freeze dependencies for deployment
pip freeze > requirements.txt On the server or in CI, recreate the environment:
python3.12 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt Pair this with a systemd unit that calls the venv binary directly:
ExecStart=/home/deploy/my-api/.venv/bin/uvicorn main:app --host 127.0.0.1 --port 8001 That avoids activation scripts in service definitions. It is the same pattern I use when documenting cron paths—always use absolute paths to the interpreter, as covered in our Ubuntu cron jobs guide.
If you exchange JSON between a Python microservice and a Laravel API, validate payloads during development with the site JSON formatter tool before wiring production webhooks.
How do you install Python for Docker and CI on Ubuntu?
Containers shift the question. On the host you may only need Python for docker-compose helpers. Inside images you pick a base and install during build.
Host Ubuntu: minimal Python for tooling
sudo apt install -y python3 python3-pip
pip3 install --user docker-compose Prefer the Compose V2 plugin via apt install docker-compose-v2 when available. It removes the separate Python dependency entirely.
Inside Dockerfile: use official slim images
FROM python:3.12-slim-bookworm
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["gunicorn", "app:application", "-b", "0.0.0.0:8000"] Build on Ubuntu CI runners with Docker installed per our Docker on Ubuntu guide. Pin the image tag—python:3.12-slim today, not python:latest six months from now.
For GitLab CI on Ubuntu runners, cache the venv or use multi-stage builds. Commit a .python-version or document the deadsnakes package name in your deploy README so the next engineer knows which binary cron and systemd expect.
How do you troubleshoot common Python installation problems on Ubuntu?
Most failures fall into a short list. Fix them in this order before reinstalling everything.
python3: command not found— Python is not installed or PATH is wrong. Runsudo apt install python3and checkecho $PATH.externally-managed-environmenton pip install — PEP 668 on Ubuntu 23.04+ blocks system-wide pip. Use a venv orpip install --userfor ad-hoc tools.ensurepip is not available— Installpython3-venvorpython3.12-venvfor your target version.- Wrong interpreter in cron — Cron uses a minimal PATH. Call
/full/path/to/.venv/bin/pythonexplicitly. ModuleNotFoundErrorin systemd service — Service runs system Python instead of venv. FixExecStartto the venv binary.- SSL errors during pip — Clock skew or missing CA certs. Run
sudo apt install ca-certificatesand verify NTP.
Fix the externally-managed-environment error
Ubuntu marks the system Python as externally managed to protect apt. The correct fix is a venv:
python3 -m venv ~/venvs/tools
source ~/venvs/tools/bin/activate
pip install httpx Do not delete /usr/lib/python3.*/EXTERNALLY-MANAGED on production servers. You will regret it at the next dist-upgrade.
Register alternatives when multiple python3 binaries exist
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.12 2
sudo update-alternatives --config python3 Use alternatives only when you understand the blast radius. On web servers I leave python3 pointing at the distro default and call python3.12 explicitly in app configs.
When Python shares a box with MySQL, Nginx, and PHP-FPM, document every interpreter path in your runbook. Mixed stacks fail silently when a backup script upgrades its venv but cron still calls yesterday's binary. Server setup checklists in our Ubuntu server setup guide and web server hardening article include the same "absolute paths everywhere" rule.
How do you keep Python updated and secure on Ubuntu?
Security patches for distro Python arrive through unattended-upgrades or your normal apt maintenance window. Track CVE notices for CPython via the official Python documentation and Ubuntu security announcements.
sudo apt update
sudo apt install --only-upgrade python3 python3-minimal
python3 --version For deadsnakes packages, run apt upgrades on the same schedule as the rest of the server. For pyenv-managed interpreters, run pyenv install for patched micro releases and update .python-version files in repos.
Apply the same discipline as other services: restrict outbound network if the app does not need it, run services as unprivileged users, and keep SSH hardened per our Ubuntu security hardening guide. If Python automation touches client infrastructure, fold upgrades into a support and maintenance retainer so venv rebuilds and dependency audits happen on a calendar—not after an incident.
On Nepali business servers where uptime windows are narrow around Dashain and Tihar, schedule Python package updates during low-traffic hours. Test pip install -r requirements.txt in staging first. Breaking changes in transitive dependencies cause more downtime than interpreter patch bumps.
Key Takeaways
- Install Python on Ubuntu with
apt install python3 python3-venv python3-pipfor scripts and system tools—leave/usr/bin/python3alone. - Use deadsnakes or pyenv when you need a pinned minor version; call
python3.12explicitly instead of replacing the default. - Always create a virtual environment before
pip install; never usesudo pipon production servers. - Point systemd and cron at full venv binary paths to avoid
ModuleNotFoundErrorsurprises. - Treat PEP 668 "externally-managed-environment" as a feature—fix it with venv, not by deleting protection files.
- Document interpreter paths in deploy runbooks alongside other stack versions from PHP and MySQL installs on the same host.
People Also Ask
Does Ubuntu come with Python pre-installed?
Yes. Ubuntu desktop and server images include Python 3 because system tools depend on it. Run python3 --version to see the default. Minimal cloud images may omit python3-pip and python3-venv, so install those packages explicitly before building environments.
Should I use python or python3 on Ubuntu?
Always use python3 in scripts and documentation. Ubuntu does not guarantee a python command points to Python 3 unless you create one inside a venv. Inside an activated venv, python is safe because it refers to the isolated interpreter.
Is the deadsnakes PPA safe for production servers?
It is widely used on Ubuntu LTS for extra CPython versions and keeps packages separate from the system default. Treat it like any third-party repository: pin versions, test upgrades in staging, and prefer venvs for application dependencies rather than system-wide pip installs.
What Python version should I use on Ubuntu in 2026?
For new projects, target Python 3.12 or newer if your libraries support it. Ubuntu 24.04 ships 3.12; 22.04 ships 3.10 with 3.12 available via deadsnakes. Check your framework's support matrix before upgrading production interpreters.
Next steps after you install Python on Ubuntu
You now have a working interpreter, an isolated venv workflow, and a clear rule: system Python stays clean, project Python lives in .venv. That is the same discipline I apply when provisioning Ubuntu boxes for Laravel, WordPress, and automation sidecars—boring, repeatable, easy for the next developer to inherit.
If you are standing up a mixed stack and want someone to handle Python utilities, PHP apps, cron, and hardening on one server, review the AI integration and automation service or browse the portfolio for examples of production systems shipped end to end. For hands-on help with Ubuntu server setup, environment variables, or shell automation, read the environment variables guide and shell scripting tutorial next—or contact us to talk through your server layout.
Frequently Asked Questions
0 Comments
Leave a comment
Your email is not published. Comments appear once they have been read. Sign in to have your details filled in.

