Skip to content

Instantly share code, notes, and snippets.

@growtopiajaw
Created August 11, 2018 05:09
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save growtopiajaw/7c5b7556c9b9a48aa68fe063107e1a5d to your computer and use it in GitHub Desktop.
Save growtopiajaw/7c5b7556c9b9a48aa68fe063107e1a5d to your computer and use it in GitHub Desktop.
update-alternatives for python2 and python3 in Ubuntu 16.04.x

List available python options

ls -larth `which python`*
lrwxrwxrwx 1 root root   10 Mar 23  2016 /usr/bin/python3m -> python3.5m
lrwxrwxrwx 1 root root    9 Mar 23  2016 /usr/bin/python3 -> python3.5
lrwxrwxrwx 1 root root    9 Nov 24  2017 /usr/bin/python2 -> python2.7
lrwxrwxrwx 1 root root    9 Nov 24  2017 /usr/bin/python -> python2.7
-rwxr-xr-x 2 root root 4.3M Nov 28  2017 /usr/bin/python3.5m
-rwxr-xr-x 2 root root 4.3M Nov 28  2017 /usr/bin/python3.5
-rwxr-xr-x 1 root root 3.4M Dec  4  2017 /usr/bin/python2.7

We have 2 options available, python2.7 and python3.5 (These are default pythons that came with Ubuntu)

update-alternatives for python/ python2

sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.5 2

sudo update-alternatives --config python
There are 2 choices for the alternative python (providing /usr/bin/python).

  Selection    Path                Priority   Status
------------------------------------------------------------
* 0            /usr/bin/python3.5   2         auto mode
  1            /usr/bin/python2.7   1         manual mode
  2            /usr/bin/python3.5   2         manual mode

Press <enter> to keep the current choice[*], or type selection number: 1

update-alternatives for python3

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.5 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python2.7 2

sudo update-alternatives --config python3
There are 2 choices for the alternative python3 (providing /usr/bin/python3).

  Selection    Path                Priority   Status
------------------------------------------------------------
* 0            /usr/bin/python2.7   1         auto mode
  1            /usr/bin/python2.7   1         manual mode
  2            /usr/bin/python3.5   2         manual mode

Press <enter> to keep the current choice[*], or type selection number: 2

Done! Commands that requests for python, python2 or python3 will now work properly and say goodbye to errors!

Credit goes to patrickmmartin for his original gist at https://gist.github.com/patrickmmartin/5b6b2ddecd29d6a1b2ffee2d8eea31ec. This gist is just an update for python3 commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment