214

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.

Romain
  • 207
  • 1
  • 6
hasen
  • 2,424
  • 2
  • 18
  • 12

10 Answers10

280

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*

faken
  • 3,044
  • 1
  • 12
  • 7
  • 2
    This should be accepted as the answer. Great! –  Dec 19 '13 at 13:53
  • Except for NixOS :-( – iElectric Apr 16 '15 at 18:34
  • 1
    It's not possible to use this for checking for the dev version (headers) of a library, right? – andig Aug 14 '15 at 08:08
  • 1
    @andig for that you my try something like "find /usr/include/ -iname \*jpeg\*". – faken Aug 14 '15 at 08:26
  • 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 - Reinstate Monica Oct 12 '15 at 13:46
  • Are you really asking me if at June 6, 2013 (one month after Debian Whezzy was released, almost two years before Jessie was released), I experimented all existing Linux distros and concluded that the suggested command worked on 99% of them? The answer is no, of course not. It is a figure of speech. Read it like: "It works in most distributions". – faken Oct 12 '15 at 15:47
  • However, even though I don't have access to a Debian Jessie right now, googling for "debian jessie ldconfig" will tell us that ldconfig is in the `libc-bin` package, which is an *essential* package, which means it should be available by default. Maybe it's a problem with your specific installation then? Maybe `sudo apt-get install libc-bin` will do the trick? Best regards. – faken Oct 12 '15 at 15:52
  • 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 09:01
  • Are there any equivalent of ldconfig for MacOS? – Leszek Sep 09 '16 at 07:45
  • 2
    `/sbin/ldconfig -p` works for me without needing to be root. – Pedro Gimeno Nov 09 '16 at 04:12
  • If you're scripting this, you get the path via `ldconfig -p | grep libjpeg | cut -d' ' -f4` – mgarciaisaia Jul 08 '17 at 05:05
  • A big caveat here is that the ldconfig approach does not take into consideration any libraries which are installed in "non-standard" locations and located by the `$LD_LIBRARY_PATH` environment variable mechanism. (Or at least not on my machine.) – R.M. Feb 16 '18 at 16:02
  • [Fix for the LD_LIBRARY_PATH issue](https://unix.stackexchange.com/a/410931/133603) – R.M. Feb 16 '18 at 16:09
36

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.

radius
  • 9,545
  • 23
  • 45
18

I use the whereis utility.

Sample:

l1feh4ck3r@xxx:~$ whereis libjpeg
libjpeg: /usr/lib/libjpeg.so /usr/lib/libjpeg.a /usr/lib/libjpeg.la
chus
  • 103
  • 4
lifchicker
  • 189
  • 4
13

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
AndreLDM
  • 231
  • 2
  • 3
12

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].

Kim
  • 729
  • 1
  • 5
  • 12
5

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
Saasira
  • 183
  • 2
  • 4
2

On Ubuntu 20.04, I am able to display a wealth of relevant information for a package using aptitude.

% aptitude show libssl-dev
Package: libssl-dev                      
Version: 1.1.1f-1ubuntu2.1
State: installed
Automatically installed: no
Multi-Arch: same
Priority: optional
Section: libdevel
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Architecture: amd64
Uncompressed Size: 8,006 k
Depends: libssl1.1 (= 1.1.1f-1ubuntu2.1)
Suggests: libssl-doc
Conflicts: libssl1.0-dev
Breaks: libssl-dev:i386 (!= 1.1.1f-1ubuntu2.1)
Replaces: libssl-dev:i386 (< 1.1.1f-1ubuntu2.1)
Description: Secure Sockets Layer toolkit - development files
 This package is part of the OpenSSL project's implementation of the SSL and TLS cryptographic protocols for secure communication over
 the Internet. 
 
 It contains development libraries, header files, and manpages for libssl and libcrypto.
Homepage: https://www.openssl.org/

To find which package would provide a particular file I have found apt-file very useful - some instructions are here: https://linuxhint.com/find_which_package_contains_specific_file_ubuntu/

% apt-file search 'libjpeg.so'          
darktable: /usr/lib/x86_64-linux-gnu/darktable/plugins/imageio/format/libjpeg.so
libjpeg-turbo8: /usr/lib/x86_64-linux-gnu/libjpeg.so.8
libjpeg-turbo8: /usr/lib/x86_64-linux-gnu/libjpeg.so.8.2.2
libjpeg-turbo8-dev: /usr/lib/x86_64-linux-gnu/libjpeg.so
libjpeg62: /usr/lib/x86_64-linux-gnu/libjpeg.so.62
libjpeg62: /usr/lib/x86_64-linux-gnu/libjpeg.so.62.0.0
libjpeg62-dev: /usr/lib/x86_64-linux-gnu/libjpeg.so
libjpeg9: /usr/lib/x86_64-linux-gnu/libjpeg.so.9
libjpeg9: /usr/lib/x86_64-linux-gnu/libjpeg.so.9.4.0
libjpeg9-dev: /usr/lib/x86_64-linux-gnu/libjpeg.so
libxine2-misc-plugins: /usr/lib/x86_64-linux-gnu/xine/plugins/2.7/xineplug_decode_libjpeg.so
nsight-systems: /usr/lib/nsight-systems/Host-x86_64/libjpeg.so.8

2

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.

nik
  • 7,040
  • 2
  • 24
  • 30
1

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]

Scott Yang
  • 11
  • 2
0

as per Kim above

dpkg -s packagename

[[ $(dpkg -s libsox-fmt-pulse 2> /dev/null) =~ "is not installed" ]] && sudo apt install libsox-fmt-pulse # for hdmi output
BETLOG
  • 3
  • 2