replace

[bash:~] $ sudo update-alternatives --install /usr/bin/python python \
/usr/bin/python2.7 2

[bash:~] $ sudo update-alternatives --install /usr/bin/python python \
/usr/bin/python3.5 3

with

[bash:~] $ sudo update-alternatives --install /usr/local/bin/python python \
/usr/bin/python2.7 2

[bash:~] $ sudo update-alternatives --install /usr/local/bin/python python \
/usr/bin/python3.5 3

e.g. installing into /usr/local/bin instead of /usr/bin.

and ensure the /usr/local/bin is before /usr/bin in PATH.

i.e.

[bash:~] $ echo $PATH
/usr/local/bin:/usr/bin:/bin

Ensure this always is the case by adding

export PATH=/usr/local/bin:$PATH

to the end of your ~/.bashrc file. Prefixing the PATH environment variable with custom bin folder such as /usr/local/bin or /opt/<some install>/bin is generally recommended to ensure that customizations are found before the default system ones.

Answer from jonrobm on Stack Overflow
Top answer
1 of 7
61

replace

[bash:~] $ sudo update-alternatives --install /usr/bin/python python \
/usr/bin/python2.7 2

[bash:~] $ sudo update-alternatives --install /usr/bin/python python \
/usr/bin/python3.5 3

with

[bash:~] $ sudo update-alternatives --install /usr/local/bin/python python \
/usr/bin/python2.7 2

[bash:~] $ sudo update-alternatives --install /usr/local/bin/python python \
/usr/bin/python3.5 3

e.g. installing into /usr/local/bin instead of /usr/bin.

and ensure the /usr/local/bin is before /usr/bin in PATH.

i.e.

[bash:~] $ echo $PATH
/usr/local/bin:/usr/bin:/bin

Ensure this always is the case by adding

export PATH=/usr/local/bin:$PATH

to the end of your ~/.bashrc file. Prefixing the PATH environment variable with custom bin folder such as /usr/local/bin or /opt/<some install>/bin is generally recommended to ensure that customizations are found before the default system ones.

2 of 7
47

Per Debian policy, python refers to Python 2 and python3 refers to Python 3. Don't try to change this system-wide or you are in for the sort of trouble you already discovered.

Virtual environments allow you to run an isolated Python installation with whatever version of Python and whatever libraries you need without messing with the system Python install.

With recent Python 3, venv is part of the standard library; with older versions, you might need to install python3-venv or a similar package.

$HOME~$ python --version
Python 2.7.11

$HOME~$ python3 -m venv myenv
... stuff happens ...

$HOME~$ . ./myenv/bin/activate

(myenv) $HOME~$ type python   # "type" is preferred over which; see POSIX
python is /home/you/myenv/bin/python

(myenv) $HOME~$ python --version
Python 3.5.1

A common practice is to have a separate environment for each project you work on, anyway; but if you want this to look like it's effectively system-wide for your own login, you could add the activation stanza to your .profile or similar.

Top answer
1 of 2
9

I would try cleaning it up manually. I've never done this so make sure you backup beforehand.

  • Remove the link from /etc/alternatives
  • Remove the relevant file from the admin directory
    • /var/lib/dpkg/alternatives/ on ubuntu (debian may be the same but check the man pages under the FILES section)
    • /var/lib/alternatives/ on CentOS 6&7
2 of 2
11

Removing them manually resulted in ... is already managed by ... error in my case. Aside from that, the manual removal also didn't scale, as i've had added multiple multiple alternatives for the centralized symlink. It's just a mess.
When it comes to either update-alternatives or alternatives, they both provide the --remove and --remove-all flags. As for the former flag, it only removes one single alternative of the link group. For example

$ update-alternatives --list python3
/usr/bin/python3.8
/usr/bin/python3.9

$ sudo update-alternatives --remove python3 /usr/bin/python3.9

$ update-alternatives --list python3
/usr/bin/python3.8

The form of the --remove flag goes by update-alternatives --remove symlink-generic-name /path/to/one/executable/alternative

However, if you want to outright erase the generic name with all of its associated alternatives all at once, then --remove-all is the way to go.

$ update-alternatives --list python3
/usr/bin/python3.8
/usr/bin/python3.9

$ sudo update-alternatives --remove-all python3

$ update-alternatives --list python3
update-alternatives: error: no alternatives for python3

$ update-alternatives --query python3
update-alternatives: error: no alternatives for python3
Discussions

How to set default Python version without --update-alternatives - Stack Overflow
I have Python3.6 and Python3.8 installed on Ubuntu 18.04. When I execute python3 -V I get python3.8.0, which is correct. That's currently my default Python. I used the Ubuntu 18.04 package reposi... More on stackoverflow.com
๐ŸŒ stackoverflow.com
debian - How to prevent changing default Python version? - Unix & Linux Stack Exchange
Many applications I use (arandr, inkscape, ...) get broken when default Python version is set to Python-3 by update-alternatives --config python. How can I prevent myself from setting default Python More on unix.stackexchange.com
๐ŸŒ unix.stackexchange.com
ubuntu - sudo update-alternatives --config . How to set the auto entry - Unix & Linux Stack Exchange
I'm trying to run a script that does somewhere: python setup.py egg_info This gives me an error. But if I manually do : python3 setup.py egg_info I don't get the error. Therefore I tried to sudo update-alternatives --config python . More on unix.stackexchange.com
๐ŸŒ unix.stackexchange.com
June 27, 2022
Remove python2, set python3 as default /usr/bin/python
I'm not sure removing python2 is a good idea.Some system programs could need it. I'd rather have made a python simlink pointing to py3 More on reddit.com
๐ŸŒ r/openSUSE
15
10
December 13, 2020
๐ŸŒ
Linux Hint
linuxhint.com โ€บ update_alternatives_ubuntu
How to Use update-alternatives Command on Ubuntu โ€“ Linux Hint
Based on the priority, an alternative should be set for the python alternatives. In this case, the Python 3 alternative. As you can see, python command executes the Python 3 interpreter. ... You can remove an alternative from the python alternatives.
๐ŸŒ
LinuxConfig
linuxconfig.org โ€บ home โ€บ how to change from default to alternative python version on debian linux
How to change from default to alternative Python version on Debian Linux
December 7, 2023 - 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
๐ŸŒ
TecAdmin
tecadmin.net โ€บ linux-update-alternatives-command
Update-alternatives Command: A Comprehensive Guide for Linux Users โ€“ TecAdmin
April 26, 2025 - โ€“remove: To remove an alternative ยท โ€“config: To configure an alternative manually ยท โ€“auto: To configure an alternative automatically ยท โ€“display: To display information about an alternative ยท Example: sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 1 ยท
Find elsewhere
๐ŸŒ
Python Forum
python-forum.io โ€บ thread-33298.html
Eliminating error in Python update-alternatives
I made an error when typing this code: sudo update-alternatives --install /usr/bin/python3 python /usr/bin/python2.7 1Obviously, I do not want to that. It create issues like this: Output:root@LAPTOP-8R8F1BRA:/home/james# python -V Python 2.7.17 ro...
Top answer
1 of 1
6

Python 2.X packages are named differently than Python 3.X packages in the default Ubuntu repositories, python-... for Python 2.X packages vs. python3-... for Python 3.X packages. The command to start the Python interpreter follows a similar pattern, python for the default Python 2.X version vs. python3 for the default Python 3.X version.

Don't remove the default Python 3.x version because removing it can break a lot of things like the terminal, the Software app and many other apps. For more information see this question: Removed Python 3 and now Ubuntu Software Center, terminal and other applications don't work. The good news is that even if you break those things it is possible to restore the original Python 3.x version by booting Ubuntu into recovery mode, and then Ubuntu will work normally again.

Instead of replacing the default Python 3.x version entirely the recommended way of installing another Python 3.x version is to keep the existing Python 3 and install the new Python 3 version alongside it. Then you can use a program called update-alternatives to select which one of the Python versions you want to use.

Add Python 3.8 to update-alternatives so that you can switch between Python 3.5 and Python 3.8 by running update-alternatives --config python3.

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.5
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8
update-alternatives --config python3

After you are done using Python 3.8 you can switch the it back to the default Python 3.5 version.

  • List installed versions of Python: update-alternatives --list python

  • Switch between Python versions: update-alternatives --config python

    From the terminal command-line Press <enter> to keep the current choice[*], or type selection number:

๐ŸŒ
GitHub
gist.github.com โ€บ 6910dd256f989abb41af30a8eeb01237
Update alternatives syntax for changing from python2.7 to python3.7 ยท GitHub
If you are on python2.7, we already know there will be no further development on that platform other than patches or security updates, so the only time you would need to modify will be for python3 revisions, etc. ... update-alternatives --config python There are 2 choices for the alternative python (providing /usr/bin/python).
๐ŸŒ
Ubuntu
manpages.ubuntu.com โ€บ trusty โ€บ man(8)
Ubuntu Manpage: update-alternatives - maintain symbolic links determining default commands
It can be usefully combined with --skip-auto to review and configure all alternatives which are not configured in automatic mode. Broken alternatives are also displayed. Thus a simple way to fix all broken alternatives is to call yes '' | update-alternatives --force --all.
๐ŸŒ
Linux Man Pages
man7.org โ€บ linux โ€บ man-pages โ€บ man1 โ€บ update-alternatives.1.html
update-alternatives(1) - Linux manual page
It is often useful for a number of alternatives to be synchronized, so that they are changed as a group; for example, when several versions of the vi(1) editor are installed, the manual page referenced by /usr/share/man/man1/vi.1 should correspond to the executable referenced by /usr/bin/vi. update-alternatives handles this by means of master and slave links; when the master is changed, any associated slaves are changed too. A master link and its associated slaves make up a link group. Each link group is, at any given time, in one of two modes: automatic or manual. When a group is in automatic mode, the alternatives system will automatically decide, as packages are installed and removed, whether and how to update the links.
๐ŸŒ
Lyons Computer
lyonscomputer.com.au โ€บ Linux โ€บ python-update-alternatives โ€บ python-update-alternatives.html
VK4PK - python update-alternatives
python update-alternatives ยท apt install python-is-python3 ยท lsb_release -a # OS version python --version # currently linked python vwersion which python which python2 which python3 # Add some alternatives sudo update-alternatives --install /usr/local/bin/python python /usr/bin/python2 20 ...