Managing Python Versions on Ubuntu
Ubuntu systems often come with both Python 2 and Python 3 installed. While Python 2 is now deprecated, it can sometimes interfere with Python 3 projects. This tutorial explains how to manage Python versions on Ubuntu and set Python 3 as the default interpreter.
Understanding the Situation
On many Ubuntu systems, the python command defaults to Python 2. This can lead to compatibility issues when working on projects that require Python 3. The goal is to configure your system so that running python executes Python 3.
Methods for Setting the Default Python Version
Several methods can achieve this. We’ll cover the most robust and recommended approaches, along with their pros and cons.
1. Using update-alternatives (Recommended)
The update-alternatives tool is designed for managing default commands, including Python interpreters. This method is preferred because it correctly integrates with the system’s package management and doesn’t rely on modifying your shell configuration.
-
Step 1: Check existing alternatives:
First, see what Python versions are currently registered withupdate-alternatives. Run:sudo update-alternatives --config pythonThis will list the available Python interpreters and prompt you to select a default. If Python 3 isn’t listed, proceed to step 2.
-
Step 2: Add Python 3 to
update-alternatives:If Python 3 is not listed, register it with
update-alternatives. The command structure is:sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 <priority>Replace
<priority>with an integer value. Higher numbers indicate higher priority. A value of1is reasonable. For example:sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 1If you have multiple Python 3 versions installed (e.g., 3.6, 3.7, 3.8), you can add each one with a different priority. For instance:
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6 1 sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.7 2This assigns a higher priority to Python 3.7, making it the default.
-
Step 3: Select the default Python version:
Run the configuration command again:
sudo update-alternatives --config pythonSelect the number corresponding to your desired Python 3 version from the list.
-
Step 4: Verify the change:
Open a new terminal and run:
python --versionThe output should now display the version of Python 3 you selected.
2. Using python-is-python3 Package (Ubuntu 20.04 and later)
Ubuntu 20.04 LTS (Focal Fossa) and later versions offer a convenient package for setting Python 3 as the default.
-
Installation:
sudo apt install python-is-python3This package creates a symbolic link in
/usr/bin/pythonthat points to/usr/bin/python3. -
Verification:
Open a new terminal and run:
python --versionThe output should display the Python 3 version.
3. Alias in Shell Configuration (Less Recommended)
While simpler, this method is not the most robust because it only affects the current user’s shell and may not work with sudo.
-
Edit
.bashrc:Open your
.bashrcfile in a text editor:nano ~/.bashrc -
Add Alias:
Add the following line to the beginning of the file:
alias python=python3 -
Source
.bashrc:Apply the changes by sourcing the file:
source ~/.bashrc -
Verification:
Open a new terminal and run:
python --versionThe output should display the Python 3 version. Be aware that commands executed with
sudowill not be affected by this alias.
Choosing the Right Method
- For most users, the
update-alternativesmethod is the most reliable and recommended approach. It correctly integrates with the system’s package management and doesn’t rely on modifying shell configuration files. - If you are using Ubuntu 20.04 LTS or later, the
python-is-python3package provides the simplest solution. - The alias method is quick for personal use but less robust and may not work with
sudo.