0%

How to Change Default Python Version on Debian Linux

Foreword

Your system may include multiple Python versions, the default alias for python might be using a version that you don’t want to work with.
Use the following command to find out what Python binary executables are avaliable:

1
2
3
$ ls /usr/bin/python*
/usr/bin/python /usr/bin/python2.7 /usr/bin/python3.7 /usr/bin/python3.8
/usr/bin/python2 /usr/bin/python3 /usr/bin/python3.7m

To check what is your default python version execute:

1
2
$ python --version
Python 2.7.8

Change python default on per user basis

Open ~/.bashrc file and add the following alias to change the default python executable:

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

Re-login after you made the change.

1
2
$ python --version
Python 3.8.2

Change python default system-wide

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

1
2
3
$ update-alternatives --list python
/usr/bin/python3.7
/usr/local/bin/python3.8

Adding alternatives

If you don’t have any alternatives, use the following command to add:

1
2
3
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.8 2
update-alternatives: using /usr/bin/python3.8 to provide /usr/bin/python (python) in auto mode

--install is used to create a symbolic link. The number a the end is for setting the priority, the higher the number, the larger the priority.

Switch default

1
$ update-alternatives --config python

Remove alternatives

In case we no longer have an alternative python version installed, we can remove its update-alternatives listing.

1
2
3
$ 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.8 to provide /usr/bin/python (python) in auto mode