How To Install Python On Linux

How To Install Python On Linux

Reading time1 min
#Python#Linux#Development#Python3#Pyenv

Mastering Python Installation on Linux: A Step-by-Step Guide for Developers

Software teams running workloads on Linux often assume python3 is universally available and up-to-date. Reality: distribution defaults lag, and unwary upgrades can break system tooling. System Python is typically coupled to critical OS scripts—disrupting it routinely disables Ansible, cloud-init, or even package management.

Quick reference: Distribution, Manager, Python 3 Package

DistributionPackage ManagerPython 3 Package
Ubuntu/Debianaptpython3
Fedoradnfpython3
CentOS/RHELyum or dnfpython3
Arch Linuxpacmanpython
OpenSUSEzypperpython3

First task: Confirm what you’re working with.

cat /etc/os-release

Why bother? Some flavors (notably CentOS <8) still default to Python 2.x, and python may point to the wrong interpreter. Always test:

python3 --version
python --version          # On some systems, this is still Python 2.x.

Avoiding System Python Entanglement

Altering system Python (/usr/bin/python*) is shortsighted; native tools and cloud agents often expect a specific version. Mess with that and you’ll see failures like:

/usr/bin/env: ‘python’: No such file or directory
Traceback (most recent call last):
  File "/usr/bin/yum", line 30, in <module>
    import yum
ModuleNotFoundError: No module named 'yum'

Proper Upgrade Paths

Prefer layered strategy:

  1. Use distro repo for OS tools.
  2. Use pyenv or containers for application workloads.
  3. Build from source only if unavoidable.

1. Install System Python for the Host

Install base Python:

Ubuntu/Debian:

sudo apt update
sudo apt install python3 python3-pip python3-venv

Fedora:

sudo dnf install python3 python3-pip python3-virtualenv

Arch:

sudo pacman -Syu python python-pip

Check what’s available. If you need Python 3.11+ on Ubuntu 20.04 (ships with 3.8), the official repos aren’t enough. Adding DeadSnakes PPA works:

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

Known issue: Some third-party PPAs lag during rapid Ubuntu LTS updates—rare, but real.

2. Install Multiple Versions with Pyenv

Trying to work on Django 3.2 and FastAPI at the same time? Use pyenv.

Pre-requisites—for Ubuntu:

sudo apt install -y make build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm \
libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python-openssl git

Install pyenv:

curl https://pyenv.run | bash

Shell init: Add to .bashrc (or .zshrc).

export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

Then:

pyenv install 3.11.3
pyenv install 3.8.19
pyenv global 3.11.3   # sets the default for your shell session

Practical example:
You need Python 3.8 for legacy TensorFlow, but want 3.11 for new projects. Use pyenv shell 3.8.19 to change for one-off use.

Side note: pyenv builds are serial; on low-RAM containers, expect compilation to stall/fail. Using MAKE_OPTS="-j2" sometimes helps, but be wary of OOM killer on cheap VM instances.

3. Isolate with Virtual Environments

Regardless of how you install Python, always use virtual environments unless you’re writing sysadmin glue. With venv:

python3.11 -m venv ~/projects/fooenv
source ~/projects/fooenv/bin/activate
pip install --upgrade pip setuptools

When active:

$ which python
/home/user/projects/fooenv/bin/python

Gotcha: Forgetting to activate leads to pip leaking packages globally. Chaos ensues later.

4. Verifying Your Stack

  • Interpreter:
    python3.11 --version
    
  • Pip and venv:
    which pip
    pip --version
    
  • Install and test a package:
    pip install requests
    python -c "import requests; print(requests.__version__)"
    
    If this doesn’t print (e.g., 2.31.0), your environment isn’t active.

5. Building Python From Source (Rarely Needed)

Sometimes, neither repos nor pyenv deliver what you need (custom debug builds, interpreter flags, patch testing).
Sample for Ubuntu:

sudo apt install build-essential libssl-dev zlib1g-dev \
libncurses5-dev libncursesw5-dev libreadline-dev libsqlite3-dev \
libgdbm-dev libdb5.3-dev libbz2-dev libexpat1-dev liblzma-dev \
tk-dev libffi-dev uuid-dev wget
cd /usr/src
sudo wget https://www.python.org/ftp/python/3.11.3/Python-3.11.3.tgz
sudo tar xzf Python-3.11.3.tgz
cd Python-3.11.3
sudo ./configure --enable-optimizations
sudo make -j4 altinstall

Note: altinstall leaves the original system python untouched, installing as e.g. python3.11.

Non-obvious troubleshooting tip

Install fails at compilation? Sometimes the build logs show:

expat.h: No such file or directory

That points to a missing dependency (libexpat1-dev on Ubuntu).


Summary—What Not To Touch, and Why

  • Never replace /usr/bin/python3 with a symlink to a manually built interpreter.
  • Always isolate project dependencies with venv or pyenv.
  • Use system Python only for OS-critical scripts, never for application deployment.

This process supports reproducible Python dev environments—vital for pipelines, CI, and onboarding. Alternative solutions exist (Docker, conda), but for most engineers managing multiple Linux servers or developer VMs, pyenv and venv suffice.

If nothing else, remember: don’t clobber the system Python, and always test the active interpreter using which python.


For further reading:

(No generic Outro)

If package versions or commands above change (merge window, deprecation), always check with apt show, dnf info, or the official documentation for updated practices.