Saurabh Srivastava | ibcoder.eth
IBCoder | Saurabh Srivastava

IBCoder | Saurabh Srivastava

Installing Multiple Python Version in WSL2

Installing Multiple Python Version in WSL2

Saurabh Srivastava | ibcoder.eth's photo
Saurabh Srivastava | ibcoder.eth
·Nov 3, 2020·

5 min read

Subscribe to my newsletter and never miss my upcoming articles

Since a month or so, I am constantly focusing on starting my YouTube Coding Channel which is finally online from November 1, 2020. Starting this channel, I learnt the difference between truly absorbing knowledge from documentations, videos, and other platform and thinking that you have learnt what is being taught. But that’s completely philosophical discussion for another time.

In this article, I will tell you about update-alternatives command in Ubuntu WSL2. But before that, here’s how I ended up with needing to work with python.

So, I wasn’t feeling well with my allergy today, resulting in unintended abs workout — sneezing. Finally getting some relief, I thought why not do something quick and interesting. So, I searched and came across this interesting tutorial on Google Translate by Ayushi Rawat. And I was like, “Ab yehi karunga” [Now I will do this only].

So, like every other person who watches an interesting tutorial, I hopped on to coding while watching to see the quick result on my system. But dang! my system didn’t had pip. Also, there was default python 3.8.5 in WSL2 Ubuntu 20.4 but I desired the latest one. One thing led to another and I ended up with 3 python versions in my WSL2:

  1. Python2.7
  2. Python 3.8
  3. Python 3.9

Now the problem was to be able to use them interchangeably based on my needs in future. Yes, I completely forgot about the real problem of not having pip. So, here’s how I solved it using update-alternatives command.

The first thing you would do, if you are not getting the desired python version on typing python on your terminal is search for its path:

$ > whereis python

This will output some paths as shown in the above image. Now next, you will note the unique paths to your versions like:

  1. /usr/bin/python2.7
  2. /usr/bin/python3.8
  3. /usr/bin/python3.9

Next, you will use update-alternatives command to list out our alternatives present within update-alternatives for python:

$ > sudo update-alternatives --list python

This will give the following result if this is the first time you are setting it up:

$ > update-alternatives: error: no alternatives for python

So, we have to add the alternatives to our list. We do that by following commands:

$ > sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
$ > sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 2
$ > sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.9 3

Let’s break apart that command:

  1. — install: It’s an install flag used to add a list of alternatives to our update-alternatives list.
  2. /usr/bin/python: This second parameter is the symlink path. Basically, it will be same for all the 3 listed python versions of ours.
  3. python: This is the symlink name by which we want to call our symlink path which will instead invoke the python version which we will be giving next.
  4. /usr/bin/python2.7: This is the real path of python2.7 in our system. Or the real path executable [Linux version of the same].
  5. 1: This is the priority of the python version. Higher the number, higher the priority.

Finally, let’s see the list once again:

Image for post

Now to choose the desired version, you will run:

Image for post

Here, you will choose the python version by typing the number and pressing enter. You can let it be 0 for it to choose the highest priority version itself.

Finally running python we get:

Image for post

We get the latest version. Now, back to that interesting tutorial on Google Translate.

 
Share this