How to add library directory to ldconfig cache


ldconfig is a program that is used to maintain the shared library cache. This cache is typically stored in the file /etc/ and is used by the system to map a shared library name to the location of the corresponding shared library file. This is used when a program is executed to find locations of shared libraries that need to be dynamically loaded and linked to the program. By default, the shared library files in /lib, /usr/lib and a few other standard directories are maintained in the cache by ldconfig.

A new program or library might be installed in a non-standard directory, for example in /opt/foobar/lib. Programs that need the shared libraries from this library might fail with this error when executed:

hello-world-program: error while loading shared libraries: cannot open shared object file: No such file or directory

The might be located in /opt/foobar/lib, but the system does not know this, so it cannot successfully load and execute the program.


To fix this problem, we need to add the library directory to the list used by ldconfig. There are two ways to do this: just add to LD_LIBRARY_PATH or rebuild cache.


Set this at shell for temporary use or add to the shell initialization file for permanent effect:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/foobar/lib

Rebuild cache

  • Open the /etc/ as sudo and add a new line with the library directory. In this case, we add /opt/foobar/lib.

  • Rerun ldconfig to rebuild the cache:

$ sudo ldconfig
  • Check if the shared library cache now includes the shared libraries from the new directory:
$ ldconfig -p

Your program should now execute without any errors 🙂

Tried with: Ubuntu 14.04


9 thoughts on “How to add library directory to ldconfig cache

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s