Introduction
When a shared library is installed or updated on Linux, the dynamic linker (ld.so) uses a cache file (/etc/ld.so.cache) to locate libraries quickly. This cache is built by ldconfig. If ldconfig is not run after installing a new library, the linker cannot find it, causing applications to fail with cannot open shared object file: No such file or directory even though the library file exists on disk.
Symptoms
- Application fails with
error while loading shared libraries: libfoo.so.1: cannot open shared object file ldd /usr/bin/myappshowslibfoo.so.1 => not found- Library file exists at
/usr/local/lib/libfoo.so.1but is not found ldconfig -p | grep libfooreturns nothing- After running
ldconfig, the same application works correctly
Common Causes
ldconfignot run aftermake installof a manually compiled library- Library installed to non-standard path not in
/etc/ld.so.conf.d/ - Custom library path
/opt/myapp/libnot configured in ldconfig - ldconfig cache corrupted or
/etc/ld.so.cachedeleted - Library symlink missing or broken (
libfoo.so->libfoo.so.1)
Step-by-Step Fix
- 1.Verify the library file exists:
- 2.```bash
- 3.find / -name "libfoo.so*" 2>/dev/null
- 4.ls -la /usr/local/lib/libfoo.so*
- 5.
` - 6.Check current ldconfig cache:
- 7.```bash
- 8.ldconfig -p | grep libfoo
- 9.# If nothing returned, the cache does not know about this library
- 10.
` - 11.Run ldconfig to rebuild the cache:
- 12.```bash
- 13.sudo ldconfig
- 14.# Verify
- 15.ldconfig -p | grep libfoo
- 16.
` - 17.Add custom library paths to ldconfig configuration:
- 18.```bash
- 19.echo "/usr/local/lib" | sudo tee /etc/ld.so.conf.d/local.conf
- 20.echo "/opt/myapp/lib" | sudo tee /etc/ld.so.conf.d/myapp.conf
- 21.sudo ldconfig
- 22.
` - 23.Fix broken library symlinks:
- 24.```bash
- 25.cd /usr/local/lib
- 26.ls -la libfoo.so*
- 27.# If libfoo.so symlink is missing:
- 28.sudo ln -sf libfoo.so.1.2.3 libfoo.so
- 29.sudo ldconfig
- 30.
` - 31.Debug with LD_LIBRARY_PATH as a temporary workaround:
- 32.```bash
- 33.export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
- 34.ldd /usr/bin/myapp
- 35../myapp
- 36.
`
Prevention
- Always run
sudo ldconfigaftermake installof libraries - Add post-install hooks in packaging scripts:
post_install: ldconfig - Use
ldconfig -vto verbose-check all library paths during deployment - Verify library resolution with
lddas part of application deployment validation - Configure all non-standard library paths in
/etc/ld.so.conf.d/at installation time