Switch between multiple versions of python3

You can have multiple versions of python 3 installed on your Ubuntu machine, and switch between the versions. This presents nice flexibility to the users. You can potentially have the latest version installed, and switch to the prior version easily!

The following steps will enable you to switch between 3.6, 3.7 and 3.8 version of python on a ubuntu machine.

Step 1: Check existing version

$ python3 --version
Python 3.6.9

Step 2: Run apt update

$ sudo apt update -y

Step 3: Install python 3.7

$ sudo apt install python3.7

Step 4: Install python 3.8

$ sudo apt install python3.8

Step 5: Use “update-alternatives” enable the switch flexibility

$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 2
$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 3

Step 6: Now, you can switch between the python version by executing the following command, and picking the one you desire.

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

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

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

Step 7: Test the version by running the following command

$ python3 --version
Python 3.7.5    (Note: This depends on the version installed, and picked)