How to Properly Remove and Correct update-alternatives Links in Linux (libRlapack.so Error Fix)


6 views

When working with Linux's alternative system, a simple typo can lead to frustrating errors. In this case, the incorrect command:

update-alternatives --install /usr/lib64/R/lib/libRblapack.so libRblapack.so /usr/lib64/R/lib/libRblapack_native.so 100

was executed instead of the intended:

update-alternatives --install /usr/lib64/R/lib/libRlapack.so libRlapack.so /usr/lib64/R/lib/libRlapack_native.so 100

The system now insists that the primary link for libRlapack.so must be /usr/lib64/R/lib/libRblapack.so, which reveals how update-alternatives maintains internal consistency through reference counting.

To properly clean up the incorrect entry, we need to:

# First display all alternatives for the group
update-alternatives --display libRblapack.so

# Then remove all paths associated with the incorrect name
update-alternatives --remove-all libRblapack.so

# Verify the removal was successful
update-alternatives --display libRblapack.so

If the standard removal doesn't work, we might need to manually edit these files:

/var/lib/dpkg/alternatives/libRblapack.so
/etc/alternatives/libRblapack.so

After editing, run:

update-alternatives --auto libRlapack.so

Once cleaned up, we can properly establish the correct alternatives:

update-alternatives --install /usr/lib64/R/lib/libRlapack.so \
    libRlapack.so \
    /usr/lib64/R/lib/libRlapack_native.so \
    100

update-alternatives --config libRlapack.so

Some best practices:

  • Always verify paths before running update-alternatives
  • Use tab-completion for path accuracy
  • Consider using --dry-run option first

When working with update-alternatives in Linux, it's easy to make typos that can cause persistent issues. A common scenario is creating an alternative with an incorrect name, which then prevents you from creating the correct one later.

# The incorrect command that was run initially
update-alternatives --install /usr/lib64/R/lib/libRblapack.so libRblapack.so /usr/lib64/R/lib/libRblapack_native.so 100

# The correct command that fails because of the previous mistake
update-alternatives --install /usr/lib64/R/lib/libRlapack.so libRlapack.so /usr/lib64/R/lib/libRlapack_native.so 100

The error message "the primary link for libRlapack.so must be /usr/lib64/R/lib/libRblapack.so" indicates that the system is still referencing the incorrect link name. This happens because update-alternatives maintains a database of all alternatives.

To properly fix this, you need to completely remove the incorrect alternative entry:

# First, check all alternatives in the group
update-alternatives --list libRblapack.so

# Then remove all paths associated with the incorrect alternative
update-alternatives --remove-all libRblapack.so

# Alternatively, if you know the exact path to remove
update-alternatives --remove libRblapack.so /usr/lib64/R/lib/libRblapack_native.so

After removing the incorrect entry, you can now create the proper alternative:

update-alternatives --install /usr/lib64/R/lib/libRlapack.so libRlapack.so /usr/lib64/R/lib/libRlapack_native.so 100

# Verify the new alternative was created correctly
update-alternatives --display libRlapack.so

If you're still facing issues, you might need to manually clean up the alternatives database:

# Check the alternatives database location (typically /var/lib/dpkg/alternatives)
ls -l /var/lib/dpkg/alternatives/

# Backup then remove the problematic entry (as root)
sudo cp /var/lib/dpkg/alternatives/libRblapack.so ~/backup_libRblapack.so
sudo rm /var/lib/dpkg/alternatives/libRblapack.so

Remember to always double-check your paths and filenames when working with update-alternatives to avoid such issues.