ldconfig command is used to tell the system about new locations of shared libraries. The uses information provided by the /etc/ld.so.conf configuration file.
The ldconfig command creates a cache database of all libraries based on the configuration file. This cache is normally stored in the /etc/ld.so.cache file.
Here is the syntax of the ldconfig command:
# ldconfig [option]
The following table describes useful options for the ldconfig command:
Option | Description |
---|---|
-v | Verbose; print additional information. |
-n | Use a command-line option to specify the location of new shared libraries. Example: ldconfig -n /some/directory. |
-f | Specify a different configuration file rather than the default (/etc/ld.so.conf). |
-p | Use to print a list of current libraries stored in the cache file. |
Configuration file /etc/ld.so.conf
The primary configuration file for shared libraries is the /etc/ld.so.conf file; however, typically there is only a single line in this file:
# cat /etc/ld.so.conf include ld.so.conf.d/*.conf
The include line in this file tells the system to also use all the configuration files in the specified directory. In the case of the previous example, that would be all the files that end in “.conf” in the /etc/ld.so.conf.d directory.
The configuration file itself is simple. It just contains a directory in which the shared libraries are stored:
# more /etc/ld.so.conf.d/libiscsi-x86_64.conf /usr/lib64/iscsi # ls /usr/lib64/iscsi libiscsi.so.2 libiscsi.so.2.0.10900
List Cached Libraries
To list the cached libraries, you can use the -p option of ldconfig command as show below:
# ldconfig -p | more 784 libs found in cache `/etc/ld.so.cache' p11-kit-trust.so (libc6,x86-64) => /lib64/p11-kit-trust.so libz.so.1 (libc6,x86-64) => /lib64/libz.so.1 libyaml-0.so.2 (libc6,x86-64) => /lib64/libyaml-0.so.2 libyajl.so.2 (libc6,x86-64) => /lib64/libyajl.so.2 libxtables.so.10 (libc6,x86-64) => /lib64/libxtables.so.10 libxslt.so.1 (libc6,x86-64) => /lib64/libxslt.so.1 libxshmfence.so.1 (libc6,x86-64) => /lib64/libxshmfence.so.1 libxml2.so.2 (libc6,x86-64) => /lib64/libxml2.so.2 libxmlrpc_util.so.3 (libc6,x86-64) => /lib64/libxmlrpc_util.so.3 libxmlrpc_server_cgi.so.3 (libc6,x86-64) => /lib64/libxmlrpc_server_cgi.so.3 libxmlrpc_server_abyss.so.3 (libc6,x86-64) => /lib64/libxmlrpc_server_abyss.so.3 ....
Add new libraries using ldconfig
To add new shared libraries to the system, you would first download the libraries to the system and place them into a directory. After adding new libraries, you would create a configuration file in the /etc/ld.so.conf.d directory and then execute the ldconfig command. All these tasks should be performed as the root user:
# ls /usr/lib64/test mylib.so.1 # cat /etc/ld.so.conf.d/libtest.conf /usr/lib64/test # ldconfig
If the ldconfig command executes successfully, there will be no output.
LD_LIBRARY_PATH variable
Regular users can’t successfully execute the ldconfig command; however, if a regular user wants to use a custom shared library, then that user can download this file into his or her home directory and make use of the LD_LIBRARY_PATH to indicate the location of custom library files, like so:
$ ls /home/testuser/lib mylib.so.1 $ LD_LIBRARY_PATH=/home/testuser/lib
If executed usefully, the last command should produce no output. To make this a permanent change, place the LD_LIBRARY_PATH=/home/testuser/lib command in your ~/.bashrc file.
$ vi ~/.bashrc LD_LIBRARY_PATH=/home/testuser/lib export LD_LIBRARY_PATH
ldd Command
You can see what shared libraries a specific command uses by using the ldd command. Here is the syntax of the ldd command:
# ldd [options] FILE
For example:
# ldd /bin/ls linux-vdso.so.1 => (0x00007ffee2b3f000) libselinux.so.1 => /lib64/libselinux.so.1 (0x00007ff5a6c22000) libcap.so.2 => /lib64/libcap.so.2 (0x00007ff5a6a1d000) libacl.so.1 => /lib64/libacl.so.1 (0x00007ff5a6814000) libc.so.6 => /lib64/libc.so.6 (0x00007ff5a6447000) libpcre.so.1 => /lib64/libpcre.so.1 (0x00007ff5a61e5000) libdl.so.2 => /lib64/libdl.so.2 (0x00007ff5a5fe1000) /lib64/ld-linux-x86-64.so.2 (0x00007ff5a6e49000) libattr.so.1 => /lib64/libattr.so.1 (0x00007ff5a5ddc000) libpthread.so.0 => /lib64/libpthread.so.0 (0x00007ff5a5bc0000)
The purpose of using the ldd command is to troubleshoot problems with code that you are writing. This command tells you not only what libraries are being called, but specifically which directory each library is being called from. This can be extremely useful when a library is not behaving as you would expect it to behave.
The following table describes useful options for the ldd command:
Option | Description |
---|---|
-v | Verbose; print additional information. |
-u | Display any unused direct dependencies. |