Unable to set default python version to python3 in ubuntu
Codemia Knowledge Hub ·
Introduction
On Ubuntu, the python command historically pointed to Python 2. Modern Ubuntu versions (20.04+) no longer ship Python 2, but the python command may still be absent or misconfigured. Setting Python 3 as the default python involves using Ubuntu's update-alternatives system or creating a virtual environment. Changing the system-level python symlink carelessly can break system tools like apt and gnome-terminal that depend on specific Python versions.
Check Current Python Setup
Method 1: update-alternatives (Recommended)
The update-alternatives system lets you manage multiple versions of a command with configurable priority.
Method 2: Shell Alias
For a per-user solution that does not affect the system, add an alias to your shell configuration.
This only affects interactive shells — scripts that use #!/usr/bin/env python will not pick up the alias.
Method 3: Symlink (Quick but Risky)
This is simple but update-alternatives is preferred because it tracks the configuration and allows easy switching.
Method 4: Virtual Environments (Best for Projects)
For project-specific Python versions, use virtual environments instead of changing the system default.
Fixing pip After Changing Python Version
After changing the default Python, pip may also need updating.
Common Pitfalls
- Breaking system tools by changing the system Python: Ubuntu system utilities like
apt,gnome-terminal, anddo-release-upgradedepend on the system Python. Removing Python 3.x or forcibly replacing/usr/bin/python3can make these tools fail. Never remove the system Python package. Useupdate-alternativeswhich manages symlinks safely. - Using
sudo ln -sfto overwrite/usr/bin/python3: Overwriting thepython3symlink (notpython) can breakaptand package management. Only create or modify thepythonsymlink — leavepython3managed by the system package manager. - Alias not working in scripts or cron jobs: Shell aliases defined in
~/.bashrconly apply to interactive shells. Scripts invoked by cron, systemd, or#!/usr/bin/env pythonshebangs will not see the alias. Useupdate-alternativesor modify the shebang to#!/usr/bin/env python3. - Forgetting to update
pipafter switching versions: After changing the default Python,pipmay still be associated with the old version. Always runpython -m pip install --upgrade pipafter switching to ensurepipinstalls packages for the correct Python. - Not using
deadsnakesPPA for newer Python versions: Ubuntu's default repositories may not have the latest Python. To install Python 3.12+ on Ubuntu 22.04, add the deadsnakes PPA:sudo add-apt-repository ppa:deadsnakes/ppa && sudo apt update && sudo apt install python3.12.
Summary
- Use
update-alternatives --installto register Python versions and--configto switch between them - Add
alias python=python3to~/.bashrcfor a per-user, non-system-affecting solution - Use virtual environments for project-specific Python version isolation
- Never remove or overwrite
/usr/bin/python3— it is managed by the system package manager - Always update
pipafter changing the default Python version
Related reading
- Unable to solve, Error occurred when finalizing GeneratorDataset iterator Failed precondition Python interpreter state is not initialized
- Unavailable to install Tensorflow 1.x on Ubuntu 20.04 LTS using pip
- UnboundLocalError local variable 'batch_outputs' referenced before assignment
- UnboundLocalError trying to use a variable supposed to be global that is reassigned even after first use
- Understanding __get__ and __set__ and Python descriptors
- Understanding __getitem__ method in Python
- Understanding generators in Python
- Understanding NumPy's einsum
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.