How to Check and Switch Python Versions on Ubuntu: Complete Guide for Developers

1. Introduction

When using Python on Ubuntu, managing Python versions becomes an important task.
Python frequently receives new releases, and depending on the development environment, you may need to use different versions.

However, Ubuntu may have multiple Python versions installed, leading to situations such as:
“Want to check the current Python version”
“Want to use a specific version”
“Want to switch Python versions”
These situations occur quite often.

This article explains in detail how to check, change, and switch Python versions on Ubuntu.
With command examples included, even beginners can follow along easily. Be sure to read through to the end.

2. How to Check the Python Version on Ubuntu【Try It Now!】

First, let’s look at how to check which Python version is currently installed on Ubuntu.

2.1 The Easiest Way (Check in 1 Second)

The simplest way to check your Python version in Ubuntu is to run the following command in the terminal:

python3 --version

You can get the same result using:

python3 -V

Example:

$ python3 --version
Python 3.10.6

This displays the current Python version.

2.2 Difference Between python --version and python3 --version

On Ubuntu, the python command may refer to Python 2.
Therefore, using python3 --version is recommended.

You can check whether python is installed by running:

python --version

If you see Command 'python' not found, it likely means that only Python 3 is installed.

2.3 Get Detailed Version Information

If you want more detailed information, run:

python3 -VV

Example:

$ python3 -VV
Python 3.10.6 (main, Jan 16 2024, 11:25:20) [GCC 11.2.0]

This command shows details such as the GCC version used for compilation and the build date.

2.4 Check the Python Version Inside a Script

If you want to check the Python version from within a script, use the sys module:

import sys
print(sys.version)
print(sys.version_info)

Example:

$ python3 script.py
3.10.6 (main, Jan 16 2024, 11:25:20) [GCC 11.2.0]
sys.version_info(major=3, minor=10, micro=6, releaselevel='final', serial=0)

Using sys.version_info, you can obtain each element of the version (major, minor, micro) as numbers.

3. How to Change or Manage Python Versions【Set System Default】

Ubuntu may have multiple Python versions installed.
This section explains how to change the system-wide default Python version.

3.1 Check Installed Python Versions

To check which Python versions are installed, run:

ls /usr/bin/python*

Example:

$ ls /usr/bin/python*
/usr/bin/python3  /usr/bin/python3.8  /usr/bin/python3.10

If multiple versions are installed, you can choose which one to use as the default.

3.2 Switch the Default Python Using update-alternatives

You can switch the default Python version using update-alternatives.

First, check the current settings:

sudo update-alternatives --display python

If python is not registered, add it with:

sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.10 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 2

Then choose the default version:

sudo update-alternatives --config python

Example:

There are 2 choices for the alternative python (providing /usr/bin/python).

  Selection    Path                Priority   Status
------------------------------------------------------------
* 0            /usr/bin/python3.10  1         auto mode
  1            /usr/bin/python3.10  1         manual mode
  2            /usr/bin/python3.8   2         manual mode

Press <enter> to keep the current choice[*], or type selection number: 

Enter the number of the version you want to switch to.

3.3 Manually Change the Symbolic Link

You can also manually change the symbolic link instead of using update-alternatives:

sudo ln -sf /usr/bin/python3.10 /usr/bin/python

This forces the python command to use python3.10 system-wide.

4. How to Switch Python Versions per Project

When working with Python on Ubuntu, you may want to use different Python versions for different projects.
For example, one project may require Python 3.10, while another may need Python 3.8.
In such cases, using virtual environments (venv) or pyenv is extremely useful.

This section explains how to easily switch Python versions using virtual environments and pyenv.

4.1 Manage Python Versions per Environment Using venv

Python provides venv (virtual environments) as a standard feature.
By using virtual environments, you can manage different Python versions or libraries within individual directories.

Create a Virtual Environment with venv

Move to the directory where you want to create a virtual environment, then run:

python3 -m venv myenv

This creates a virtual environment named myenv.

Activate the Virtual Environment

To activate the virtual environment, run:

source myenv/bin/activate

Once activated, the terminal prompt changes:

(myenv) user@ubuntu:~/project$

While active, the environment-specific Python version is used.

Check Python Version Inside the Environment

To check the Python version inside the virtual environment, run:

python --version

Deactivate the Virtual Environment

To exit the virtual environment, run:

deactivate

Using this method, you can manage Python versions and packages independently for each project.

4.2 Manage Python Versions Using pyenv

While venv manages Python per project,
if you want to freely switch the Python version system-wide, pyenv is very convenient.

Install pyenv

First, install pyenv.
To install pyenv on Ubuntu, run:

curl https://pyenv.run | bash

Then apply the settings with:

exec $SHELL

Install Python Versions with pyenv

To install a Python version using pyenv:

pyenv install 3.10.6

To check all available versions:

pyenv install --list

Switch Python Versions with pyenv

To change the global (system-wide) Python version:

pyenv global 3.10.6

To change the Python version for a specific directory:

pyenv local 3.8.10

Check the Current pyenv Python Version

To check which Python versions pyenv is managing, run:

pyenv versions

This allows you to easily manage different Python versions per project.

5. FAQ (Troubleshooting)

Here are common questions and troubleshooting tips when managing Python versions on Ubuntu.

Q1: What is the difference between python and python3?

On Ubuntu, python3 is the standard, while python may refer to Python 2.
Therefore, using python3 --version is recommended.

Q2: What if python --version shows an unexpected version?

You can change the default Python version with update-alternatives or pyenv.

  • Using update-alternatives:
sudo update-alternatives --config python
  • Using pyenv:
pyenv global 3.10.6

Q3: Why does python3 --version work but python does not?

The python command may not be installed.
Create a symbolic link to fix it:

sudo ln -sf /usr/bin/python3 /usr/bin/python

Q4: How do I remove an old Python version on Ubuntu?

First, list installed Python packages:

apt list --installed | grep python

To remove a specific Python version:

sudo apt remove python3.6

Q5: Will removing an older Python version affect Ubuntu?

Some Ubuntu system tools depend on specific Python versions.
Check with:

python3 --version

Before removing anything, always verify what is installed:

apt list --installed | grep python

6. Summary & Recommended Articles

In this guide, we covered how to check, change, and switch Python versions on Ubuntu.

  • Check Python versionpython3 --version
  • Change system-wide versionupdate-alternatives or ln -sf
  • Manage versions per projectvenv or pyenv

Using pyenv makes Python version management especially easy.
If you need different Python versions across multiple projects or want to change the system default, try using pyenv.

Related Site

Pythonプログラミングの世界

Pythonプログラミングの世界へようこそ!初心者から上級者まで、Pythonの基礎から応用、データ分析やWeb開発まで…