python
ubuntu
default version
python3
development environment

Unable to set default python version to python3 in ubuntu

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

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

bash
1# Check what "python" points to
2python --version
3# bash: python: command not found  (common on Ubuntu 20.04+)
4
5# Check Python 3
6python3 --version
7# Python 3.10.12
8
9# Check all installed Python versions
10ls /usr/bin/python*
11# /usr/bin/python3  /usr/bin/python3.10  /usr/bin/python3.11
12
13# Check if alternatives are configured
14update-alternatives --list python
15# update-alternatives: error: no alternatives for python

The update-alternatives system lets you manage multiple versions of a command with configurable priority.

bash
1# Register Python 3.10 as an alternative for "python"
2sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.10 1
3
4# Register Python 3.11 if also installed
5sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.11 2
6
7# The number at the end is the priority — higher wins in auto mode
8
9# Switch between versions interactively
10sudo update-alternatives --config python
11#   Selection    Path                 Priority   Status
12# ------------------------------------------------------------
13# * 0            /usr/bin/python3.11   2          auto mode
14#   1            /usr/bin/python3.10   1          manual mode
15#   2            /usr/bin/python3.11   2          manual mode
16
17# Verify
18python --version
19# Python 3.11.x

Method 2: Shell Alias

For a per-user solution that does not affect the system, add an alias to your shell configuration.

bash
1# Add to ~/.bashrc or ~/.zshrc
2alias python=python3
3alias pip=pip3
4
5# Reload the shell config
6source ~/.bashrc
7
8# Verify
9python --version
10# Python 3.10.12

This only affects interactive shells — scripts that use #!/usr/bin/env python will not pick up the alias.

bash
1# Create a symlink manually
2sudo ln -s /usr/bin/python3 /usr/bin/python
3
4# Verify
5python --version
6# Python 3.10.12

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.

bash
1# Create a virtual environment with Python 3.11
2python3.11 -m venv myproject_env
3
4# Activate it
5source myproject_env/bin/activate
6
7# Now "python" points to Python 3.11 within this environment
8python --version
9# Python 3.11.x
10
11# Deactivate when done
12deactivate

Fixing pip After Changing Python Version

After changing the default Python, pip may also need updating.

bash
1# Install pip for the new default Python
2python -m ensurepip --upgrade
3
4# Or install via apt
5sudo apt install python3-pip
6
7# Verify pip points to the correct Python
8pip --version
9# pip 23.x from /usr/lib/python3/dist-packages/pip (python 3.11)
10
11# If pip still points to the old version, use:
12python -m pip install --upgrade pip

Common Pitfalls

  • Breaking system tools by changing the system Python: Ubuntu system utilities like apt, gnome-terminal, and do-release-upgrade depend on the system Python. Removing Python 3.x or forcibly replacing /usr/bin/python3 can make these tools fail. Never remove the system Python package. Use update-alternatives which manages symlinks safely.
  • Using sudo ln -sf to overwrite /usr/bin/python3: Overwriting the python3 symlink (not python) can break apt and package management. Only create or modify the python symlink — leave python3 managed by the system package manager.
  • Alias not working in scripts or cron jobs: Shell aliases defined in ~/.bashrc only apply to interactive shells. Scripts invoked by cron, systemd, or #!/usr/bin/env python shebangs will not see the alias. Use update-alternatives or modify the shebang to #!/usr/bin/env python3.
  • Forgetting to update pip after switching versions: After changing the default Python, pip may still be associated with the old version. Always run python -m pip install --upgrade pip after switching to ensure pip installs packages for the correct Python.
  • Not using deadsnakes PPA 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 --install to register Python versions and --config to switch between them
  • Add alias python=python3 to ~/.bashrc for 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 pip after changing the default Python version

Course illustration
Course illustration

All Rights Reserved.