How to set default python3 to python 3.9 instead of python 3.8 in Ubuntu 20.04 LTS

Question:

I have installed Python 3.9 in the Ubuntu 20.04 LTS. Now the system has both Python 3.8 and Python 3.9.

# which python
# which python3
/usr/bin/python3
# which python3.8
/usr/bin/python3.8
# which python3.9
/usr/bin/python3.9
# ls -alith /usr/bin/python3
12583916 lrwxrwxrwx 1 root root 9 Jul 19  2021 /usr/bin/python3 -> python3.8

But the pip3 command will still install everything into the Python 3.8 directory.

# pip3 install --upgrade --find-links file:///path/to/directory <...>

I want to change that default pip3 behavior by updating the symbolic link /usr/bin/python3 to /usr/bin/python3.9.

How to do that?

# update-alternatives --set python3 /usr/bin/python3.9
This command will not work as expected.

Here is the pip3 info:

# which pip3
/usr/bin/pip3
# ls -alith /usr/bin/pip3
12589712 -rwxr-xr-x 1 root root 367 Jul 13  2021 /usr/bin/pip3
# pip3 -V
pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)
# 

The alias command will not work:

# alias python3=python3.9
# ls -alith /usr/bin/python3
12583916 lrwxrwxrwx 1 root root 9 Jul 19  2021 /usr/bin/python3 -> python3.8
Asked By: stackbiz

||

Answers:

You should be able to use python3.9 -m pip install <package> to run pip with a specific python version, in this case 3.9.

The full docs on this are here: https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/

If you want python3 to point to python3.9 you could use the quick and dirty.

alias python3=python3.9

EDIT:

Tried to recreate your problem,

# which python3
/usr/bin/python3
# python3 --version
Python 3.8.10
# which python3.8
/usr/bin/python3.8
# which python3.9
/usr/bin/python3.9

Then update the alternatives, and set new priority:

# sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 1
# sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2
# 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.9   2         auto mode
  1            /usr/bin/python3.8   2         manual mode
* 2            /usr/bin/python3.9   2         manual mode

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

Check new version:

# ls -alith /usr/bin/python3
3338 lrwxrwxrwx 1 root root 25 Feb  8 14:33 /usr/bin/python3 -> /etc/alternatives/python3
# python3 -V
Python 3.9.5
# ls -alith /usr/bin/pip3
48482 -rwxr-xr-x 1 root root 367 Jul 13  2021 /usr/bin/pip3
# pip3 -V
pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.9)

Hope this helps (tried it in wsl2 Ubuntu 20.04 LTS)

Answered By: MrWorldWide

Changing Python version:

First we need to check what are the versions in Python using this command: sudo ls /user/bin/python*

And then need to use this command to symlink them:

sudo ln -sf /user/bin/python3.9 /user/bin/python3 

When you use that command, it will use python3.9 as the default python3.

Answered By: sanjeev
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.