How to Install Python 3 on Linux: pip, venv, and pyenv

How to Install Python 3 on Linux: pip, venv, and pyenv

✅ Tested on Ubuntu 24.04, Debian 12, Fedora 40, Arch Linux — Last updated: June 2026

Installing Python 3 on Linux is the starting point for almost every developer, data scientist, and sysadmin working in the Linux ecosystem. Python 3 is already installed on most modern Linux distributions, but the default version is often outdated, missing pip, or not set up for virtual environments. This guide covers proper Python 3 installation, pip management, virtual environments, and pyenv for managing multiple Python versions.

Table of Contents

Check Your Current Python Installation

python3 --version
# Python 3.12.3

pip3 --version
# pip 24.0 from /usr/lib/python3/dist-packages/pip (python 3.12)

# Check all installed Python versions:
ls /usr/bin/python*
which python3

# On some distros, 'python' still points to Python 2 (or nothing):
python --version 2>/dev/null || echo "No 'python' command"

If python3 --version returns 3.10 or higher, you're in good shape for most use cases. If it's older, or if you need a specific version for a project, read on.

Install Python on Ubuntu / Debian / Linux Mint

Method 1: System Package Manager (Easiest)

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

# Verify:
python3 --version
pip3 --version

Method 2: deadsnakes PPA (Latest Python on Ubuntu)

Ubuntu's default repositories often have older Python versions. The deadsnakes PPA provides newer versions:

sudo add-apt-repository ppa:deadsnakes/ppa -y
sudo apt update

# Install the latest Python (check for newest version available):
sudo apt install python3.13 python3.13-venv python3.13-dev -y

# Use it explicitly:
python3.13 --version

# Optional: set as default python3:
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.12 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.13 2
sudo update-alternatives --config python3

Install Essential Development Packages

# Required for compiling Python packages with C extensions (NumPy, Pillow, etc.):
sudo apt install -y 
  python3-dev 
  build-essential 
  libssl-dev 
  libffi-dev 
  libxml2-dev 
  libxslt1-dev 
  zlib1g-dev

Install Python on Fedora

sudo dnf install python3 python3-pip python3-virtualenv -y

# Python development headers (for building packages):
sudo dnf install python3-devel -y

# Check installed version:
python3 --version

# On Fedora, multiple Python versions can coexist:
sudo dnf install python3.12 python3.13 -y

Install Python on Arch Linux

sudo pacman -S python python-pip -y

# Arch always ships the latest stable Python
python --version       # on Arch, 'python' = Python 3
pip --version

# Development headers:
sudo pacman -S base-devel -y

pip — Python Package Manager

pip is how you install Python packages from PyPI (Python Package Index). On modern systems, pip is tied to a specific Python version.

# Install a package:
pip3 install requests

# Install a specific version:
pip3 install requests==2.31.0

# Install from requirements file:
pip3 install -r requirements.txt

# Upgrade a package:
pip3 install --upgrade requests

# Upgrade pip itself:
pip3 install --upgrade pip

# List installed packages:
pip3 list

# Show package details:
pip3 show requests

# Search PyPI:
pip3 index versions requests    # see all available versions

# Uninstall:
pip3 uninstall requests

# Export installed packages to requirements file:
pip3 freeze > requirements.txt

⚠️ Important: On Ubuntu 23.04+, you'll see "error: externally-managed-environment" if you try to install packages system-wide with pip. This is intentional — install into virtual environments instead (see next section).

# If you absolutely need to install system-wide and know the risks:
pip3 install requests --break-system-packages  # Ubuntu 23.04+

Virtual Environments — The Right Way to Work

A virtual environment is an isolated Python installation for your project. Each project gets its own packages and Python version, with no conflicts between projects. You should always use virtual environments — never install project dependencies system-wide.

Creating and Using a venv

# Create a virtual environment:
python3 -m venv myenv

# Activate it (Linux/Mac):
source myenv/bin/activate

# Your prompt changes:
# (myenv) $

# Now install packages — they go ONLY into myenv:
pip install requests numpy pandas

# Check where Python is:
which python
# /home/jm/myproject/myenv/bin/python

# Deactivate (return to system Python):
deactivate

# Delete the environment:
rm -rf myenv

Best Practices for Virtual Environments

# Standard project layout:
mkdir myproject
cd myproject
python3 -m venv .venv          # hidden folder by convention
source .venv/bin/activate
pip install package1 package2
pip freeze > requirements.txt  # record dependencies

# Add to .gitignore:
echo ".venv/" >> .gitignore
echo "__pycache__/" >> .gitignore
echo "*.pyc" >> .gitignore

Useful: Auto-activate venv in a Directory

# Add to ~/.bashrc for automatic venv activation:
function cd() {
    builtin cd "$@"
    if [[ -f ".venv/bin/activate" ]]; then
        source .venv/bin/activate
        echo "✓ venv activated"
    fi
}
source ~/.bashrc

pyenv — Manage Multiple Python Versions

pyenv lets you install and switch between multiple Python versions without affecting your system Python. Essential when you work on projects that require specific Python versions.

Install pyenv

# Install dependencies (Ubuntu/Debian):
sudo apt install -y build-essential libssl-dev zlib1g-dev 
  libbz2-dev libreadline-dev libsqlite3-dev libncursesw5-dev 
  xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev

# Install pyenv:
curl https://pyenv.run | bash

# Add to ~/.bashrc:
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

Using pyenv

# List available Python versions:
pyenv install --list | grep "  3."

# Install specific version:
pyenv install 3.12.7
pyenv install 3.11.10
pyenv install 3.10.14

# List installed versions:
pyenv versions

# Set global default:
pyenv global 3.12.7

# Set local version for current directory (creates .python-version file):
pyenv local 3.11.10

# Set for current shell only:
pyenv shell 3.10.14

# Verify:
python --version
which python

pyenv + venv Together

# Set Python version for project:
cd myproject
pyenv local 3.11.10

# Create venv with that specific Python:
python -m venv .venv
source .venv/bin/activate

# Both pyenv version and venv are active:
python --version    # 3.11.10
which python        # myproject/.venv/bin/python

pipx — Install Python CLI Tools

pipx installs Python command-line tools in isolated environments. Unlike pip install --global, tools installed with pipx don't conflict with each other or with your projects.

# Install pipx:
sudo apt install pipx -y    # Ubuntu 23.04+
pipx ensurepath

# Or via pip:
pip install pipx --user
python3 -m pipx ensurepath

# Install tools:
pipx install httpie           # HTTP client
pipx install black            # Python code formatter
pipx install flake8           # linter
pipx install poetry           # dependency management
pipx install yt-dlp           # YouTube downloader
pipx install claude-code      # Claude Code CLI (if available)

# List installed:
pipx list

# Upgrade all:
pipx upgrade-all

Set Up Python for AI and Machine Learning

Python is the primary language for AI/ML. Here's a complete setup for running local AI models:

# Create a dedicated AI environment:
python3 -m venv ~/venvs/ai
source ~/venvs/ai/bin/activate

# Install core AI/ML libraries:
pip install --upgrade pip

# PyTorch with CUDA (for NVIDIA GPU):
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu124

# Or CPU-only version:
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu

# Common AI libraries:
pip install 
  transformers           # HuggingFace transformers
  diffusers              # Stable Diffusion
  accelerate             # multi-GPU, quantization
  datasets               # HuggingFace datasets
  numpy 
  pandas 
  matplotlib 
  scikit-learn 
  jupyter 
  ipykernel

# Verify GPU is available:
python3 -c "import torch; print('CUDA:', torch.cuda.is_available()); print('GPU:', torch.cuda.get_device_name(0) if torch.cuda.is_available() else 'None')"

Run Jupyter Notebook

# Install Jupyter:
pip install jupyter

# Start notebook server:
jupyter notebook

# Or JupyterLab (better UI):
pip install jupyterlab
jupyter lab

# Access in browser: http://localhost:8888

# On a remote server, tunnel via SSH:
# Server: jupyter lab --no-browser --port=8888
# Local: ssh -L 8888:localhost:8888 user@server

Troubleshooting

ModuleNotFoundError: No module named 'pip'

# Install pip manually:
python3 -m ensurepip --upgrade

# Or on Debian/Ubuntu:
sudo apt install python3-pip

error: externally-managed-environment (Ubuntu 23.04+)

# Best solution: use a virtual environment (recommended):
python3 -m venv myenv && source myenv/bin/activate && pip install package

# Alternative: use pipx for CLI tools
pipx install black

# Last resort: override (understand the risks first):
pip install package --break-system-packages

pip install fails with gcc/compilation error

# Install development tools:
sudo apt install python3-dev build-essential -y   # Ubuntu/Debian
sudo dnf install python3-devel gcc -y             # Fedora
sudo pacman -S base-devel -y                      # Arch

# For specific errors about missing headers (e.g., openssl):
sudo apt install libssl-dev                        # Ubuntu/Debian

Which python vs. python3

# Make 'python' point to python3:
# Option 1: alias in ~/.bashrc:
alias python=python3
alias pip=pip3

# Option 2: update-alternatives:
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 1

# Option 3: install the python-is-python3 package (Ubuntu):
sudo apt install python-is-python3

Frequently Asked Questions

Should I use conda or venv?

For most Python development: venv (built-in, lightweight, no dependencies). For scientific computing and data science where you need non-Python packages alongside Python ones (like CUDA, HDF5, MKL): conda or mamba. The rule: start with venv, switch to conda when you hit a package that conda handles better.

What's the difference between pip and pip3?

On systems with both Python 2 and 3, pip might manage Python 2 packages and pip3 manages Python 3. On modern systems (Python 2 is dead), pip and pip3 are usually the same. Inside an activated virtual environment, pip always refers to that environment's pip.

How do I keep my requirements.txt up to date?

# The classic way (pinned versions):
pip freeze > requirements.txt

# Problem: pip freeze includes transitive dependencies (dependencies of dependencies)
# For cleaner requirements, use pip-tools:
pip install pip-tools

# Create requirements.in with just your direct dependencies:
echo "requests" > requirements.in
echo "numpy>=1.24" >> requirements.in

# Generate pinned requirements.txt:
pip-compile requirements.in

# Update all packages:
pip-compile --upgrade requirements.in

Python 3.10, 3.11, 3.12, 3.13 — which to use?

Python 3.12 is the current stable release (2026) and is what you should use for new projects. Python 3.10 and 3.11 are still supported. For AI/ML work, check that your key libraries (PyTorch, TensorFlow) support the version — they typically lag by 2-3 months after a new Python release.

Python is the backbone of modern Linux automation and AI workloads. Once your environment is set up, explore our guides on running AI models locally with Ollama and bash scripting to combine Python and shell scripting for powerful automation.


Go up