How to check if a library is installed in Linux

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to check if a specific library is installed on your Linux system. You will explore different methods to achieve this, starting by listing available shared libraries using ldconfig -p to understand what libraries are present and their locations.

Following that, you will utilize the dpkg -l command to identify the package associated with a particular library, providing insight into how libraries are managed within the system's package manager. Finally, you will learn how to directly inspect common library directories like /usr/lib to manually verify the presence of library files.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/BasicFileOperationsGroup(["Basic File Operations"]) linux(("Linux")) -.-> linux/FileandDirectoryManagementGroup(["File and Directory Management"]) linux(("Linux")) -.-> linux/TextProcessingGroup(["Text Processing"]) linux(("Linux")) -.-> linux/InputandOutputRedirectionGroup(["Input and Output Redirection"]) linux(("Linux")) -.-> linux/PackagesandSoftwaresGroup(["Packages and Softwares"]) linux/BasicFileOperationsGroup -.-> linux/ls("Content Listing") linux/BasicFileOperationsGroup -.-> linux/less("File Paging") linux/FileandDirectoryManagementGroup -.-> linux/cd("Directory Changing") linux/TextProcessingGroup -.-> linux/grep("Pattern Searching") linux/InputandOutputRedirectionGroup -.-> linux/pipeline("Data Piping") linux/PackagesandSoftwaresGroup -.-> linux/software("Linux Software") subgraph Lab Skills linux/ls -.-> lab-558728{{"How to check if a library is installed in Linux"}} linux/less -.-> lab-558728{{"How to check if a library is installed in Linux"}} linux/cd -.-> lab-558728{{"How to check if a library is installed in Linux"}} linux/grep -.-> lab-558728{{"How to check if a library is installed in Linux"}} linux/pipeline -.-> lab-558728{{"How to check if a library is installed in Linux"}} linux/software -.-> lab-558728{{"How to check if a library is installed in Linux"}} end

List libraries with ldconfig -p

In this step, you will learn how to list the shared libraries available on your Linux system using the ldconfig command.

Shared libraries are collections of code that multiple programs can use. This saves disk space and memory because the code doesn't need to be duplicated in every program.

The ldconfig command is primarily used to configure dynamic linker run-time bindings. When used with the -p option, it prints the list of directories and candidate libraries that are cached.

Open your terminal if it's not already open. You can do this by clicking the Xfce Terminal icon on the left side of your desktop.

Now, type the following command and press Enter:

ldconfig -p

This command will output a long list of shared libraries found on your system. The output will look something like this:

...
        libzstd.so.1 (libc6,x86-64) => /lib/x86_64-linux-gnu/libzstd.so.1
        libz.so.1 (libc6,x86-64) => /lib/x86_64-linux-gnu/libz.so.1
        libxml2.so.2 (libc6,x86-64) => /lib/x86_64-linux-gnu/libxml2.so.2
        libxml2.so (libc6,x86-64) => /lib/x86_64-linux-gnu/libxml2.so
        libxslt.so.1 (libc6,x86-64) => /lib/x86_64-linux-gnu/libxslt.so.1
...

The output shows the library name, the architecture it's built for (like x86-64), and the path to the library file on the system.

Because the output is very long, you might want to pipe it to a pager like less to view it screen by screen. To do this, use the pipe symbol |:

ldconfig -p | less

Press the spacebar to scroll down a page, b to scroll up, and q to exit less.

This command is useful for seeing which libraries are available and where they are located.

Click Continue to proceed to the next step.

Check library package with dpkg -l

In the previous step, you listed shared libraries. Now, let's learn how to find out which package a specific library belongs to using the dpkg command.

dpkg is the package manager for Debian-based systems like Ubuntu. It's used to install, remove, and manage .deb packages. The -l option lists installed packages.

To find a specific package, we can combine dpkg -l with grep to filter the output. Let's search for packages related to the xml2 library we saw in the ldconfig -p output.

Type the following command in your terminal and press Enter:

dpkg -l | grep xml2

This command lists all installed packages (dpkg -l) and then filters that list to show only lines containing "xml2" (grep xml2).

You should see output similar to this:

ii  libxml2:amd64                      <version>                     amd64        GNOME XML library
ii  libxml2-dev:amd66                  <version>                     amd64        Development files for the GNOME XML library
...

The output provides information about the package:

  • The first two characters (ii) indicate the package status (installed).
  • The package name (e.g., libxml2:amd64).
  • The version number.
  • The architecture (e.g., amd64).
  • A brief description of the package.

This is a powerful way to identify which software package provides a particular library file. This is very useful for troubleshooting or understanding dependencies.

Click Continue to move on.

Inspect library files in /usr/lib

In this final step, let's explore the directory where many shared libraries are stored: /usr/lib.

The /usr directory in Linux contains user-level programs and data. The /usr/lib subdirectory specifically holds shared libraries that are not essential for the system to boot but are needed by applications.

First, let's change our current directory to /usr/lib. We use the cd command for this.

Type the following command and press Enter:

cd /usr/lib

Now you are inside the /usr/lib directory. To see the contents of this directory, use the ls command. Because this directory contains many files, we'll again pipe the output to less.

Type the following command and press Enter:

ls | less

You will see a long list of files and directories. Many of the files ending with .so are shared libraries. You might also see directories like x86_64-linux-gnu, which contain architecture-specific libraries.

Press the spacebar to scroll down and q to exit less.

Let's look for the libxml2 library file we discussed in the previous steps. We can use ls with grep to find it within the current directory (/usr/lib).

Type the following command and press Enter:

ls | grep libxml2

You should see output similar to this, showing the libxml2 library files and symbolic links:

libxml2.so
libxml2.so.2
libxml2.so.2.<version>

The .so files are the actual shared libraries, and the files without a version number (like libxml2.so) are often symbolic links pointing to the actual library file with the version number.

You can use the readlink command to see where a symbolic link points. For example:

readlink libxml2.so

This will show you the actual file that libxml2.so links to, which includes the full version number.

libxml2.so.2

This step showed you how to navigate to a common library directory and inspect its contents using cd, ls, and grep. Understanding where libraries are stored is crucial for system administration and development.

Click Continue to complete the lab.

Summary

In this lab, you learned how to check if a library is installed in Linux using several methods. You started by listing available shared libraries and their locations using the ldconfig -p command, understanding its role in managing dynamic linker bindings.

Next, you explored how to identify the package a specific library belongs to using the dpkg -l command, which is a key tool for managing Debian packages. Finally, you learned to directly inspect common library directories like /usr/lib to manually verify the presence of library files. These steps provide a comprehensive approach to determining library installation status on a Linux system.