Apt – checking if a similar package has been installed

aptdpkg

This question explains how to find out if a given Debian package has been installed, but it does not take into account "synonyms" when installing via apt-get.

For instance, if I try apt-get install libncurses-dev, apt-get replies:

Note, selecting 'libncurses5-dev' instead of 'libncurses-dev'

And then it installs that package (libncurses5-dev), which is fine by me.

But what if I want to make a script to detect if the package has already been installed?

dpkg -s libncurses-dev replies that the package is not installed, which is indeed correct, since it's libncurses5-dev that was installed. But I'd like my script to detect that, in this case, it no longer needs to install libncurses-dev.

I could not find an option in apt-get to check if the given package or one of its providers has already been installed, such that my script would work when checking for libncurses-dev as well as for libncurses5-dev.

Best Answer

If you want to write a script to check to see if package libncurses-dev or its alias has been installed, consider the following program flow:

  1. Check if the package has been installed with dpkg using the exact name, libncurses-dev in this case.
  2. If the above does not evaluate to true, then search apt for the package you are looking for using the non-aliased name:

    $ apt-cache search libncurses-dev
    libncurses5-dev - developer's libraries for ncurses
    

It appears that apt-cache search will return the 'alias' if the package has one.

  1. If #1 evaluates false and #2 returns an alias, just grab the package's alias and try #1 again.

Check dpkg again with the alias name of the package, in this case it would be libncurses5-dev. If dpkg does not find the package by an alias (actually a superseded package) then it must not be installed.

Related Question