Shared libraries and building from source
How the dynamic linker finds shared libraries, what sonames are for, ldd and ldconfig, and how to build software from source without making a mess of /usr.
Packages, repositories and dependenciesKernel space, user space and system calls
Shared libraries and building from source
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A dynamically linked program records the libraries it needs as NEEDED entries naming sonames, such as libc.so.6. At start-up the dynamic linker, ld-linux, finds and maps them before main runs.
- The soname carries the ABI version. libz.so.1 is a symlink to the real file libz.so.1.3.1, so compatible updates replace the file while programs keep asking for the same name.
- The linker searches RPATH, LD_LIBRARY_PATH, RUNPATH, then /etc/ld.so.cache, then the default directories. ldconfig rebuilds that cache from /etc/ld.so.conf.d after you add libraries.
- ldd lists what a binary would load, but can execute code in some cases. Never run it on an untrusted file; objdump -p shows NEEDED entries without running anything.
- Build from source into /usr/local (the default prefix for ./configure && make && make install) or /opt, never into /usr. Better still, build a real package so the package manager can track and remove it.
Why share code at all?
Almost every program on a server uses the C library, many use OpenSSL and zlib. If each program carried its own copy, a fix for an OpenSSL hole would mean rebuilding every program that contains it. Shared libraries avoid that: one file on disk, mapped into every process that needs it, replaced once when a fix arrives. That is the model the distributions in this group are built on, and it is why the packages lesson showed nginx depending on libssl3t64 instead of containing it.
The price is that a program no longer runs on its own. Something must find the right libraries every time it starts.
What a binary asks for
A dynamically linked ELF file names two things: an interpreter, the dynamic linker, and a list of libraries. file on the Debian 13 image’s ls, run on a Mac, shows the first:
usr/bin/ls: ELF 64-bit LSB pie executable, x86-64, ... dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, ...
and objdump -p from Apple’s developer tools, which reads Linux ELF, shows the second:
$ objdump -p usr/bin/ls | grep NEEDED
NEEDED libselinux.so.1
NEEDED libcap.so.2
NEEDED libc.so.6
When you run ls, the kernel’s execve loads the interpreter named in the file. ld-linux then finds each NEEDED library, maps it, resolves symbols, and only then jumps to the program. This happens in user space, which is why the system calls lesson could show glibc functions as ordinary code.
Sonames
The names in NEEDED are sonames, which carry the library’s ABI version. The number changes only when the library breaks compatibility. In the image:
$ objdump -p usr/lib/x86_64-linux-gnu/libz.so.1.3.1 | grep SONAME
SONAME libz.so.1
libz.so.1 -> libz.so.1.3.1
The real file is libz.so.1.3.1; the soname libz.so.1 is a symlink to it. A bug-fix release ships libz.so.1.3.2 and moves the link, and every program keeps working. An incompatible zlib 2 would be libz.so.2, installable alongside version 1, so old and new programs can coexist. That is also why Debian names library packages after the soname (libssl3t64, zlib1g) and why RPM records requirements like libc.so.6(GLIBC_2.34)(64bit), as the dnf lesson showed.
Where the linker looks
ld.so(8) gives the search order for a name with no slash:
DT_RPATHin the binary, if there is noDT_RUNPATH;LD_LIBRARY_PATH, ignored for setuid programs (“secure-execution mode”);DT_RUNPATHin the binary;- the cache file
/etc/ld.so.cache; - the default directories,
/libthen/usr/lib(or their 64-bit equivalents).
The cache is built by ldconfig, which ldconfig(8) says “creates the necessary links and cache to the most recent shared libraries found” in /etc/ld.so.conf and the trusted directories. In the Debian image, /etc/ld.so.conf just includes /etc/ld.so.conf.d/*.conf, which list /usr/local/lib, /usr/local/lib/x86_64-linux-gnu, /lib/x86_64-linux-gnu and /usr/lib/x86_64-linux-gnu. After installing a library yourself, run sudo ldconfig. For a library in a new directory, add a .conf file naming it first.
LD_LIBRARY_PATH is for testing and odd cases, not configuration: set globally it affects every program and is a classic source of “works in my shell, fails under systemd”.
ldd, and a warning
ldd ./program prints each library the linker would load and where it found it, and is the quickest check for “not found”. Its man page carries a security note worth knowing: in some circumstances ldd “may attempt to obtain the dependency information by attempting to directly execute the program”, and so “you should never employ ldd on an untrusted executable”. Use objdump -p or readelf -d on files you do not trust.
Building from source
Sometimes you need a version or build option no package offers. The classic autotools sequence, written from convention and the Chris Titus video in deeper, not run here:
sudo apt install build-essential # or: sudo dnf group install development-tools
./configure --prefix=/usr/local # checks dependencies, writes Makefiles
make -j"$(nproc)"
sudo make install # copies files under the prefix
/usr/local is the default prefix for autotools projects and the right place: the UAPI file-hierarchy spec and hier(7) reserve /usr for the distribution, and as the Debian config above shows, /usr/local/lib is already on the linker’s path. Missing -dev or -devel packages, which hold headers and the unversioned libfoo.so link used at build time, are the usual reason configure fails.
The weakness of make install is the one from the packages lesson: nothing records which files it wrote. Better options, roughly in order:
- Build a package. Rebuild the distribution’s own source package with your change (
apt sourceanddpkg-buildpackage, orrpmbuildfrom a source RPM). It installs, upgrades and removes like any other. - Use a package-generating wrapper. Tools in the
checkinstalltradition, orfpm, watch or take a staged install and wrap it into a.debor.rpm. - Stage the install. Most Makefiles accept
make DESTDIR=/tmp/stage install, so you can see exactly what would be installed, then package or copy it. - Keep it out of the system. Install under
/opt/<name>with its own prefix, or ship it in a container.
About the video
The embed is Bit Byte Talks’s “Static vs Dynamic linking explained for Beginners”, October 2026, 11 minutes, in full. It is an animated explanation of what libraries are, static versus dynamic linking and their trade-offs, managing dynamic dependencies, debugging with ldd, position-independent code and lazy loading. It does not cover sonames in depth, ldconfig, or building from source, which this lesson adds, and it says nothing of ldd’s security caveat. It has no description; I judged it from its chapters, and English from the original English audio track. Chris Titus’s build-from-source walkthrough (2019) and theurbanpenguin’s 2014 shared library demo are in deeper.
What was run: file and objdump -p on binaries and libraries from the downloaded Debian 13 image, and reading its ld.so.conf files, on a Mac. The search order and warnings are from ld.so(8), ldconfig(8) and ldd(1) on man7.org, checked 2026-11-28. The build commands and the package-wrapper tools are from general knowledge, not run or re-checked.
Check yourself
A program fails with 'error while loading shared libraries: libfoo.so.2: cannot open shared object file'. What are the usual fixes?
Install the package that provides that soname, or, if you installed the library yourself in a non-standard directory, add the directory to a file in /etc/ld.so.conf.d and run ldconfig. LD_LIBRARY_PATH works for a quick test.
Why does /usr/lib/x86_64-linux-gnu contain libz.so.1 as a symlink rather than the library itself?
Programs ask for the soname libz.so.1. The symlink points at the actual versioned file, so a compatible update can replace the file and move the link without relinking anything.
Why should you not run ldd on a binary someone emailed you?
ldd may run the program's ELF interpreter, or the program itself, to find its dependencies. objdump -p reads the NEEDED entries without executing it.
Why install hand-built software under /usr/local rather than /usr?
/usr belongs to the package manager, which can overwrite or conflict with files you put there. /usr/local is left alone by packages and is already on the default search paths.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.