Static and dynamic linking
When a program is linked statically, all of its library code is copied into the executable file, and the program can run without any other library present. When a program is linked dynamically, the executable records the names of the shared libraries it needs, and the loader connects them at startup. Dynamic linking saves disk space and memory, because many programs can share one copy of a library in memory. It also means that a program depends on the libraries that exist on the machine where it runs.
Most of the programs in a Linux system are dynamically linked. You can tell by reading the first bytes of the file with the file command, which names the interpreter for dynamic binaries.
file /bin/lsThe output says the file is an ELF executable and that it is dynamically linked, and it names the interpreter. The interpreter is the dynamic loader itself, and on this machine it is the musl loader for aarch64.
What ldd shows
The ldd program prints the shared libraries that a dynamic executable would load, and where each one was found. It does this by asking the loader to resolve the dependencies. Each line has a library name, an arrow, the path where it was found, and a load address. A line with only a path, starting with a slash, is the loader itself.
ldd /bin/lsThe output lists the C library and the loader, which are the two libraries almost every program needs. On this machine the loader is /lib/ld-musl-aarch64.so.1, because the Alpine base uses the musl C library. A Debian or Ubuntu system would show glibc instead. The format is the same either way.
ldd works by running the program loader with special settings, and on some systems it can execute the program. Do not run ldd on a binary you do not trust. Use objdump or readelf for untrusted files, when those tools are installed.
Where the loader looks
The loader searches a fixed set of directories, in an order that is easy to state. First come directories embedded in the program as an RPATH, when the program has one and no RUNPATH. Then come the directories in the LD_LIBRARY_PATH environment variable, then directories embedded as a RUNPATH, then the system library directories listed in the loader configuration, and finally the default library directories. The first match wins. That order explains why setting LD_LIBRARY_PATH can change which copy of a library a program uses, and why doing so is a common source of confusion.
Setting LD_LIBRARY_PATH is a quick test, but it is a poor permanent fix. It affects every program started from that shell, and it bypasses the normal rules of the system. The better habit is to install the library where the loader already looks, or to set a runpath when you build the program.
A missing library
When a library is missing, ldd prints a line that says not found in place of a path. That line is the answer to the question that caused the program to fail. The usual fixes are to install the package that provides the library, to correct the path in LD_LIBRARY_PATH, or to rebuild the program against the library that is actually installed.
A program that fails at startup with a loader error usually prints the name of the missing file. You can confirm the diagnosis by comparing that name with the output of ldd for the same program.
Two details trip people up. The first is that the loader search order applies to every library, including those that other libraries load. A library can be found through a path that you did not expect, because the directory that holds it was listed in a runpath baked into a different program. The second is that the same library name can refer to different files on different machines, and only the path shown by ldd tells you which one a program will use. When you debug a library problem, record the full path, not only the name.
A program that works in your terminal but fails under a service often has a different environment. The service may not set LD_LIBRARY_PATH, may run with a different working directory, or may use a different user with different file permissions. Compare the environment of the two runs before you change the libraries, because the cause is usually an environment difference rather than a missing file.
Libraries in a running process
The loaded libraries are not only visible through ldd. A running process maps them into its address space, and the kernel records the mappings in /proc/PID/maps. Reading that file shows the libraries for one live process, including any that were loaded later with the dlopen call, which ldd cannot see.
grep -c "\.so" /proc/$$/mapsThe command counts the mappings in your shell whose name contains .so, which is the usual suffix of a shared object. The count varies, but it is never zero for a bash process, because bash loads its own libraries at startup.
Hands-on: inspect the loader
Save the library list of two programs in files. The first is ls, and the second is bash, which is a larger program with more dependencies. Compare the two outputs afterward.
ldd /bin/ls > ldd-ls.txt
ldd /bin/bash > ldd-bash.txt
wc -l ldd-ls.txt ldd-bash.txtThe line counts show how many libraries each program needs. Open the files and look for the loader line in each, which is the one that starts with a slash and has no arrow.