How to Check, Change, and Switch Python Versions on Ubuntu [Complete Guide]

1. Introduction

When using Python on Ubuntu, managing Python versions is a crucial aspect.
Python is regularly updated with new versions, and different development environments may require different versions.

However, Ubuntu often has multiple Python versions installed, leading to situations where you might need to:
– Check the current Python version
– Use a specific Python version
– Switch between different Python versions

In this article, we will provide a comprehensive guide on checking, changing, and switching Python versions on Ubuntu.
With easy-to-follow command examples, even beginners can follow along. Be sure to read until the end!

2. How to Check Your Python Version [Try It Instantly!]

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

2.1 The Easiest Method (Check in 1 Second)

The simplest way to check the Python version on Ubuntu is by running the following command in the terminal:

python3 --version

You can also use this alternative command to get the same result:

python3 -V

Example output:

$ python3 --version
Python 3.10.6

This command displays the current Python version installed on your system.

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

On Ubuntu, the python command may refer to Python 2.x, depending on your system configuration.
For this reason, it’s recommended to use python3 --version to check your Python version.

You can verify if the python command is available with the following command:

python --version

If you see an error message like Command 'python' not found, this means that only Python 3 is installed on your system.

2.3 Getting Detailed Version Information

If you need more detailed information about your Python version, use this command:

python3 -VV

Example output:

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

This command provides additional details, such as the compiler version (GCC) and build date.

2.4 Checking the Python Version Inside a Script

If you need to check the Python version inside a Python script, use the sys module:

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

Example output:

$ 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)

The sys.version_info object allows you to retrieve version components (major, minor, and micro) as numerical values.

年収訴求

3. Changing and Managing Python Versions [Set System Default]

Since Ubuntu may have multiple Python versions installed, this section explains how to change the default Python version for your system.

3.1 Checking Installed Python Versions

First, check which Python versions are installed on your system:

ls /usr/bin/python*

Example output:

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

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

3.2 Switching the Default Python Version Using update-alternatives

Ubuntu allows you to switch the default Python version using the update-alternatives command.

First, check the current configuration:

sudo update-alternatives --display python

If Python is not registered in update-alternatives, register it with these commands:

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, select the default version:

sudo update-alternatives --config python

Example output:

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 corresponding to the Python version you want to set as default.

3.3 Manually Changing the Default Python Version Using a Symbolic Link

If you prefer not to use update-alternatives, you can manually change the default Python version by updating the symbolic link:

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

After this change, the python command will point to python3.10 system-wide.

4. Switching Python Versions for Each Project

Sometimes, you may need to use different Python versions for different projects.
For example, one project may require Python 3.10, while another project may need Python 3.8.
In such cases, using virtual environments (venv) or pyenv can be very helpful.

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

4.1 Managing Python Versions per Environment with venv

Python includes a built-in feature called venv (virtual environment).
By using a virtual environment, you can manage different Python versions and dependencies within specific directories.

Creating a Virtual Environment with venv

First, navigate to the directory where you want to create the virtual environment and run the following command:

python3 -m venv myenv

This creates a virtual environment named myenv.

Activating the Virtual Environment

To activate the virtual environment, run:

source myenv/bin/activate

Once activated, the terminal prompt will change:

(myenv) user@ubuntu:~/project$

While the virtual environment is active, all Python commands will use this isolated environment.

Checking the Python Version Within the Virtual Environment

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

python --version

Deactivating the Virtual Environment

To exit the virtual environment, run:

deactivate

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

4.2 Managing Python Versions with pyenv

While venv is useful for per-project management, pyenv is a better solution for switching Python versions system-wide.

Installing pyenv

To install pyenv on Ubuntu, run:

curl https://pyenv.run | bash

After installation, apply the changes with:

exec $SHELL

Installing a Specific Python Version with pyenv

To install a specific Python version, use:

pyenv install 3.10.6

To check available versions, use:

pyenv install --list

Switching Python Versions with pyenv

To set a global default Python version:

pyenv global 3.10.6

To change the Python version for a specific directory only:

pyenv local 3.8.10

Checking the Current Python Version in pyenv

To see the currently selected Python version in pyenv, use:

pyenv versions

With pyenv, you can easily manage multiple Python versions across different projects.

5. Frequently Asked Questions (FAQ) [Troubleshooting]

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

Q1: What is the difference between python and python3?

On Ubuntu, python3 is the default, while python may refer to Python 2.x.
It is recommended to always use python3 --version to check the Python version.

Q2: The version displayed by python --version is not what I expected. How can I fix this?

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

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

Q3: python3 --version works, but python doesn’t. Why?

Your system may not have the python command linked to Python 3.
To fix this, create a symbolic link:

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

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

First, check the installed Python versions:

apt list --installed | grep python

To remove a specific Python version, run:

sudo apt remove python3.6

Q5: Will removing an old Python version affect my system?

Ubuntu system tools depend on certain Python versions.
Before removing any version, check which ones are installed:

apt list --installed | grep python

6. Summary & Recommended Articles

We have covered how to check, change, and switch Python versions on Ubuntu in detail.

  • Check Python version: python3 --version
  • Change system-wide default: Use update-alternatives or ln -sf
  • Manage project-specific versions: Use venv (virtual environments) or pyenv

Using pyenv makes Python version management much easier.
If you need to use different Python versions for various projects or change the default system version, try pyenv!

Related Resources

Pythonプログラミングの世界

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

侍エンジニア塾