Understanding the ldconfig command in Linux

Written by: Nimesha Jinarajadasa   |   Last updated: December 10, 2022

Are you adding or removing a new library to your program? There are situations where dynamic linker may not be aware of it.

In this tutorial, we learn about ldconfig command to manage dynamic linker run-time bindings in Linux.

ldconfig command

ldconfig is a Linux command that is used to configure dynamic linker run-time bindings. The dynamic linker is in charge of loading and linking libraries to applications at run time.

ldconfig command is used to manage the list of libraries that the dynamic linker searches when loading libraries for dynamically linked programs. It checks for the shared library directories specified in the following places.

  • /etc/ld.so.conf file
  • directories specified in the command line (command-line mode with -n)
  • trusted directories
    • /lib,/usr/lib (32-bit systems)
    • /lib64,/usr/lib64 (64-bit systems)
*.conf file located under this ld.so.conf.d directory

Syntax:

ldconfig [option]

How ldconfig works

The ldconfig tool uses the ld.so.conf file to decide which directories to look for libraries when it is executed. It searches for libraries that have the .so suffix (signifying that they are shared libraries) and generates symbolic links for them in the /etc/ld.so.cache directory for each directory given in the ld.so.conf file.

The format of the symbolic links produced by ldconfig is yourlibname.so.x, where yourlibname denotes the name of the library and x denotes the version. The dynamic linker looks up the locations of libraries at runtime using the ld.so.cache file.

The dynamic linker looks through the ld.so.cache file for the necessary libraries before loading them into memory when a dynamically linked program is run.

Install ldconfig

ldconfig is available by default in most Linux Distributions. In case you get the "ldconfig command not found", install the respective Glibc package.

Ubuntu / Debian

apt install glibc-source  #Debian
apt install eglibc-source  #Ubuntu

Redhat / RHEL

yum install glibc 

Fedora

dnf install glibc

Arch Linux

pacman -S glibc

How to use ldconfig

Let's look at how to use ldconfig command with some useful examples.

Print libraries stored in the current cache

When you add a new shared library to the system, the ldconfig command creates the symbolic links and updates the cache as well. The -p option will print all the shared objects stored in the current cache as follows.

ldconfig -p
show current libraries from the cache

Related Read: 8 LDD Command Examples in Linux

Manually link libraries

Use the -n option to specify that the specified libraries should be added to the list of libraries that are searched by the dynamic linker, without actually creating any symbolic links.

For example, to add the /usr/local/lib/mynewlib.so library to the list of libraries searched by the dynamic linker, you could use the following command:

ldconfig -n /usr/local/lib/mynewlib.so

Update the Cache

When you installed a new library, the ldconfig command can be used with the -n option to directly update the links for the shared library. But this doesn't update the cache. It is mandatory to run the following command to rebuild and update the cache.

sudo ldconfig

If the command is executed successfully, no output will print to the console.

Display libraries

All the available shared libraries in the system can be examined by executing the ldconfig command along with the -v option.

ldconfig -v

ldconfig command options

-p - Prints all the directories and shared libraries stored in the current cache file /etc/ld.so.cache. Additionally, prints the number of libraries found in the cache as well.

-v - The verbose mode prints the version numbers of each library, directory names, and links associated with those. It overrides the normal quiet mode.

-n - Enables the command line mode and the command will only process directories specified in the command line. The trusted directories or the ones specified in the /etc/ld.so.conf won't process.

-V - Prints the ldconfig program version.

-N - Halts the cache file (ld.so.cache) update.

-X - Stops updating the links.

-f - Use the configuration file specified with this option instead of the default one (/etc/ld.so.conf).

-l - Enables linking the individual libraries manually. This is called library mode.

-C Use the cache file specified with this option instead of the default one (/etc/ld.so.cache)

-i - Auxiliary cache file will ignore.

Conclusion

Typically during the makefiles process, the shared library cache is updated. ldconfig usually comes to help when the library got changed or newer versions are installed.

Thanks for reading, please leave your feedback and suggestions in the below comment section.

About The Author

Nimesha Jinarajadasa

Nimesha Jinarajadasa

Nimesha is a Full-stack Software Engineer for more than five years, he loves technology, as technology has the power to solve our many problems within just a minute. He have been contributing to various projects over the last 5+ years and working with almost all the so-called “03 tiers(DB, M-Tier, and Client)”. Recently, he has started working with DevOps technologies such as Azure administration, Kubernetes, Terraform automation, and Bash scripting as well.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment