43

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 Answers5

62

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
  • 881
19

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.

17

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.

8

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
  • 259
4

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.