32

I have installed Python 3.9 in 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 -l /usr/bin/python3
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 -l /usr/bin/pip3
-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 -al /usr/bin/python3
lrwxrwxrwx 1 root root 9 Jul 19  2021 /usr/bin/python3 -> python3.8
6
  • stackoverflow.com/questions/42871090/… but use the correct pip instead of 2.7 Commented Feb 8, 2022 at 12:45
  • Thank you. I have added the pip3 info in the post. But I still don't know how to do that, can you help? Commented Feb 8, 2022 at 12:50
  • 1
    An easier option is to run pip "correctly" via the python command itself: python -m pip install ... Commented Feb 8, 2022 at 12:52
  • The "python3.9 -m pip install ..." will have problem when I added the pip3 option "--find-links file:///path/to/directory", it won't recognize the /path/to/directory at all. But if I use the above "pip3 install .. -find-links file:///path/to/directory", then the pyhton 3.8 can recognize it correctly. So I need to change the default python 3.8 version to python 3.9. and then use the pip3 as usually.' Commented Feb 8, 2022 at 12:57
  • Gotcha. The problem is that you also need to change the pip executable symlink as I understand you've done for the python executable. Commented Feb 8, 2022 at 13:05

3 Answers 3

28

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 -l /usr/bin/python3
lrwxrwxrwx 1 root root 25 Feb  8 14:33 /usr/bin/python3 -> /etc/alternatives/python3
# python3 -V
Python 3.9.5
# ls -l /usr/bin/pip3
-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)

Sign up to request clarification or add additional context in comments.

7 Comments

This command will not work "alias python3=python3.9", after executing the command, the "ls" still shows the python 3.8. I have added the alias command output in the post. Please help to check.
alias python3=python3.9 works for me on centos7
The alias command will work, if the python3.9 is the only python version installed. If python3 (v3.8) is already installed, the alias won't work
"Then update the alternatives" - Are you proposing that as a solution? Cause it's not. Different versions of Python are not alternatives on Ubuntu (or Debian). Don't do that; there's a reason alternatives isn't configured by default for Python.
Your alteration of the answer was unnecessary and does not provide any value. My answer worked and has helped to solve the problem. If you have any valuable solutions which is better you should add it as an answer and not only leave comments on others answers, saying they are bad, and not providing any value.
"Your alteration of the answer was unnecessary and does not provide any value." - How so? I improved the formatting and removed irrelevant details.
"you should add it as an answer" - I actually closed the question as a duplicate and the answer there is "don't", which is what I would say myself (cf an earlier answer of mine).
13

Changing Python version:

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

And then need to use this command to symlink them:

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

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

3 Comments

I got Operation not permitted on my mac.
for me on centos 7, after compiling python3.9 is installed in /usr/local/bin e.g. sudo ln -sf /usr/local/bin/python3.9 /usr/bin/python3
Don't do this; it'll break things, e.g. gnome-terminal.
0

It’s NOT recommended to change non-preinstalled Python package as default for Python3, since it will break some core applications. So I like to use update-alternatives

However, you may try to set python3.9 as python3 by running the commands below one by one:

  • First, run command to find out where Python 3.12 executable is installed to:

    whereis python3.9
    

    It’s either /usr/bin/python3.9 or /usr/local/bin/python3.9.

  • Then, add Python 3.9 as an alternative link to python (replace /usr/local/bin/python3.9 according last command output).

    sudo update-alternatives --install /usr/bin/python3 python /usr/local/bin/python3.9 1
    
  • Finally, run command to set default for /usr/bin/python3 executable if more then one available:

    sudo update-alternatives --config python3
    

1 Comment

Huh??? You're right that it's not recommended, so why are you giving instructions on how to do it???

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.