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

While many tutorials jump straight to the usual apt install python3 or yum install python3 commands, few explain the nuances between different Linux distributions, package managers, and Python versions. In this post, we’ll cut through the noise and offer a pragmatic approach that works no matter your Linux flavor—because one-size-fits-all advice is often just outdated or incomplete.

Python is an essential programming language for developers and data scientists alike. Knowing how to correctly install and manage Python versions on Linux ensures a stable development environment and maximizes productivity. A clear, accurate installation process prevents common pitfalls and saves you time troubleshooting.


Why Proper Python Installation Matters on Linux

Unlike Windows or macOS, Linux comes in a dizzying variety of distributions (Ubuntu, Debian, Fedora, Arch, CentOS, and more), each with its own preferred package manager and Python versions in their repositories. Additionally:

  • The system Python is usually tied to OS tools and should not be modified or removed casually.
  • Developers often need multiple Python versions (e.g., 3.8, 3.9, 3.10) for different projects.
  • Managing Python with tools like pyenv or using virtual environments keeps projects isolated.
  • Installing Python incorrectly risks dependency hell or broken system scripts.

With that, let’s dive into a step-by-step, distribution-agnostic method to install and manage Python on Linux effectively.


Step 1: Identify Your Linux Distribution and Package Manager

Your installation commands depend heavily on your Linux distribution's package manager:

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

To check your Linux version, run:

cat /etc/os-release

Step 2: Check Existing Python Versions

Most modern Linux distros come with some Python version pre-installed. Check what you have:

python3 --version

Or

python --version

Beware that some systems link python to Python 2.x, which is deprecated, so prefer using python3.


Step 3: Install or Upgrade Python Using Your Package Manager

For Ubuntu/Debian

Here’s how to install or upgrade Python 3:

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

Check the installed version:

python3 --version

If the version is older than what you need (say you want Python 3.10 or 3.11 but your repo has 3.8), you can add the deadsnakes PPA, which maintains newer Python versions:

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

For Fedora

sudo dnf install python3 python3-pip python3-virtualenv

To get a specific newer Python version if Fedora’s repos lag, consider building from source or using pyenv (we’ll discuss this shortly).

For Arch Linux

Arch Linux usually ships the latest Python in its repo:

sudo pacman -Syu python python-pip

Step 4: Managing Multiple Python Versions with Pyenv

If your projects require multiple Python versions, it's best not to rely on your Linux package manager alone. Enter pyenv—a powerful tool to easily install and switch between Python versions without affecting your system Python.

Installing Pyenv

# Install dependencies (Ubuntu example)
sudo apt update
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

# Clone pyenv repository
curl https://pyenv.run | bash

Add these lines to your ~/.bashrc or ~/.zshrc:

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

Restart your shell or run source ~/.bashrc to load pyenv.

Install a Python Version with Pyenv

pyenv install 3.11.2
pyenv global 3.11.2

Check version:

python --version

Now python points to Python 3.11.2 managed by pyenv.


Step 5: Creating Virtual Environments

Virtual environments keep your project dependencies isolated. Whether using system Python or pyenv-installed Python, create a virtual environment like so:

python3 -m venv myproject-env
source myproject-env/bin/activate
pip install --upgrade pip setuptools

Deactivate with:

deactivate

Using virtual environments prevents polluting your system site packages and avoids conflicting dependencies between projects.


Step 6: Verify Your Installation and Setup

Make sure you have:

  • The correct Python version:

    python3 --version
    # or
    python --version
    
  • Working package manager:

    pip3 --version
    
  • Virtual environment activated and working:

    source myproject-env/bin/activate
    python -m pip install requests
    python -c "import requests; print(requests.__version__)"
    deactivate
    

Bonus: Building Python from Source (Advanced)

When the required Python version is not available in your repos or through pyenv, building from source is the fallback.

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.2/Python-3.11.2.tgz
sudo tar xzf Python-3.11.2.tgz
cd Python-3.11.2
sudo ./configure --enable-optimizations
sudo make altinstall  # 'altinstall' prevents overwriting system python

After this, call it with:

python3.11 --version

Final Thoughts

Mastering Python installation on Linux is fundamental for developers aiming for a reproducible, stable development environment. Avoid modifying your system Python directly—use your package manager wisely, rely on pyenv for multiple versions, and embrace virtual environments to isolate your projects.

With this approach, you’re ready to tackle Python projects on any Linux distribution confidently—whether Ubuntu, Fedora, Arch, or anything else.

Happy coding! 🐍🚀


If you found this guide helpful, please share and stay tuned for future posts on Python environment management and optimization on Linux!