Installing Python on Linux
Most Linux distributions provide Python 3 through their package manager, and some system tools depend on that interpreter. Do not replace or remove the distribution's system Python. Package names and supported versions vary by distribution and release, so confirm them in your distribution's documentation.
Check the existing interpreter
python3 --version
python3 -c "import sys; print(sys.executable)"
If Python 3 is missing, install it through the operating-system package manager. Common examples are:
# Debian or Ubuntu
sudo apt update
sudo apt install python3 python3-venv python3-pip
# Fedora
sudo dnf install python3 python3-pip
These commands are examples, not universal instructions. Enterprise, immutable, and minimal distributions may use different package names or policies.
Create a project environment
mkdir example-project
cd example-project
python3 -m venv .venv
source .venv/bin/activate
python -m pip --version
Add .venv/ to version control ignore rules. Install third-party packages only after activation, and prefer python -m pip so the selected interpreter is explicit.
When you need another Python version
The distribution package manager is the safest default. If a project needs a different version, use an isolated tool or container whose installation and update model you understand; do not overwrite /usr/bin/python3. A source build requires development headers and careful maintenance and is beyond this beginner guide.
Troubleshooting
- If
venvcreation fails, install the distribution package that suppliesvenv. - If pip reports an externally managed environment, create a virtual environment rather than bypassing the protection.
- Do not use
sudo python -m pip install ...; it can conflict with files owned by the operating-system package manager.