Mastering Python Installation on Ubuntu: Beyond the Basics
Python isn’t just another programming language—it's the backbone of countless development and automation workflows on Ubuntu servers and desktops. Whether you’re a seasoned developer, a DevOps engineer, or a hobbyist, correctly installing and managing Python on your Ubuntu system is crucial. Too often, one-size-fits-all tutorials leave users juggling conflicts between system and user-installed Python versions or struggling with deprecated packages.
This post goes beyond the basics to show you how to install, manage, and switch between multiple Python versions on Ubuntu without headaches. By mastering these techniques, you’ll future-proof your projects and optimize your development workflow.
Why Care About Python Installation on Ubuntu?
Ubuntu ships with Python pre-installed—often Python 3, but sometimes Python 2 as well (depending on the version). However:
- The system's default Python version might be outdated.
- Installing or upgrading via traditional
apt
methods can mix system-critical libraries with your projects. - Certain applications require different Python versions simultaneously.
- Using virtual environments alone doesn’t solve version conflicts if the base interpreter isn’t what you need.
Thus, mastering installation means understanding not just how to install Python but how to manage multiple versions side by side.
Step 1: Check Your Current Python Versions
Open your terminal and check what you’ve got already:
python --version
python3 --version
You might get something like:
Python 2.7.18
Python 3.8.10
On newer Ubuntus (20.04+), python
might not point anywhere by default, as it’s being phased out in favor of python3
.
Step 2: Install the Latest Python Version With apt
Ubuntu's APT repositories usually contain several supported versions of Python.
To search for available packages:
apt-cache search python3 | grep "^python3\.[0-9]"
To install the latest stable release available (for example, python3.10):
sudo apt update
sudo apt install python3.10 python3.10-venv python3.10-dev
This gives you:
- The interpreter (
python3.10
) - The virtual environment module (
venv
), essential for isolated project environments - Development headers (
dev
) for compiling C extensions or installing some packages
Step 3: Managing Multiple Versions — Switching Between Interpreters
Now that you have multiple versions installed (say python3.8
and python3.10
), how do you choose which one runs when you type python3
?
Ubuntu uses the update-alternatives
system to manage multiple binary versions:
Set Up update-alternatives
for Python
Run these commands as sudo:
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 2
Here the number at the end is priority; higher means more preferred by default.
To select which version is active:
sudo update-alternatives --config python3
You’ll see a prompt like this:
There are 2 choices for the alternative python3 (providing /usr/bin/python3).
Selection Path Priority Status
------------------------------------------------------------
* 1 /usr/bin/python3.8 1 auto mode
2 /usr/bin/python3.10 2 manual mode
Press <enter> to keep the current choice[*], or type selection number:
Enter 2
to switch to Python 3.10.
Check your current default with:
python3 --version
Now it should reflect your chosen version.
Note: Be cautious switching system defaults if other system tools depend on a specific version.
Step 4: Using Pyenv — The Developer’s Best Friend for Multiple Versions
While update-alternatives
manages global binaries, pyenv gives powerful per-user control over many Python versions without sudo or risking system conflicts.
Install Pyenv
First, install dependencies for building Pythons:
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
Then install pyenv via Git:
curl https://pyenv.run | bash
Follow on-screen instructions to add pyenv to your shell profile (~/.bashrc
, ~/.zshrc
):
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv virtualenv-init -)"
Reload terminal or source config.
Install and Manage Python Versions With Pyenv
List all available versions:
pyenv install --list | grep -E "^\s*3\."
Install the latest stable version (example 3.11.2
):
pyenv install 3.11.2
Set global default version for your user:
pyenv global 3.11.2
Check which version is active from pyenv’s perspective:
pyenv version
python --version
You can override the default per project directory by placing a .python-version
file automatically when running:
pyenv local 3.9.9 # sets local project interpreter to python 3.9.9 only in current directory
This setup ensures no conflicts between projects and lets you experiment freely with newer or older interpreters.
Step 5: Virtual Environments — Isolating Project Dependencies
Whichever installation method you prefer, always use virtual environments within projects!
Create a venv with installed Python version (whether apt-installed or pyenv-managed):
# Using system-installed interpreter (example python3)
python3 -m venv myproject-env
# If using pyenv:
pyenv shell 3.x.x # set pyenv generated python temporarily
python -m venv myproject-env
Activate it before installing dependencies or running scripts:
source myproject-env/bin/activate # Linux/MacOS bash/zsh shell
# On Windows PowerShell:
myproject-env\Scripts\Activate.ps1
Bonus Tips
- Consider installing pipx to run isolated CLI tools written in Python globally without polluting environments.
sudo apt install pipx
pipx ensurepath
pipx install black # example tool
-
Always keep your system’s default interpreter intact, especially if critical OS utilities depend on it!
-
For conda users, Miniconda & Anaconda offer alternative env/version management outside of system-level control but can be overkill for simple cases.
Conclusion
Mastering Python installation on Ubuntu means understanding that “one-size-fits-all” doesn't work when managing modern development workflows across projects that may require different interpreters.
Whether through APT packages combined with update-alternatives
, or more flexibly with pyenv, managing multiple Python versions independently safeguards your environment from conflicts and allows rapid switching—and paired with virtual environments—is a best practice every dev should embrace.
Try these methods today to future-proof your setups—because well-managed interpreters mean happier coding!
Happy coding! 🐍🚀 If this helped you set up a solid Python environment on Ubuntu, share your experience or questions below!