The update-alternatives command in Debian is a helpful tool to manage different versions of software on your system. It allows you to easily switch between different versions of the same program. Assuming you have installed multiple Java versions or Python versions on a single system. With the help of update-alternatives you can set any version of the program as the default version for your system.
What is update-alternatives?
update-alternatives is a command-line tool that helps you set up, maintain, and manage symbolic links for different versions of software. These symbolic links point to the actual files on your system, making it easy to switch between versions.
Why Use update-alternatives?
Using update-alternatives is beneficial when you have multiple versions of a program installed on your system and you want to switch between them easily. For example, if you have two versions of Python installed, you can use update-alternatives to switch the default version used by your system.
How update-alternatives Works
When you install software on Debian, it often creates symbolic links in the /usr/bin directory. These links point to the actual program files. The update-alternatives tool helps you manage these links by creating a group of links for each program and allowing you to switch between them.
See the below screenshot showing how the update-alternatives links PHP 8.3 as default version by making link to /usr/bin/php binary:
Practical Examples
Let’s look at some practical examples to understand how to use update-alternatives.
Example 1: Managing Multiple PHP Versions
I have installed PHP 7.4, PHP 8.1 and PHP 8.3 on my system. By default the installation process creates make references for update-alternatives for each installed version. You can simple use update-alternatives to switch between them with the following command:
To switch between the versions, use:
sudo update-alternatives --config php
You should see the following output based on installed PHP versons:
There are 3 choices for the alternative php (providing /usr/bin/php).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/bin/php8.3 83 auto mode
1 /usr/bin/php7.4 74 manual mode
2 /usr/bin/php8.1 81 manual mode
3 /usr/bin/php8.3 83 manual mode
Press <enter> to keep the current choice[*], or type selection number:
Type to selection number and hit Enter to make it as default PHP version for your system.
Example 2: Managing Java Versions
In the previous example the links are already created, but what if they are not created. Then we need to create a group of links of version for the application. Assuming you have multiple versions of Java installed, you can switch between them using update-alternatives. In these steps first we will add different java version to a group and then choose the default version.
- Lets create a link for Java 8 and name the group “java”:
sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/java-8-openjdk-amd64/bin/java 1
You should see the following output:
update-alternatives: using /usr/lib/jvm/java-8-openjdk-amd64/bin/java to provide /usr/bin/java (java) in auto mode
- Next, create a link for Java 11 in the same group:
sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/java-11-openjdk-amd64/bin/java 2
You should see the following output:
update-alternatives: using /usr/lib/jvm/java-11-openjdk-amd64/bin/java to provide /usr/bin/java (java) in auto mode
- To switch between the versions, use:
sudo update-alternatives --config java
You should see the following output:
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 2 auto mode 1 /usr/lib/jvm/java-8-openjdk-amd64/bin/java 1 manual mode 2 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 2 manual mode Press <enter> to keep the current choice[*], or type selection number:
Choose any selection number and press Enter to make it default Java version on your system.
Conclusion
The update-alternatives command in Debian is a powerful tool for managing different versions of software. It simplifies switching between versions and helps you maintain a well-organized system. By following the examples provided, you can easily manage and switch between different versions of Python, Java, and other programs on your Debian system.