How do I find if a library is installed in linux?

Learn how do i find if a library is installed in linux? with practical examples, diagrams, and best practices. Covers linux, shared-libraries development techniques with visual explanations.

How to Determine if a Library is Installed on Linux

Hero image for How do I find if a library is installed in linux?

Learn various methods to check for the presence of shared and static libraries on your Linux system, ensuring your applications have the dependencies they need.

When developing or deploying applications on Linux, a common task is to verify whether specific libraries are installed. Libraries provide reusable code that applications depend on, and their absence can lead to compilation errors or runtime failures. This article explores several command-line tools and techniques to effectively determine the presence and location of both shared and static libraries on your system.

Understanding Linux Libraries

Before diving into the methods, it's crucial to understand the two primary types of libraries on Linux:

  • Shared Libraries (.so files): These are dynamically linked at runtime. Multiple programs can share a single copy of a shared library in memory, saving disk space and memory. They are typically found in directories like /lib, /usr/lib, /usr/local/lib, or paths specified in /etc/ld.so.conf.
  • Static Libraries (.a files): These are statically linked at compile time. The library's code is copied directly into the executable, making the executable self-contained but larger. They are less common for general system dependencies but are often used for specific application components or when dynamic linking is undesirable.
flowchart TD
    A[Application] --> B{Needs Library?}
    B -->|Yes| C[Check Shared Library (.so)]
    B -->|Yes| D[Check Static Library (.a)]
    C --> E{Found in /lib, /usr/lib, ld.so.conf?}
    D --> F{Found in /usr/lib, /usr/local/lib?}
    E -->|Yes| G[Library Available]
    E -->|No| H[Library Not Found]
    F -->|Yes| G
    F -->|No| H

Flowchart illustrating the library checking process

Method 1: Using ldconfig and ldd for Shared Libraries

The ldconfig utility is used to configure dynamic linker run-time bindings. It creates the necessary links and cache to the most recent shared libraries found in the directories specified in /etc/ld.so.conf and /etc/ld.so.conf.d/*.conf. The ldd command, on the other hand, prints the shared library dependencies of an executable or shared library.

To check if a shared library is known to the system, you can query ldconfig's cache. For example, to check for libcurl:

sudo ldconfig -p | grep libcurl

Checking for libcurl using ldconfig

If the library is installed and configured, you'll see output similar to this:

	libcurl.so.4 (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libcurl.so.4
	libcurl.so (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libcurl.so

To check the dependencies of an executable (e.g., curl itself):

ldd /usr/bin/curl

Listing shared library dependencies of the curl executable

This will show a list of all shared libraries curl depends on, and their paths. If a library is missing, it will typically show not found.

Method 2: Searching for Library Files Directly

You can directly search for library files using commands like find or locate. This is particularly useful for static libraries (.a files) or when ldconfig doesn't list a shared library that you suspect is present but not in a standard path.

To find shared library files (e.g., libssl.so):

find / -name "libssl.so*" 2>/dev/null

Searching for libssl.so files across the filesystem

For static library files (e.g., libz.a):

find /usr/lib /usr/local/lib -name "libz.a" 2>/dev/null

Searching for libz.a in common library directories

Method 3: Using Package Managers

The most reliable way to check for system-installed libraries (especially those installed via official repositories) is through your distribution's package manager. This method tells you if the library's package is installed, which implies the library files are present.

For Debian/Ubuntu (APT):

dpkg -s libssl-dev
# Or to find which package provides a specific file:
dpkg -S /usr/lib/x86_64-linux-gnu/libssl.so

Checking for libssl-dev package and finding package by file

For Red Hat/CentOS/Fedora (RPM/YUM/DNF):

rpm -q libssl-devel
# Or to find which package provides a specific file:
rpm -qf /usr/lib64/libssl.so

Checking for libssl-devel package and finding package by file

For Arch Linux (Pacman):

pacman -Qs openssl
# Or to find which package provides a specific file:
pacman -Qo /usr/lib/libssl.so

Checking for openssl package and finding package by file

Summary of Library Checking Methods

Choosing the right method depends on what you're trying to achieve. If you need to know if a shared library is available for a runtime application, ldconfig -p or ldd are excellent. If you're compiling and need to verify development files, package managers or direct file searches are more appropriate.

Hero image for How do I find if a library is installed in linux?

Comparison of methods for checking library installation