Install Python on Linux
Introduction
This chapter teaches you how to install Python on Linux in a clean, production-ready way. You will learn distribution-specific commands, environment verification steps, and practical troubleshooting for common setup failures. A correct Linux Python setup is important because many backend, automation, and CI workflows run on Linux servers.
Prerequisites
- A Linux machine (Ubuntu, Debian, Fedora, CentOS Stream, or similar)
- Basic terminal knowledge (
cd,ls, package manager commands) sudoaccess for package installation- Internet access for package repositories
Choose the Right Installation Strategy
Linux offers multiple ways to install Python. For most teams, using your distribution package manager is the safest default.
Recommended strategy:
- Use your distro package manager for stability and security updates
- Use
pyenvwhen you need multiple Python versions in parallel - Keep project dependencies isolated in virtual environments
Warning
Do not install project dependencies globally on shared Linux servers. This often causes version conflicts and production incidents.
Install Python on Debian and Ubuntu
For Debian-based systems, install Python and common tooling with apt.
# Update package index
sudo apt update
# Install Python runtime and venv tooling
sudo apt install -y python3 python3-venv python3-pip
# Verify Python version
python3 --version
# Verify pip version
pip3 --versionInstall Python on Fedora / RHEL-like Distributions
For Fedora and RHEL-family systems, use dnf.
# Install Python runtime and pip
sudo dnf install -y python3 python3-pip
# Verify Python version
python3 --version
# Verify pip version
pip3 --versionTip
Best Practice
On servers, keep OS packages updated before provisioning Python to reduce dependency and SSL issues.
FAQ
Should I use system Python for development on Linux?
It is better to keep system Python for OS tools and use project virtual environments for development work. This reduces conflicts and protects system components.
When should I use pyenv on Linux?
Use pyenv when you need multiple Python versions on one machine, such as testing across Python 3.10, 3.11, and 3.12.
Why does pip install a package but python cannot import it?
This usually means pip and python point to different interpreters. Activate the correct virtual environment and run installs with python -m pip.
Which command should I use: python or python3?
On Linux, python3 is the safest explicit command for Python 3. After activating a virtual environment, python usually maps to the correct local interpreter.