Linux update alternatives install

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:

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

Читайте также:  Как установить linux на телефон с android

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.

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:

Читайте также:  Уменьшить размер swap linux

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.

Источник

What exactly does `update-alternatives` do?

What does it do? How is it different from adding jdk to the path? Let’s say that I have run the command. How would I revert back to the original state?

You can also use sudo update-alternatives —config java and javac and javaws to choose between installed versions.

4 Answers 4

It updates the links in /etc/alternatives to point to the program for this purpose. There’s lots of examples, like x-www-browser , editor , etc. that will link to the browser or editor of your preference. Some scripts or system tools may want you to edit a file manually (e.g. configuration conflict in dpkg ) and they’ll look into the alternatives to give you the editor of choice. For java , this is the Java runtime environment — Oracle’s, OpenJRE, etc.

The links in /etc/alternatives are just symbolic links. You can see them using for example

Moreover, the regular /usr/bin binaries are also symlinks. E.g.:

ls -l /usr/bin/java lrwxrwxrwx 1 root root 22 Aug 14 10:33 /usr/bin/java -> /etc/alternatives/java ls -l /etc/alternatives/java lrwxrwxrwx 1 root root 46 Aug 14 10:33 /etc/alternatives/java -> /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java 

So, no PATH has to be modified. It just uses symbolic links.

So in other words I can type in a single command and it will prompt me for choices. That is the only added advantage over setting a path right. Also lets say I have a different vendor of java also installed How would I update java to point to both the two different vendors?

You can’t point to both like you describe. The whole purpose of it is to have a single pointer to the implementation of the entry as in the example given. Without questions. And any of the candidates should work — it should just be a preference of the administrator. And again: it has nothing to do with PATH s.

Читайте также:  Linux rdp terminal client

Okay. That clears things up. I know I am coming back to paths but I just want to understand the difference between them . In case of a path we have to explicitly specify the location but in this case it just sym links so that when we type java it searches /etc/alternatives first. Or am i wrong in making this assumption?

While @gertvdjik’s answer is a good explanation of how alternatives work at the lower level, it doesn’t explicitly say how to revert the original state.

I find it easier to use the corresponding GUI galternatives which is available as a package. To install it, just run:

sudo apt-get install galternatives 

Then managing alternatives becomes much easier. For java in particular, you have a lot of auxiliary binaries which you’ll have to update and it’s faster to overview them in the GUI.

And update-alternatives can create a group to switch easily. For example javaw , java and javac needs to go together if java switches. Then javaw and javac should be installed as slaves of java . Or oc and kubectl (openshift client and kubernetes control) should go together, too. I have installed oc v3 and v4, and each version goes with its own kubectl . So I do:

sudo update-alternatives --install /usr/bin/oc oc /opt/oc3/oc 2 --slave /usr/bin/kubectl kubectl /opt/oc3/kubectl sudo update-alternatives --install /usr/bin/oc oc /opt/oc4/oc 1 --slave /usr/bin/kubectl kubectl /opt/oc4/kubectl 

So oc3 and its kubectl are prioritized.

update-alternatives creates, removes, maintains and displays information about the symbolic links comprising the Debian alternatives system.

.

The generic name is not a direct symbolic link to the selected alternative. Instead, it is a symbolic link to a name in the alternatives directory, which in turn is a symbolic link to the actual file referenced. This is done so that the system administrator’s changes can be confined within the /etc directory .

As an example on Ubuntu 22.04, I can switch which Java I am using with sudo update-alternatives —config java :

$ sudo update-alternatives --config java There are 2 choices for the alternative java (providing /usr/bin/java). Selection Path Priority Status ------------------------------------------------------------ 0 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 auto mode 1 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 manual mode * 2 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode Press to keep the current choice[*], or type selection number: 

So to answer the original questions:

  • update-alternatives updates symbolic links
  • from context, it looks like you meant PATH, however it does not change PATH or JDK, e.g. from Java 11 to Java 8 directly
  • it has nothing like history built in, so in the general case it’s not possible to revert back to an original state. I’d expect you need to pick the most relevant alternative for the system state that works for you (in practice there’s a good chance that you only have two alternatives when you first encounter this, so if you recently installed a second alternative that broke something for you, there’s only one other to switch back to)

Источник

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