Menu
Manage shared libraries
“What shared libraries are, where Linux keeps them, and how ldd, ldconfig, /etc/ld.so.conf and LD_LIBRARY_PATH decide which ones a program loads.”
Sooner or later you’ll see this:
./myapp: error while loading shared libraries: libacme.so.2: cannot open shared object file: No such file or directoryThe program is fine. It just can’t find a piece of code it relies on. To fix it you need to know what shared libraries are, where Linux looks for them, and the few tools and files that control that search.
Static and shared#
A library is a bundle of ready-made functions: printing text, reading files, talking TLS. Programs can use one in two ways:
- Statically linked: the library code is copied into the program when it’s built. The file is bigger, but it runs anywhere. Static libraries end in
.a(for “archive”). - Dynamically linked: the program only records which libraries it needs. When it starts, a loader finds them and maps them into memory. These shared libraries end in
.so(“shared object”), often with a version number:libc.so.6,libssl.so.3.
Most programs are dynamic. Many programs share one copy of a library in memory, and a security fix in libssl reaches every program that uses it after a single package update.
Shared libraries come with a naming pattern made of symlinks:
ls -l /usr/lib/x86_64-linux-gnu/libz.so*lrwxrwxrwx 1 root root 14 Mar 3 2023 /usr/lib/x86_64-linux-gnu/libz.so.1 -> libz.so.1.2.13
-rw-r--r-- 1 root root 121280 Mar 3 2023 /usr/lib/x86_64-linux-gnu/libz.so.1.2.13libz.so.1.2.13 is the real file. libz.so.1 is the soname, the name programs ask for, so a bug-fix release can replace the real file without breaking anything. A plain libz.so link only shows up when you install the development package (zlib1g-dev, zlib-devel), because only the compiler needs it.
Where libraries live#
| Location | Used for |
|---|---|
/lib, /usr/lib | System libraries. On modern systems /lib is a link to /usr/lib. |
/lib64, /usr/lib64 | 64-bit libraries on RHEL, Fedora and SUSE |
/usr/lib/x86_64-linux-gnu/ | 64-bit libraries on Debian and Ubuntu (“multiarch”: each CPU type gets its own folder) |
/usr/local/lib | Libraries you built and installed yourself |
/opt/<app>/lib | Libraries bundled with third-party software |
The dynamic loader#
When you start a dynamic program, the kernel first runs the dynamic loader (also called the dynamic linker), ld.so. On 64-bit x86 it’s /lib64/ld-linux-x86-64.so.2; older 32-bit systems use ld-linux.so.2. It looks for each needed library in this order:
- Directories in the
LD_LIBRARY_PATHenvironment variable - Directories baked into the program at build time (
RUNPATH) - The cache file
/etc/ld.so.cache - The defaults:
/liband/usr/lib(/lib64and/usr/lib64on 64-bit RPM systems)
The loader never reads /etc/ld.so.conf itself. It uses the cache, which ldconfig builds from that file.
ldd: what does this program need?#
ldd lists a program’s shared libraries and where each one resolves. Try it in a Debian container (docker run -it --rm debian):
ldd /bin/ls linux-vdso.so.1 (0x00007ffd6b9f2000)
libselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007f3c1a6b5000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f3c1a4d4000)
libpcre2-8.so.0 => /lib/x86_64-linux-gnu/libpcre2-8.so.0 (0x00007f3c1a43a000)
/lib64/ld-linux-x86-64.so.2 (0x00007f3c1a71b000)Each line is needed name => found at (memory address). The last line is the loader itself. linux-vdso.so.1 has no path: the kernel provides it directly in memory. A missing library shows as libacme.so.2 => not found, which tells you exactly what to install or point to.
ldd works on libraries too (ldd /lib/x86_64-linux-gnu/libssl.so.3), and ldd -v adds symbol version details.
Warning
ldd may actually run part of the program to work out the answer. Don’t use it on files you don’t trust. objdump -p ./file | grep NEEDED shows the needed libraries without running anything.
/etc/ld.so.conf and ldconfig#
/etc/ld.so.conf lists extra folders to search. These days it’s usually one line that pulls in a whole folder of small files:
cat /etc/ld.so.conf
ls /etc/ld.so.conf.d/
cat /etc/ld.so.conf.d/libc.confinclude /etc/ld.so.conf.d/*.conf
libc.conf x86_64-linux-gnu.conf
# libc default configuration
/usr/local/libldconfig reads those folders plus the trusted defaults, creates the soname links it needs, and writes the lookup table to /etc/ld.so.cache. Run it as root after installing a library by hand or changing the config. Package managers run it for you.
| Command | What it does |
|---|---|
ldconfig | Rebuild links and the cache |
ldconfig -v | Same, and list every folder and library it handles |
ldconfig -p | Print what’s in the current cache (no root needed) |
ldconfig -n /some/dir | Only create links in that folder, don’t touch the cache |
ldconfig -p | grep libz libz.so.1 (libc6,x86-64) => /lib/x86_64-linux-gnu/libz.so.1ldconfig lives in /sbin (/usr/sbin), which isn’t always in a regular user’s PATH on Debian. Use /sbin/ldconfig -p if the short name isn’t found.
Making a new library folder permanent looks like this:
echo "/opt/acme/lib" | sudo tee /etc/ld.so.conf.d/acme.conf
sudo ldconfig
ldconfig -p | grep acmeLD_LIBRARY_PATH: a temporary override#
LD_LIBRARY_PATH is a colon-separated list of folders the loader checks first. It’s handy for testing, or for running an app that ships its own libraries, without touching system files:
LD_LIBRARY_PATH=/opt/acme/lib ./myapp # just this one run
export LD_LIBRARY_PATH=/opt/acme/lib:$LD_LIBRARY_PATH # this shell and its childrenIt’s per process and per user, so it’s not a fix for the whole system: use /etc/ld.so.conf.d/ for that.For security, the loader ignores LD_LIBRARY_PATH for setuid and setgid programs. Otherwise any user could make passwd load a fake library and run code as root. The same goes for its cousin LD_PRELOAD, which forces a library to load before all others.
Exam checklist#
- Shared libraries end in
.so(plus a version), static ones in.a. A program asks for a library by its soname, likelibc.so.6. - Typical locations:
/lib,/usr/lib,/lib64,/usr/lib64,/usr/lib/x86_64-linux-gnu,/usr/local/lib. - The dynamic loader is
ld.so/ld-linux.so.2/ld-linux-x86-64.so.2. ldd programlists the needed libraries and showsnot foundfor missing ones./etc/ld.so.conf(usuallyinclude /etc/ld.so.conf.d/*.conf) lists extra library folders.ldconfigrebuilds/etc/ld.so.cache; run it after changing the config.ldconfig -pprints the cache.LD_LIBRARY_PATHis searched before the cache and is only a per-process override, ignored for setuid programs.
Answer these 3 questions to commit this byte to your learning path.
Reset. This byte isn't committed any more. Answer the questions again to commit it.
$ git commit -m "learn(lpic-102): Manage shared libraries"
[main 9a49ecd] learn(lpic-102): Manage shared libraries
1 byte changed, 3 insertions(+)
See it on your path Already committed . Try the questions again any time, it won't undo anything. To uncommit it and start over: