Set Python version with update-alternatives

Python users typically use a non-system Python distribution such as Miniconda. Python distributions typically allow easy switching of Python version. Where Linux system Python must be used, the default Python version can be switched persistently with update-alternatives.

Update-alternatives without sudo

This is a one-time setup.

  1. Configure Bash shell to use ~/.local/bin instead of system-wide /usr as follows:

    mkdir ~/.local/bin
    
  2. Add to ~/.bashrc

    export PATH="$HOME/.local/bin:$PATH"
    
  3. Enable switching Python default between Python 3 and 2.7 with these one-time commands:

    update-alternatives --install $HOME/.local/bin/python python /usr/bin/python3 20
    
    update-alternatives --install $HOME/.local/bin/python python /usr/bin/python2 10
    

After the setup above, at any time select the default system Python version without sudo:

update-alternatives --config python

Notes

With old Linux distros apt update may fail after making system Python default to Python 3. Temporarily switch back to Python 2 via update-alternatives if this becomes an issue.