Update alternatives python install

How can I change the default python on my Ubuntu 20.04 to Python3.8?

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

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.

I did sudo apt install python-is-python3 , but nothing changed. What are the next steps? No man entry either

@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.

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.

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?

@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.

@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 .

Читайте также:  JavaScript Ajax GET Demo

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 .

Источник

How to Use update-alternatives Command on Ubuntu

If you have 2 or more versions of the same command, you can use update-alternatives to set which one to use by default and also switch between them very easily. For example, let’s say, you have Python 2 and Python 3 installed on your computer. Now, you have to run a Python 2 script as follows:

Here, is the path of the Python 2 script i.e. ~/scripts/hello.py

The same way, you have to run a Python 3 script as follows:

Here, is the path of the Python 3 script i.e. ~/scripts/hello2.py

So, what if you want to access both Python 2 and Python 3 using the same command python?

Well, using update-alternatives command, you can make a new executable python (/usr/local/bin/python) and add all the available Python versions to the alternatives database. Then, you can easily set which version of Python to use by default. You can also switch between the Python versions very easily. I am going to show you how to do this practically in this article. That way, you will gain practical knowledge of how update-alternative works.

Prerequisites:

You must have Python 2 and Python 3 installed on your Ubuntu machine if you want to follow along and try out the examples in this article.

You can install Python 2 and Python 3 with the following command on Ubuntu:

Python 2 and Python 3 should be installed if not installed already. In my case, they are already installed.

Now, find the Python 2 and Python 3 interpreter path with the following commands:

As you can see, the Python 2 interpreter path is /usr/bin/python2 and Python 3 interpreter path is /usr/bin/python3. Memorize them. We will need this later.

Installing New Alternatives:

Now, you can create a new alternatives python and install Python 2 interpreter /usr/bin/python2 as an alternative with the priority 20 as follows:

Читайте также:  div class and style

NOTE: Here, /usr/local/bin/python is the binary path of the python alternatives. You can change it to some other path such as /usr/bin/python if you want. But I would suggest you place it somewhere in the /usr/local/bin/ directory as this is the directory where user-space programs should be according to the directory structure of Linux.

A new alternatives python should be created and Python 2 interpreter should be added there as an alternative.

The same way, install Python 3 interpreter /usr/bin/python3 as an alternative of the python alternatives and set it a priority of 40 with the following command:

Python 3 interpreter alternative should be added to the python alternatives.

Now, you can see more information about the python alternatives you’ve just created as follows:

As you can see, the python alternatives has 2 alternative commands /usr/bin/python2 (priority 20) and /usr/bin/python3 (priority 40).

Auto Mode and Alternative Priority:

Every alternatives in the update-alternatives database has 2 modes: auto and manual.

In auto mode, the alternatives selects the command/executable/alternative with the highest available priority by default.

In manual mode, you select the default command/executable/alternative for the alternatives manually.

The default mode for any alternatives is auto.

In the earlier section, you’ve seen I’ve set the priority for Python 2 interpreter (/usr/bin/python2) alternative 20 and for Python 3 interpreter (/usr/bin/python3) alternative 40. This was because I wanted to set Python 3 interpreter as the default alternative for the python alternatives. As the default mode is auto, the python alternatives selected the Python 3 interpreter alternative automatically as it has higher priority than Python 2 interpreter alternative.

As you can see, the mode (Status) is auto. The Python 3 interpreter (/usr/bin/python3) is the Best one as it has the highest priority. Also, the currently selected one is also the Python 3 interpreter (/usr/bin/python3) as it has the highest priority and the mode is auto.

As you can see, python command executes the Python 3 interpreter.

In the next section of this article, I will show you how to change the alternatives mode to manual and select a lower priority command/executable for the alternatives as well.

Changing Alternatives Command Manually:

As I have said, Python 2 won’t be automatically selected as I have set it to a lower priority. So, if you want to select a lower priority alternative, you have to do that manually.

Читайте также:  Шаблоны соц сетей html

To select Python 2 alternative manually for the python alternatives, run the following command:

Now, update-alternatives will list all the commands/alternatives you have installed for the python alternatives. In my case, it’s Python 2 and Python 3 alternatives.

You also have a Selection number for each version/alternative which you can use to select a version/alternative from the list. Selection number 0 is auto. Selection number other than 0 is manual.

Here, Selection number 1 is for Python 2 alternative and Selection number 2 is for Python 3 alternative.

So, to select Python 2 alternative, press 1 and then press .

Python 2 should be set as the default alternative for the python alternatives.

As you can see, the current mode is manual. Even though the Best alternative is /usr/bin/python3 (Python 3), it’s using /usr/bin/python2 (Python 2).

As you can see, python command executes the Python 2 interpreter now.

Settings Alternatives Mode to Auto:

If you want to switch to auto mode for the python alternatives again, run the following command:

Based on the priority, an alternative should be set for the python alternatives. In this case, the Python 3 alternative.

As you can see, python command executes the Python 3 interpreter.

Removing Alternative from Alternatives:

You can remove an alternative from the python alternatives.

First, list all the available alternative for the python alternatives as follows:

Now, select and copy the path of the alternative you want to remove. I am going to remove the /usr/bin/python2, the Python 2 interpreter here.

To remove the /usr/bin/python2 (Python 2 interpreter) alternative from the python alternatives, run the following command:

As you can see, the /usr/bin/python2 (Python 2 interpreter) alternative is removed from the python alternatives.

Removing All Commands from Alternatives:

You can also remove all the available alternatives from the python alternatives with the following command:

As you can see, there are no alternatives for python any more.

So, that’s how you use update-alternatives command on Ubuntu to switch between different versions of the same program or command very easily. Thanks for reading this article.

About the author

Shahriar Shovon

Freelancer & Linux System Administrator. Also loves Web API development with Node.js and JavaScript. I was born in Bangladesh. I am currently studying Electronics and Communication Engineering at Khulna University of Engineering & Technology (KUET), one of the demanding public engineering universities of Bangladesh.

Источник

Оцените статью