3

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.

3

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.

  • Thanks, but on my machine I had to use apt-cache search, since apt-get search resulted in E: Invalid operation search. Is it a typo, or does your apt-get include the search operation? – anol Jun 25 '15 at 17:13
  • @anol Ah yes, it was indeed a typo, thanks for catching that. – datUser Jun 25 '15 at 17:39
  • There's just a minor issue I had: even after removing the package (including via purge), apt-cache still lists it. And unlike dpkg, it doesn't include a status letter (such as i for installed), so I guess it would be necessary to use apt-cache search for the synonym, then dpkg to check if it's installed... a bit more complex than I expected, but still doable. Could you please confirm if all of that is required, or if you see a simpler way? – anol Jun 25 '15 at 21:03
  • Perhaps i was unclear, use dpkg to see if the package is installed on your system, this is always at step 1. Use apt-cache search only to generate the alias to use when rechecking via dpkg. apt-cache will not reflect installed packages, so it is unaffected if you purge a package, I would only use this to find aliases to check in dpkg. – datUser Jun 25 '15 at 21:09
2

This looks like a job for an aptitude search.

$ aptitude -F %p search '~Plibncurses-dev'
libncurses5-dev                                                                 
libncurses5-dev:i386
$ aptitude -F %p search '~Plibncurses-dev ~rnative'
libncurses5-dev                                                                 
$ aptitude -F %p search '~Plibncurses-dev ~rnative ~i'
libncurses5-dev

That last one would print nothing if libncurses5-dev wasn't installed.

  • Can I expect aptitude to be as widely available as apt-cache on a given machine? I mean, would there be potential users who might have one installed but not the other? – anol Jun 26 '15 at 5:12
  • 1
    @anol No, aptitude isn't as common. apt-cache is part of the base installation (it's in the apt package), aptitude is a separate package that isn't always installed. – Gilles Jun 26 '15 at 8:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.