Background
Recently I needed to run the end to end tests in the older chrome browser as the tool the chrome driver available to me was only working with the older version of chrome.
The solution instead of downgrading my chrome installation was to install multiple versions of chrome-browser. More on this in my recent article how and where to get older versions of google-chrome.
Long story short, some of the command-line tools require or expect to be be able to interact with system version and need to be able to change it quickly and effortlessly and at best in command-line interface.
The same goes when installing multiple versions of java, node, pyhton etc. In java world people might be using sdkman
[www.sdkman.org] or node js world us used nvm
www.getnvm.org.
What is update-alternatives
update-alternatives is tool provided Debian based distributions which is designed to help to manage multiple versions or instances of the one command/program
Debian Alternatives. Update-alternatives in Debian based system like Ubuntu is used as default option by apt
, however, it offers the command-line interface to interact with the settings and to add and remove entries manually.
It can also provide functionality to update something what is called slave links.
This is e.g. handy when you have a version specific man page along the software version you want to add to your system.
It will tell update-alternatives to update also all associated links when you are changing the current version. The analogy in the nodejs world would be corresponding npm
installation or in python world it would be corresponding pip
. We also want to change these if we change the current version of python or node.
How to install another version with update-alternatives
if you plan to use update-alternavives to e.g. install the multiple node versions you will download the desired version and unpack it in the directory of the choice
In my case it would be /opt/google/chromium/1027016/chrome-linux/
this will contain binary /opt/google/chromium/1027016/chrome-linux/chrome
if you already have chrome
already installed and you did that from the Debian package by
sudo apt install google-chrome-stable.deb
your already have used update-alternatives
and running
update-alternatives --display google-chrome
will show output:
google-chrome - auto mode
link best version is /usr/bin/google-chrome-stable
link currently points to /usr/bin/google-chrome-stable
link google-chrome is /usr/bin/google-chrome
/usr/bin/google-chrome-stable - priority 200
indicates there is only one version of google-chrome and it shows the location and priority for auto mode (the higher version takes precedence)
I don’t want to go into too much detail here as it could be found in man pages or online debian alternatives
also running does similar, list the existing links
update-alternatives --list google-chrome
?> /usr/bin/google-chrome-stable
Investigating links further we get to:
/usr/bin/google-chrome --> /etc/alternatives/google-chrome
/etc/alternatives/google-chrome --> /usr/bin/google-chrome-stable
/usr/bin/google-chrome-stable --> /opt/google/chrome/google-chrome
The last one is hard link to executable file
you can now add the downloaded specific version to it by executing the following
sudo update-alternatives --install /usr/bin/google-chrome google-chrome /opt/google/chromium/1027016/chrome-linux/chrome
if you want to use this as default link you can add the priority number at the end, higher than existing 200, but if you don’t you can use the following to change the version system wide.
sudo update-alternatives --config google-chrome
output (depending on given priority nubmer this might differ):
There are 2 choices for the alternative google-chrome (providing /usr/bin/google-chrome).
Selection Path Priority Status
------------------------------------------------------------
0 /opt/google/chromium/1027016/chrome-linux/chrome 300 auto mode
1 /opt/google/chromium/1027016/chrome-linux/chrome 300 manual mode
* 2 /usr/bin/google-chrome-stable 200 manual mode
Press to keep the current choice[*], or type selection number:
if you choose one option the link will no longer be in auto mode, but you can return anytime to automode by running
sudo update-alternative --auto google-chrome
slave links
As mentioned before, these are associated links which should change when master link changes. The examples are node and npm or python and pip. It also works with manual pages as some packages install also manual pages. This is also the case when installing google-chrome as deb package as above.
Version managers not based on update-alternatives usually add/edit the entry in manpath
pointing to correct man page.
update-alternatives
does this via mentioned slave links.
In case of google-chrome, even though it uses update-alternatives by default, it does not use slave links and the installer simply copies the man page in the man location. Which prevents update-alternatives to apply sim link here. It can’t delete the original file and replace it with link as it would point anywhere. Its can be found running
man -w google-chrome
We could manually move man entry for google-chrome-stable elsewhere and let update-alternatives utilize slave links, but we could also add another man entry via slave link to demonstrate flexibility. Let’s do it.
man page for chromium is cosmetically different to man page for google-chrome. If we want to also change the man page when swapping the browser version we can do it by
sudo update-alternatives --install /usr/bin/google-chrome google-chrome /opt/google/chromium/chrome-linux/chrome
--slave /usr/share/man/man1/chromium.1.gz chromium.1.gz /opt/google/chromium/chromium.1.gz
The result of this will be following. If we use the google-chrome-stable the manual entry for chromium via man chromium
will not be available, but swapping the version for chromium will make the manual entry for chromium available along the original manual entry which is/was set as hard link (actual file).
Summary
This short articles demonstrate the basic capabilities of the tool update-alternatives. More detail can be found on manual pages or on Debian website.
Links