Shared libraries (.so files) are reusable code modules loaded by multiple programs at runtime. Understanding how Linux finds and manages shared libraries is essential for installing software, troubleshooting "library not found" errors, and managing dependencies.
How Shared Libraries Work
# Shared libraries are loaded at runtime, not compiled into the binary
# This saves memory — one copy shared among all processes
# Common library locations:
# /lib and /usr/lib — System libraries
# /lib64 and /usr/lib64 — 64-bit system libraries
# /usr/local/lib — Locally installed libraries
# /opt/*/lib — Third-party software libraries
# Library naming convention:
# libname.so.major.minor.patch
# Example: libssl.so.3.0.2
# Symlink: libssl.so.3 -> libssl.so.3.0.2
# Symlink: libssl.so -> libssl.so.3
Finding Library Dependencies
# Show libraries a binary needs
ldd /usr/bin/nginx
# Example output:
# linux-vdso.so.1 (0x00007ffcf1bfe000)
# libssl.so.3 => /lib/x86_64-linux-gnu/libssl.so.3 (0x...)
# libcrypto.so.3 => /lib/x86_64-linux-gnu/libcrypto.so.3 (0x...)
# libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x...)
# libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x...)
# "not found" means the library is missing!
# libcustom.so.1 => not found ← This is a problem
Fixing "Library Not Found" Errors
# Step 1: Find where the library actually is
find / -name "libmissing.so*" 2>/dev/null
# Or search installed packages
dpkg -S libmissing.so # Debian/Ubuntu
rpm -qf libmissing.so # RHEL/Rocky
# Step 2: Add the library path
# Option A: Create a config file in /etc/ld.so.conf.d/
echo "/opt/myapp/lib" | sudo tee /etc/ld.so.conf.d/myapp.conf
sudo ldconfig
# Option B: Set LD_LIBRARY_PATH (temporary, for testing)
export LD_LIBRARY_PATH=/opt/myapp/lib:$LD_LIBRARY_PATH
# Option C: Set rpath in the binary at compile time
# gcc -Wl,-rpath,/opt/myapp/lib myapp.c -o myapp
# Step 3: Verify
ldd /path/to/binary | grep "not found"
ldconfig: Library Cache Manager
# ldconfig updates the shared library cache
# Run it after installing new libraries
# Update cache
sudo ldconfig
# Show cached libraries
ldconfig -p | head -20
# Search for a specific library
ldconfig -p | grep libssl
# Rebuild cache verbosely
sudo ldconfig -v 2>&1 | head -30
Managing Library Versions
# Multiple versions can coexist via symlinks
ls -la /lib/x86_64-linux-gnu/libssl*
# libssl.so -> libssl.so.3
# libssl.so.3 -> libssl.so.3.0.2
# libssl.so.3.0.2 (actual file)
# Install a specific version
sudo apt install libssl-dev=3.0.2-1
# Check which version a binary uses
ldd /usr/bin/openssl | grep ssl
Static vs Shared Linking
# Shared linking (default): binary uses .so files at runtime
gcc -o myapp myapp.c -lssl -lcrypto
# Static linking: library code compiled into binary
gcc -static -o myapp myapp.c -lssl -lcrypto
# Check if binary is statically linked
file /usr/bin/myapp
# "statically linked" vs "dynamically linked"
Troubleshooting Common Issues
# "error while loading shared libraries"
# Solution: Run ldconfig or add library path
# "version GLIBC_2.xx not found"
# Solution: Your binary needs a newer glibc than installed
# Check your glibc version:
ldd --version
# "cannot open shared object file"
# Solution: Install the missing library package
apt-cache search libmissing # Find the package
sudo apt install libmissing1 # Install it
sudo ldconfig # Update cache