ldconfig in Linux: A Complete Guide to Library Management
Introduction / Why This Is Needed
In Linux, dynamic libraries (.so files) are a key mechanism for saving memory and simplifying program updates. However, the system needs to know where these libraries are located. This is handled by the linker cache, which is built and updated by the ldconfig utility.
This guide will teach you how to manage library search paths: add non-standard directories, update the cache, and diagnose problems when a program complains that a "library is not found."
Requirements / Preparation
Before you begin, make sure you have:
- Access to a Linux terminal (Ubuntu, Debian, CentOS, Fedora, etc.).
- Superuser (
sudo) privileges to edit system configuration files and runldconfig. - A basic understanding of the filesystem structure.
- The library itself (a file with the
.soextension, e.g.,libexample.so) must already be copied to your chosen directory.
Step-by-Step Instructions
Step 1: Check the Current Library Cache
First, see which paths are already known to the system. Run the command:
ldconfig -p
The output will be very extensive—it's a list of all libraries in the cache and their full paths. To find a specific library, use grep:
ldconfig -p | grep libssl
If your library is already in the list, the problem might be elsewhere (for example, with the library version).
Step 2: Add a New Library Directory
Suppose you placed your libraries in /opt/myapp/lib. You need to inform the system about this path.
Method A (Recommended): Create a separate configuration file in /etc/ld.so.conf.d/:
echo "/opt/myapp/lib" | sudo tee /etc/ld.so.conf.d/myapp.conf
Method B: Manually edit the file sudo nano /etc/ld.so.conf and add the path on a new line. This method is less preferable because changes to the main file are harder to track.
💡 Tip: Use Method A. It complies with the Filesystem Hierarchy Standard and allows you to easily enable/disable paths by deleting a single file.
Step 3: Update the Cache
After adding the path, you need to rebuild the cache. Run:
sudo ldconfig
For debugging, run it with the -v (verbose) flag to see which directories are being processed:
sudo ldconfig -v
In the output, you will see lines like caching file... and scanning.... Ensure your new path (/opt/myapp/lib) is mentioned.
Step 4: Verify the Library is Found
Repeat the check from Step 1, but now for your specific library:
ldconfig -p | grep libexample
If a line with the full path to your library appears in the output (e.g., libexample.so (libc6,x86-64) => /opt/myapp/lib/libexample.so), then everything is configured correctly.
Step 5: Alternative — The LD_LIBRARY_PATH Variable (A Temporary Solution)
If you need to quickly run a program without administrator privileges or for testing, use the environment variable:
export LD_LIBRARY_PATH="/opt/myapp/lib:$LD_LIBRARY_PATH"
./myprogram
Important: This setting only affects the current terminal. To make it permanent, add the line export LD_LIBRARY_PATH=... to your ~/.bashrc or ~/.profile.
Verifying the Result
Run the application that should use the new library. If it starts without "cannot open shared object file" errors, then ldconfig worked.
You can also check the dependencies of a specific binary file using the ldd utility:
ldd /usr/bin/myprogram | grep libexample
The output should display the path you added.
Common Issues
Error: "No such file or directory" when running ldconfig
Ensure the path in the configuration file exists and is a directory, not an individual file. Check with: ls -ld /opt/myapp/lib.
Error: "Permission denied"
You are running ldconfig without sudo. All system operations require administrator privileges.
The library is in the directory, but ldconfig doesn't find it
- Check the filename. Usually, the linker looks for files named
lib<name>.soorlib<name>.so.<version>. Symlinks must be correct. - Ensure the directory and file have read permissions for all users (e.g.,
chmod 755 /opt/myapp/libandchmod 644 /opt/myapp/lib/*.so).
Changes don't apply after editing the config
You might have forgotten to run sudo ldconfig after saving the file. The cache does not update automatically.
LD_LIBRARY_PATH doesn't work
Make sure you export the variable in the same session where you run the program and that the path is absolute.
Advanced Usage
Force-update only one file
If you replaced a library in an already known directory (e.g., /usr/lib), you can update the cache only for that file:
sudo ldconfig -n /usr/lib
Ignore the cache (for debugging)
To make the linker ignore the cache and search for libraries directly in standard paths, use the -N flag when running the program:
LD_PRELOAD=/path/to/libexample.so myprogram
Or temporarily rename the cache file: sudo mv /etc/ld.so.cache /etc/ld.so.cache.bak (not recommended in production).
View information about a specific library
To find out which version of a library will be loaded by default, use:
ldconfig -p | grep -w libexample
The -w flag searches for exact word matches, helping to avoid false positives.
Conclusion
The ldconfig utility is a simple but critically important Linux administration tool. Proper management of library paths solves most problems with running programs that complain about missing .so files. The basic algorithm: add the path to /etc/ld.so.conf.d/, run sudo ldconfig, verify with ldconfig -p. For temporary solutions, use LD_LIBRARY_PATH.