Your Debian Linux installation may include multiple python versions and thus also include multiple python binary executables, and it’s possible to change python version that the system is using. In this tutorial, you will see how to install multiple versions of python, and change the python version on Debian using the update-alternatives python command. Check the sections below to learn how.

How to change from default to alternative Python version on Debian Linux
How to change from default to alternative Python version on Debian Linux
CategoryRequirements, Conventions or Software Version Used
SystemDebian Linux
SoftwarePython
OtherPrivileged access to your Linux system as root or via the sudo command.
Conventions# – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Install Python on Debian

There are many different versions of Python available. The two that most developers probably want to install, and which are available in Debian’s default repos, are the latest versions of Python 2 and 3. These commands below can be used to install them.

Install Python 2 on Debian:

$ sudo apt install python2

Install Python 3 on Debian:

$ sudo apt install python3 

What version of Python am I using?

You can run the following ls command to find out what python binary executables are available on your system:

$ ls /usr/bin/python*
/usr/bin/python  /usr/bin/python2  /usr/bin/python2.7  /usr/bin/python3  /usr/bin/python3.4  /usr/bin/python3.4m  /usr/bin/python3m

To check what is your default python version execute:

$ python --version
Python 2.7.8

If that command doesn’t work, try with python3 command instead:

$ python3 --version

Change python version on per user basis

To change a python version on per user basis you simply create an alias within user’s home directory. Open ~/.bashrc file and add new alias to change your default python executable:

alias python='/usr/bin/python3.4'

Once you make the above change, re-login or source your .bashrc file:

$ . ~/.bashrc

Check your default python version:

$ python --version
Python 3.4.2

Change python version system-wide with update-alternatives python

To change python version system-wide we can use update-alternatives python command. Logged in as a root user, first list all available python alternatives:

# update-alternatives --list python
update-alternatives: error: no alternatives for python

The above error message means that no python alternatives has been recognized by update-alternatives command. For this reason we need to update our alternatives table and include both python2.7 and python3.4:

# update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
update-alternatives: using /usr/bin/python2.7 to provide /usr/bin/python (python) in auto mode
# update-alternatives --install /usr/bin/python python /usr/bin/python3.4 2
update-alternatives: using /usr/bin/python3.4 to provide /usr/bin/python (python) in auto mode

The --install option take multiple arguments from which it will be able to create a symbolic link. The last argument specified it priority means, if no manual alternative selection is made the alternative with the highest priority number will be set. In our case we have set a priority 2 for /usr/bin/python3.4 and as a result the /usr/bin/python3.4 was set as default python version automatically by update-alternatives command.

# python --version
Python 3.4.2

Next, we can again list all python alternatives:

# update-alternatives --list python
/usr/bin/python2.7
/usr/bin/python3.4

From now on, we can anytime switch between the above listed python alternative versions using below command and entering a selection number:

# update-alternatives --config python

debian linux change alternative python version

# python --version
Python 2.7.8

Troubleshooting

In case we no longer have the alternative python version installed on our system we can remove its update-alternatives listing. For example let’s remove python2.7 version:

# update-alternatives --remove python /usr/bin/python2.7
update-alternatives: removing manually selected alternative - switching python to auto mode
update-alternatives: using /usr/bin/python3.4 to provide /usr/bin/python (python) in auto mode

Closing Thoughts

In this tutorial, we saw how to switch between multiple Python versions on Debian Linux. As you have seen here, there is no need to remove one version of Python in favor of the other, as you can use both concurrently. This comes in handy for Python programmers that want to write code for different versions, allowing them to retain support for legacy applications while still coding in the latest standards.