Linux is all about freedom. While proprietary operating systems tend to point you towards a default for each type of app (e.g. web browser), Linux provides the tools to tailor your system to your own tastes. You should be able to have as many different programs of a type as you like. And that’s what update-alternatives is all about — an easy way to switch between options.
Here’s how to use it in Ubuntu (and other Debian-based systems).
The update-alternatives System
Before we delve into the different alternatives, we’ll take a look at things behind the scenes. One alternative, editor, provides a terminal-based text editor:
whereis editor editor: /usr/bin/editor /usr/share/man/man1/editor.1.gz
And opening a text file with this command does exactly what you’d expect:
sudo editor /etc/fstab
This will open the filesystem config file in a text editor… but which one? On the below system it opened in nano:
The editor command is in fact a symbolic link (symlink). A link to nano, you might ask? Nope! The command /usr/bin/editor is a link to /etc/alternatives/editor. The /etc/alternatives directory is where all the alternatives in the system are managed as symbolic links. These are the links that point to the actual program in question. So it’s a link (in your PATH) to a link (in the alternatives directory) to a program.
As you might have guessed, update-alternatives is the tool that helps you manage these links. Let’s take a look at how to use it.
Example update-alternatives Usage
Most update-alternatives commands you’ll be using follows this pattern:
sudo update-alternatives [option] [alternative(s)]
In the above, alternative(s) refers to the program you’ll end up using. The option is what you want to do with it. To continue with the editor component, before we go mucking around let’s get the lay of the land. The display option shows us some detail.
update-alternatives --display editor
The top lines tell us the path to the editor command itself, as well as what’s linked at the moment. There’s a lot of stuff in there though, including man page translations and such. A more focused list command makes things easier to understand:
update-alternatives --list editor
There, you can see that nano is indeed listed as an alternative for editor. But what else could we use? This gives us three options, including VIM. You can re-assign editor to call the VIM program instead with the config option.
sudo update-alternatives --config editor
Using the interactive menu, you can select a new option. Or if you know the program you want, use the set option:
sudo update-alternatives --set editor /usr/bin/vim.basic
Next we’ll take a look at at a few alternatives you may want to try your hand on.
Notable update-alternative Options
As mentioned, there are already many packages that make use of the update-alternatives system. Here are some that will may prove useful in managing your system:
update-alternatives --config java
Some programs expect/require a certain version of Java to be installed. On Ubuntu-based systems, you can install multiple versions of the OpenJDK (open source Java) from repositories as well as install multiple versions of the official Oracle JRE by hand. Setting up the latter as alternatives will allow you to switch which Java environment launches programs on the fly.
Note: There are a number of Java-related alternatives that should all be updated together. Check out the convenience app update-java-alternatives, which does some of the Java-specific work for you.
update-alternatives --config x-www-browser/gnome-www-browser
Pretty self-explanatory, this will allow you to set your default web browser. If you work in a GNOME-based desktop, you should also look at gnome-www-browser too.
update-alternatives --config mozilla-flashplugin
If you’re curious about the full range of alternatives available, try the following to list them all along their current setting:
Customizing Your Alternatives
Managing the options Canonical gives us is all well and good. But it wouldn’t be freedom if you couldn’t make it your own, would it? In the following sections we’ll look at how to add and remove your own alternative groups.
Adding Alternatives From the System
Alternatives are added to your system automatically when you install supported packages. For example, if you installed emacs, the installation process would run a script that creates the necessary option in /etc/alternatives, including a priority.
But you can create your own alternatives as well, if you’re adventurous enough. If you do, you should bear in mind that you’ll need to populate these alternatives manually. For example, if you create a new alternative titled x-word-processor, you’ll need to add the first and all subsequent programs by hand. You’ll also need to remove them, otherwise you may find your alternative pointing to a program that no longer exists.
Let’s add a group called x-word-processor and one alternative (in this case the excellent LibreOffice Writer ) is as follows:
sudo update-alternatives --install /usr/bin/word-processor x-word-processor /usr/bin/lowriter 40
This command creates:
- A new command (in fact a symlink) called word-processor representing;
- A new alternatives group called x-word-processor, which;
- Contains (and defaults to) the application /usr/bin/lowriter, which has;
- A priority of 40.
Calling word-processor from the command line will now launch LibreOffice Writer (specified above as lowriter). You can add others (e.g. the text-mode word processor wordgrinder) with the same command, changing the real application’s path as needed:
sudo update-alternatives --install /usr/bin/word-processor x-word-processor /usr/bin/wordgrinder 20
Now querying the x-word-processor group will show these two options.
By default the group is in “auto” mode, which means the system will use option with the highest Priority (by number) it contains — in this case LibreOffice (40, versus wordgrinder’s 20). You can use the config option described above to change this.
Removing Alternatives From the System
If you decide that you don’t need an option, a simple command using the remove option will get rid of it.
sudo update-alternatives --remove x-word-processor /usr/bin/wordgrinder
Finally, the remove-all option will delete the entire group, including all of its alternatives:
sudo update-alternatives --remove-all x-word-processor
Note that these remove the update alternatives entries but not the programs to which they are linked.
Have you ever update-alternatives in action before? Any tips or tricks related to working with alternatives? Let us know in the comments!
Image Credit: momente via Shutterstock.com