# 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.

Source: https://aminbytes.dev/bytes/installation-and-packages/shared-libraries/


Sooner or later you'll see this:

```text
./myapp: error while loading shared libraries: libacme.so.2: cannot open shared object file: No such file or directory
```

The 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:

```bash
ls -l /usr/lib/x86_64-linux-gnu/libz.so*
```

```text
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.13
```

`libz.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:

1. Directories in the **`LD_LIBRARY_PATH`** environment variable
2. Directories baked into the program at build time (`RUNPATH`)
3. The cache file **`/etc/ld.so.cache`**
4. The defaults: `/lib` and `/usr/lib` (`/lib64` and `/usr/lib64` on 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`):

```bash
ldd /bin/ls
```

```text
	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:

```bash
cat /etc/ld.so.conf
ls /etc/ld.so.conf.d/
cat /etc/ld.so.conf.d/libc.conf
```

```text
include /etc/ld.so.conf.d/*.conf
libc.conf  x86_64-linux-gnu.conf
# libc default configuration
/usr/local/lib
```

**`ldconfig`** 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 |

```bash
ldconfig -p | grep libz
```

```text
	libz.so.1 (libc6,x86-64) => /lib/x86_64-linux-gnu/libz.so.1
```

`ldconfig` 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:

```bash
echo "/opt/acme/lib" | sudo tee /etc/ld.so.conf.d/acme.conf
sudo ldconfig
ldconfig -p | grep acme
```

## LD_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:

```bash
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 children
```

It's per process and per user, so it's not a fix for the whole system: use `/etc/ld.so.conf.d/` for that. (Side note: 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**, like `libc.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 program` lists the needed libraries and shows `not found` for missing ones.
- `/etc/ld.so.conf` (usually `include /etc/ld.so.conf.d/*.conf`) lists extra library folders.
- `ldconfig` rebuilds `/etc/ld.so.cache`; run it after changing the config. `ldconfig -p` prints the cache.
- `LD_LIBRARY_PATH` is searched before the cache and is only a per-process override, ignored for setuid programs.


## Byte check

1. You added `/opt/acme/lib` to a file in `/etc/ld.so.conf.d/`. Programs still can't find the library. What did you forget?
   - **Run `ldconfig` to rebuild `/etc/ld.so.cache`** (correct)
   - Reboot the machine
   - Run `ldd` on the program
   The loader reads the cache file, not the config files. `ldconfig` reads `/etc/ld.so.conf` and its includes and rebuilds `/etc/ld.so.cache`.

2. Which command lists the shared libraries a program needs, and where each one is found?
   - `ldconfig -v`
   - **`ldd /usr/bin/ssh`** (correct)
   - `lsmod`
   `ldd` prints each library the program needs and the path it resolves to, or `not found`. `lsmod` lists kernel modules, which are a different thing.

3. How do you make one program use libraries from `/home/dev/lib` without changing any system file?
   - Add the folder to `/etc/ld.so.conf`
   - **`LD_LIBRARY_PATH=/home/dev/lib ./myapp`** (correct)
   - Copy the program into `/home/dev/lib`
   `LD_LIBRARY_PATH` is searched before the system cache and only affects processes that have it in their environment. Editing `/etc/ld.so.conf` changes it for the whole system.


