39

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?

5 Answers 5

54

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.

4
  • Thanks! I'll look into this when I next upgrade python! Sep 7, 2020 at 0:09
  • 2
    I did sudo apt install python-is-python3, but nothing changed. What are the next steps? No man entry either
    – Abdull
    Feb 26, 2022 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, 2022 at 14:35
  • This ensures that the command python invokes python3. It does not run python3.8 specifically unless that is the default version of python3. If you have multiple versions of python3.x installed you may need to use update-alternatives to switch between them or set a default. Jan 24 at 16:26
16

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, so 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.

6
  • Thanks! update-alternatives looks great, but perhaps a bigger gun than I need for a one-man laptop. Sep 7, 2020 at 0:09
  • 7
    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
14

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.

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. Feb 19, 2021 at 21:55
  • Personal preference, since I have no use for python2: use the python-is-python3 package to ensure that python always invokes python3. However I do have multiple versions of python3.x installed, so adapting this answer to install each of those as alternatives allows me to set a specific version of python3 as the default: sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 8, etc. Jan 24 at 16:36
  • Thanks. This works for me since I have a few python versions. Feb 29 at 12:00
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.

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

Here is an answer that I hopes differentiate between using the python-is-python3 and update-alternatives options described in different answers. At first I believed that using BOTH of these options gave me an optimal solution, after testing this for a short while I realized I had a problem.

I am using Ubuntu 20.04.6 LTS (Focal Fossa), running under the Windows Subsystem for Linux (WSL) on Windows 10.

My first observation is that I didn't have python2 installed at all; I did have python3.8, provided as the default "system" Python 3 package for this distro. I noticed that the python command didn't work:

$ python --version

Command 'python' not found, did you mean:

  command 'python3' from deb python3
  command 'python' from deb python-is-python3

I solved this by installing python-is-python3, which creates symlinks in /usr/bin/:

$ ls -l /usr/bin/python*
lrwxrwxrwx 1 root root       7 Apr 15  2020 /usr/bin/python -> python3
lrwxrwxrwx 1 root root       9 Mar 13  2020 /usr/bin/python3 -> python3.8

This may be enough, but I have multiple versions of Python 3 installed for... reasons. I wanted to be able to choose between different versions of Python 3 so the update-alternatives package was useful:

$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 8
$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 10

The effect of this was (1) to make python3.10 the default on my system, since it has the highest priority--the number at the end--and (2) allows me to switch the system-wide version of python via:

$ 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.10   10        auto mode
  1            /usr/bin/python3.10   10        manual mode
  2            /usr/bin/python3.8    8         manual mode

Note that this is using python3 as the command/alias.


Note: I don't recommend using update-alternatives to switch (frequently) between different default versions of Python 3.x for your projects. It is better to use virtual environments within each project, installed using an explicit version of Python, e.g.

$ python3.8 -m venv .venv

UPDATE

Since using the approach described above I have discovered a problem: when trying to upgrade my disto using apt-get update, etc. I saw problems such as this: ModuleNotFoundError: No module named 'apt_pkg'.

The cause was that I had changed my default version of python3 to be python3.10 and this was incompatible with many system scripts in Ubuntu 20.04, which expected python3.8.

Thus I have reverted my default to be python3.8 and will explicitly use python3.10 to create venvs for my projects.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .