25

I would like to set python 3.8 as default on my PC Thinkpad X230 Ubuntu 20.04

I tried setting an alias

gt@gt-ThinkPad-X230:~$ alias python='usr/bin/python3.8'

Q: Does this alter a .bashrc file? If so, which? ~/.bashrc? another? if so, which?

gt@gt-ThinkPad-X230:~$ python --version
bash: usr/bin/python3.8: No such file or directory

Complains it cannot find /usr/bin/python3.8, buuuuut:

gt@gt-ThinkPad-X230:~$ ls /usr/bin/python*

/usr/bin/python /usr/bin/python3.8 /usr/bin/python3-pasteurize /usr/bin/python2 /usr/bin/python3.8-config /usr/bin/python3-unidiff /usr/bin/python2.7 /usr/bin/python3-config /usr/bin/python3 /usr/bin/python3-futurize

How do I get bash to find see /usr/bin/python3.8?

4 Answers 4

43

The correct way is sudo apt install python-is-python3 - it effectively does a symlink, but it also keeps pace with future updates; so if your ubuntu distribution moves to say Python 3.9, the manual symlink will no longer work, but the package makes sure it is still valid.

3
  • Thanks! I'll look into this when I next upgrade python! Sep 7, 2020 at 0:09
  • I did sudo apt install python-is-python3, but nothing changed. What are the next steps? No man entry either
    – Abdull
    Feb 26 at 18:31
  • @Abdull inspect your environment. As I wrote, it does not do much more then install a symlink, so perhaps your PATH is messed up, or the binary that the symlink points to has been changed, there can be bunch of other problems. If you are at your wits end, I suggest you open a new question.
    – volferine
    Feb 28 at 14:35
13

Firstly to answer your question, your approach should work, I think the path you've given in your alias needs the / preceding the path so the command should be alias python='/usr/bin/python3.8', this would indeed need to go into your ~/.bashrc file assuming you are using bash.

Secondly, Ubuntu has a really nice method of setting default binaries globally rather than messing with dot config files as depicted here: update-alternatives

a better solution may be to simply run:

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

This will ensure you have the version of python in use that you intend, everywhere.

5
  • Thanks! update-alternatives looks great, but perhaps a bigger gun than I need for a one-man laptop. Sep 7, 2020 at 0:09
  • 4
    I just tried this (in my case setting 'python' to '/usr/bin/python3' on ubuntu20.04, but got the following error: 'update-alternatives: error: no alternatives for python'. Any idea what I'm doing wrong?
    – Max Power
    Dec 17, 2020 at 17:44
  • @dagmarPrime update-alternatives is exactly what you need, as your aliases won't get called in scripts. python can and should point to python 3 for most purposes these days and update-alternatives is the right way to do that. Mar 4, 2021 at 10:52
  • 3
    @MaxPower You're correct; this does not work on Ubuntu 20.04 LTS, for the reason you describe. But sudo apt install python-is-python3 does, per another answer, askubuntu.com/a/1272899/379076 . Apr 2, 2021 at 15:30
  • After updating from 16 Xenial to 20.04.3 (via 18) alternatives seems to be a little wonky. alternatives says the symlink is pointing to python3.9 but it isn't. update-alternatives --display python3 link currently points to /usr/bin/python3.9 link python3 is /usr/bin/python3 But... ls -l /usr/bin/python3 gives lrwxrwxrwx 1 root root 9 Mar 13 2020 /usr/bin/python3 -> python3.8 ...ie pointing at 3.8. update-alternatives --set python3 /usr/bin/python3.9 shows update-alternatives: warning: forcing reinstallation ... because link group python3 is broken.
    – mattpr
    Nov 10, 2021 at 8:25
8

Check the installed versions of Python:

ls /usr/bin/python*

Then, create the alternatives:

sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 2

Then, choose the version you want:

sudo update-alternatives --config python

You can easily switch between default Python versions.

1
  • 1
    We recommend against creating new alternatives in /usr/bin/ that the OS hasn't provided for you - it may well cause problems on future upgrades. Somebody else has already pointed to the python-is-python3 package, which is better in this case. Feb 19, 2021 at 21:55
4

You should be able to do it in a command shell by typing:

alias python=python3.8

To make it permanent you need to open up ~/.bashrc and add that line to the end of it. Should be as simple as that! Keep in mind this only works on a per user basis, which may or may not be what you want.

The other other thing that I notice with your attempt, is that your missing the leading /, so it should be reading as:

alias python='/usr/bin/python3.8'

without that leading forward / it may be trying to use a relative path.

1
  • 4
    this is not the solution Feb 5, 2021 at 14:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.