In linux, how do I check if a library is installed or not? (from the command line of course).

In my specific case now, I want to check whether libjpeg is installed.

  • What distribution are you using ? – radius Aug 16 '09 at 16:09
up vote 176 down vote accepted

To do this in a distro-independent* fashion you can use ldconfig with grep, like this:

ldconfig -p | grep libjpeg

If libjpeg is not installed, there will be no output. If it is installed, you will get a line for each version available.

Replace libjpeg by any library you want, and you have a generic, distro-independent* way of checking for library availability.

If for some reason the path to ldconfig is not set, you can try to invoke it using its full path, usually /sbin/ldconfig.

**99% of the times*

  • 2
    This should be accepted as the answer. Great! – user105523 Dec 19 '13 at 13:53
  • 1
    It's not possible to use this for checking for the dev version (headers) of a library, right? – andig Aug 14 '15 at 8:08
  • 2
    Can you specify 99% times? bash: ldconfig: command not found on Debian x64 Jessie with xfce! This is the recomended debian release on the debian website... – Tomáš Zato Oct 12 '15 at 13:46
  • 4
    @TomášZato: ldconfig is not available (command not found shows up) if you try to run it without being superuser. – Gauthier Aug 29 '16 at 9:01
  • 2
    /sbin/ldconfig -p works for me without needing to be root. – Pedro Gimeno Nov 9 '16 at 4:12

You can check with the package manager of your distribution (aptitude, yum, ...) but as you did not give your distribution I can't give you the right command.

Another way can be to run gcc -ljpeg, if you get 'ld: library not found for -ljpeg' it means that gcc has not found the library (but it don't mean that it's not installed), if you get something like 'Undefined symbols: "_main", referenced from: ...' it means that libjpeg has been found.

locate libjpeg; ls /usr/lib/libjpeg*; ls /lib/libjpeg* are some other way to find if the lib in installed in the system

There is many other way to check that, if you give us more context (why you need to check if libjpeg is installed) we could give you the best solution for your specific case.

  • How to find it using aptitude? – BBK Apr 3 '12 at 5:52

I'm use whereis utility.
Sample:

l1feh4ck3r@xxx:~$ whereis libjpeg
libjpeg: /usr/lib/libjpeg.so /usr/lib/libjpeg.a /usr/lib/libjpeg.la

For deb-based distribution you can do

dpkg -s packagename

Or if you know the filename only, use

locate filename

The filename is usually libsomething.so[.version].

  • 1
    The filename can also be something like "libsomething.a" for static version. – radius Aug 16 '09 at 16:40
  • I find dpkg -s limited in utility, because it wants the actual package name, which may differ subtly or significantly from the library itself. I use dpkg -s|grep LIBRARY – Drew Stephens Aug 16 '09 at 16:41
  • 2
    @dinomite apt-file is your friend :) – radius Aug 16 '09 at 16:50

I use this:

gcc -lpng

When the lib is installed, it yields:

undefined reference to 'main'

When the lib is not installed:

cannot find -lpng

On Redhat based systems, one can use pkg-config to verify if a library is installed or not. Many rpm binaries actually make the same checks before proceeding with installation, so we can reasonably rely on its veracity.

pkg-config --cflags jpeg

pkg-config --libs jpeg

pkg-config --cflags "jpeg >= 1.0.0" # for version check
pkg-config  --modversion jpeg | awk -F. '{ printf "0x%02X%02X%02X\n",$1,$2,$3 }' #version check

This is done by configuration tools on linux all the time.

Look at this Tutorial about autoconf and KDevelop.

Other tricks would use commands like ldconfig and dpkg.

You can also try using dpkg to check whether it is installed.

dpkg --list | grep [some_key_words_of_your_lib]

Besides, on CentOS, you can try this.

rpm -qa [lib_name]

  • Note: Will work only for Debian based distros – ALex_hha Oct 25 at 21:33
  • @ALex_hha Yes, thank you, this should be emphasized. On CentOS or red hat based systems, use pkg-config instead. – Scott Yang Oct 31 at 9:23

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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