How do I change my default python 3 having more than one version installed (Ubuntu 18.04)?

Viewed 8969

When I type python --version in my terminal, it shows Python 3.8.5, but when I type python3 --version, it shows Python 3.6.9. I want to create a virtual environment using python3 -m venv .venv with the version 3.8.5, but because of the fact that my default python3 version is the 3.6.9, it's creating a virtual environment using 3.6.9 as it's version. How do I change my default python3 version?

2 Answers

The python and python3 commands are usually soft links to the actual executables and you can change the targets. For example:

Firstly, find out where python 3.6 and python 3.8 are located:

# which python
/usr/bin/python
# ls -l /usr/bin/python
/usr/bin/python -> python3.8
# which python3
/usr/bin/python3
# ls -l /usr/bin/python3
/usr/bin/python3 -> python3.6

Then, change the soft links:

# rm /usr/bin/python3
# ln -s /usr/bin/python3.8 /usr/bin/python3

Run this Command in your terminal

sudo update-alternatives --config python

You'll get choice prompt, enter selection number of your desired python version.

But if this shows an error like this: update-alternatives: error: no alternatives for python3

Then you have to update your update-alternatives, then you will be able to set your default python version.

sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6.9
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8.5

Now run below command to set default python

sudo update-alternatives  --set python /usr/bin/python3.8
Related