Home > other >  Permanently change the default Python3 version in Linux (Ubuntu on Windows)
Permanently change the default Python3 version in Linux (Ubuntu on Windows)

Time:05-29

I'm using WSL2 with Ubuntu on Windows 11 v2004.2022.10 and I have both Python 3.8 and 3.9 installed. I want to make the 3.9 version the default, and I'm happy to remove Python 3.8 altogether if necessary.

If I type python --version in Ubuntu, I get Python 3.8.10.

I tried the following:

sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.9 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 0

and if I type sudo update-alternatives --config python

I see

There are 2 choices for the alternative python (providing /usr/bin/python).

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

However, if I type again python3 --version, it still says Python 3.8.10

I then tried sudo update-alternatives --remove python /usr/bin/python3.8 and now sudo update-alternatives --config python tells me that There is only one alternative in link group python (providing /usr/bin/python): /usr/bin/python3.9 Nothing to configure.

And yet, python3 --version still says Python 3.8.10

I also tried sudo update-alternatives --set python /usr/bin/python3.9 and that didn't work either.

This works: alias python='/usr/bin/python3.9': now python3 --version is Python 3.9.5 - but only temporarily, as upon closing and reopening Ubuntu it reverts to Python 3.8.10.

I then tried creating a permanent alias by adding that same line to my .bashrc script (I followed these steps), and the same thing happened.

I'm new to all of this, so please be patient.

How can I change the default Python 3.8 to the 3.9 version, and/or remove Python 3.8 altogether? I tried deleting the python3.8 directory but that didn't work. Perhaps it's because I still have python3.8-config, which I didn't manage to delete?

Thanks!

CodePudding user response:

I managed to solve it! I had to do add alias python3='/usr/bin/python3.9' with no spaces, and including the number 3, to my bash script, following the video I linked above. And then I had to close and re-open the terminal - typing clear was not enough.

I'd love to hear some explanations on why the sudo update-alternatives commands didn't work though!

  • Related