Skip to content

Instantly share code, notes, and snippets.

@cobaohieu
Forked from growtopiajaw/update-python.md
Last active June 3, 2021 15:48
Show Gist options
  • Save cobaohieu/38f33c0b877c35f552c4a9288c272803 to your computer and use it in GitHub Desktop.
Save cobaohieu/38f33c0b877c35f552c4a9288c272803 to your computer and use it in GitHub Desktop.
update-alternatives for python2 and python3 in Ubuntu 18.04.x
  • Run the following commands as root or user with sudo access to update the packages list and install the prerequisites:
$ sudo apt update
$ sudo apt install software-properties-common
  • Add the deadsnakes PPA to your system’s sources list:
$ sudo add-apt-repository ppa:deadsnakes/ppa -y
  • When prompted press Enter to continue:
  Press [ENTER] to continue or Ctrl-c to cancel adding it.
  • Once the repository is enabled, install Python 3.8 with:
$ sudo apt install python3.8
  • Verify that the installation was successful by typing:
$ python3.8 --version
Python 3.8.10

List available python options

$ ls -larth `which python`*
lrwxrwxrwx 1 root root   16 Thg 4 16  2018 /usr/bin/python-config -> python2.7-config
lrwxrwxrwx 1 root root   16 Thg 4 16  2018 /usr/bin/python2-config -> python2.7-config
lrwxrwxrwx 1 root root    9 Thg 4 16  2018 /usr/bin/python2 -> python2.7
lrwxrwxrwx 1 root root   17 Thg 1 25  2018 /usr/bin/python3m-config -> python3.6m-config
lrwxrwxrwx 1 root root   10 Thg 1 25  2018 /usr/bin/python3m -> python3.6m
lrwxrwxrwx 1 root root   16 Thg 1 25  2018 /usr/bin/python3-config -> python3.6-config
lrwxrwxrwx 1 root root   35 Thg 1 26 22:33 /usr/bin/python3.6m-config -> aarch64-linux-gnu-python3.6m-config
-rwxr-xr-x 2 root root 4,4M Thg 1 26 22:33 /usr/bin/python3.6m
lrwxrwxrwx 1 root root   34 Thg 1 26 22:33 /usr/bin/python3.6-config -> aarch64-linux-gnu-python3.6-config
-rwxr-xr-x 2 root root 4,4M Thg 1 26 22:33 /usr/bin/python3.6
-rwxr-xr-x 1 root root 4,8M Thg 2 26 05:10 /usr/bin/python3.8
lrwxrwxrwx 1 root root   34 Thg 2 27 22:10 /usr/bin/python2.7-config -> aarch64-linux-gnu-python2.7-config
-rwxr-xr-x 1 root root 3,2M Thg 2 27 22:10 /usr/bin/python2.7
lrwxrwxrwx 1 root root   24 Thg 5 27 13:42 /usr/bin/python -> /etc/alternatives/python
lrwxrwxrwx 1 root root   25 Thg 5 27 13:43 /usr/bin/python3 -> /etc/alternatives/python3

We have 3 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 27
$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6 36
$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 38

$ 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.8   38        auto mode
* 1            /usr/bin/python2.7   27        manual mode
  2            /usr/bin/python3.6   36        manual mode
  3            /usr/bin/python3.8   38        manual mode

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

Check the version of python

$ python -V
Python 2.7.17

update-alternatives for python3

$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 36
$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 38

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

  Selection    Path                Priority   Status
------------------------------------------------------------
* 0            /usr/bin/python3.8   38        auto mode
  1            /usr/bin/python3.6   36        manual mode
  2            /usr/bin/python3.8   38        manual mode

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

Check the version of python3

$ python3 -V
Python 3.8.10

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