How to use the command 'ldconfig' (with examples)
- Linux
- December 25, 2023
The ’ldconfig’ command is used to configure symlinks and cache for shared library dependencies. It is commonly used in Linux systems to manage shared libraries. This article will illustrate three use cases of the command with examples.
Use case 1: Update symlinks and rebuild the cache
Code:
sudo ldconfig
Motivation: This use case is typically executed when a new library is installed on a system. Running ’ldconfig’ updates the symlinks and rebuilds the cache, ensuring that the newly installed library is correctly linked and available for use by other programs.
Explanation: The command ’ldconfig’ with no additional arguments instructs the system to update the symlinks and rebuild the cache for all shared libraries in the default library search paths.
Example output:
/sbin/ldconfig.real: RelativeFileNames directory `lib/x86_64-linux-gnu' is not allowed anymore
/sbin/lclight.list:102: ignoring bad line starting with `/lib/x86_64-linux-gnu', errno 0
Use case 2: Update symlinks for a given directory
Code:
sudo ldconfig -n path/to/directory
Motivation: This use case is useful when you want to update symlinks and register shared libraries located in a specific directory. It allows you to explicitly specify the directory where the system should look for shared library updates.
Explanation: The ‘-n’ option tells the ’ldconfig’ command to perform the update without actually rebuilding the cache. The ‘path/to/directory’ argument specifies the directory for which the symlinks should be updated and registered.
Example output:
(libsqlite3.so.0.8.6) => /usr/lib/x86_64-linux-gnu/libsqlite3.so.0.8.6
(libzstd.so.1) => /usr/lib/x86_64-linux-gnu/libzstd.so.1
Use case 3: Print libraries in the cache and check for a specific library
Code:
ldconfig -p | grep library_name
Motivation: This use case is helpful when you want to verify whether a particular library is present in the cache. It allows you to search through the cache and find the matching library entry.
Explanation: The ‘-p’ option directs the ’ldconfig’ command to print all the shared libraries in the cache along with their associated information. The ‘grep’ command is then used to search for a specific library name within the output.
Example output:
libncurses.so.5 (libc6,x86-64) => /lib/x86_64-linux-gnu/libncurses.so.5
libm.so.6 (libc6,x86-64) => /lib/x86_64-linux-gnu/libm.so.6
Conclusion:
The ’ldconfig’ command is a powerful tool for managing shared library dependencies on a Linux system. It allows you to update symlinks, rebuild the cache, and verify the presence of specific libraries. By understanding and utilizing the various options of the command, you can ensure that shared libraries are correctly linked and available for use by programs.