37

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?

dagmarPrime
  • 485
  • 1
  • 4
  • 6

4 Answers4

53

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.

volferine
  • 791
  • 1
  • 4
  • 5
  • Thanks! I'll look into this when I next upgrade python! – dagmarPrime Sep 07 '20 at 00:09
  • 1
    I did `sudo apt install python-is-python3`, but nothing changed. What are the next steps? No man entry either – Abdull Feb 26 '22 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 '22 at 14:35
15

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.

Sayed H Fatimi
  • 346
  • 2
  • 6
  • Thanks! update-alternatives looks great, but perhaps a bigger gun than I need for a one-man laptop. – dagmarPrime Sep 07 '20 at 00:09
  • 5
    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 '20 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. – mikemaccana Mar 04 '21 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, https://askubuntu.com/a/1272899/379076 . – Ben Johnson Apr 02 '21 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 '21 at 08:25
11

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.

NoirHirsute
  • 121
  • 1
  • 3
  • 2
    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. – Colin Watson Feb 19 '21 at 21:55
7

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.

Nebri
  • 249
  • 1
  • 9