DEVOPS, SRE AND CLOUD · PART 1 · LINUX, FROM THE INSIDE · SOFTWARE · 17 OF 187

Linux Packages, Building From Source and Libraries

How software gets onto a Linux server: package managers (apt and dpkg, dnf and rpm), repositories and dependencies (curl needs libc6, libcurl4t64 and zlib1g; 1,091 packages installed on our machine), building from source with a compiler and make, shared libraries and ldd (curl links 33 lines of libraries), and static builds: our hello program was 15,960 bytes dynamic and 785,336 bytes static.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
1 / 11

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • apt + dpkg, dnf + rpm
  • update first
  • dpkg -S, -L
  • make tracks changes
  • ldd
  • static vs dynamic

Notes

Package managers

A package is an archive of files plus metadata: version, dependencies and install scripts. Debian and Ubuntu use apt as the front end and dpkg underneath, with dot deb files. Red Hat, Fedora and Rocky use dnf and rpm. Alpine uses apk. The front end reads a repository's index, works out dependencies, downloads the packages and checks their signatures; the back end unpacks the files and runs the install scripts.

Two families
  • Debian, Ubuntu: apt (front) + dpkg (back) .deb files
  • RHEL, Fedora, Rocky: dnf (front) + rpm (back) .rpm files
  • Alpine: apk
  • repositories are signed; apt checks the signature

Daily commands

The daily commands. apt update refreshes the index, so always run it first. apt install installs, with minus y in scripts so it does not stop to ask. List upgradable and upgrade keep a server patched. apt cache policy shows which versions are available and which one would be installed. apt mark hold pins a version so an upgrade cannot change it, and purge removes a package along with its config files.

apt
$ sudo apt update      refresh index
$ sudo apt install -y curl
$ apt list --upgradable
$ sudo apt upgrade
$ apt-cache policy nginx
  versions available, which wins
$ sudo apt-mark hold nginx
  pin a version
$ sudo apt purge pkg  + config

Ask the package database

The package database answers questions about a server. Our machine had one thousand and ninety one packages installed. dpkg minus S tells you which package owns a file: slash usr slash bin slash curl belongs to the curl package. apt cache depends shows what a package needs: curl needs the C library, libcurl and zlib. And dpkg minus L lists every file a package installed, as you saw for nginx in episode three.

Our machine
$ dpkg -l | grep -c ^ii
1091     installed packages
$ dpkg -S /usr/bin/curl
curl: /usr/bin/curl
$ apt-cache depends curl
  Depends: libc6
  Depends: libcurl4t64
  Depends: zlib1g
$ dpkg -L curl   its files

Build from source

Building from source means turning source code into a program. We wrote a five line C program and a Makefile with one rule: hello depends on hello dot c, and is built by running gcc. make ran gcc, and the program printed hello from source. Run make again and it says hello is up to date, because make compares file times and only rebuilds what changed. Real projects add a configure step first, then make and make install, usually into usr local.

Our build
hello.c: printf("hello from
  source\n")
Makefile:
hello: hello.c
	gcc -O2 -o hello hello.c
$ make hello
$ ./hello
hello from source
$ make hello
make: 'hello' is up to date.

Shared libraries

Most programs are dynamically linked: they load shared library files, ending in dot s o, when they start. ldd lists them. Our hello needed the C library and the dynamic loader. curl needed thirty three entries, including libcurl and zlib. When a program fails with error while loading shared libraries, one of these is missing or the wrong version, and ldd shows which. That is the classic copy-a-binary-to-another-server failure.

ldd (our runs)
$ ldd ./hello
  linux-vdso.so.1
  libc.so.6 => /lib/x86_64-
    linux-gnu/libc.so.6
  /lib64/ld-linux-x86-64.so.2
$ ldd /usr/bin/curl | wc -l
33
"error while loading shared
  libraries" = one is missing

Static builds

The alternative is a static build: all the library code copied into the program itself. We built hello both ways. The dynamic one was fifteen thousand nine hundred and sixty bytes; the static one was seven hundred and eighty five thousand, about fifty times bigger, and ldd says it is not a dynamic executable. Static programs run anywhere with the same CPU and kernel, which is why Go tools and tiny container images like them. Episode six of part three builds one.

Same program, two ways
$ gcc -O2 -o hello hello.c
$ gcc -O2 -static -o
    hello-static hello.c
hello          15,960 bytes
hello-static  785,336 bytes
$ ldd ./hello-static
not a dynamic executable

Go builds static by default

Which to use

Which should you use? Distribution packages first: security fixes arrive with normal updates. A vendor's own repository when you need a newer version, which is common for nginx, PostgreSQL and Docker. Build from source only when you need a feature or fix that packages lack, knowing that you now own every future security update. And in modern setups, build once in CI into a container image, rather than compiling on servers.

Rules of thumb
  • distro package: patched by the distro, easy to update
  • vendor repo: newer versions (nginx, PostgreSQL, Docker)
  • source build: only for a feature or fix you need; you now own the updates
  • containers: build once in CI, never compile on servers

Common mistakes

Mistakes. Installing without updating the index first, and getting old versions or errors. Piping scripts from unknown sites straight into a shell. Copying a binary to another server and hitting missing libraries. Compiling on production servers. Unpinned package versions in container images, so two builds differ. And make install writing over files the package manager owns in usr.

Avoid these
  • apt install without apt update
  • curl | sh from unknown sites
  • copying binaries between servers (missing .so)
  • compiling on production
  • unpinned versions in images
  • make install over package files in /usr

Say it like this

In the interview: the program is dynamically linked and the loader cannot find one of its shared libraries. ldd shows which one is missing. I install the package that provides it, found with apt file search or dnf provides, or fix the library path if it is installed somewhere unusual. For tools I need to move between machines, I prefer a static build or a container image.

In the interview
  • "The binary is dynamically
  • linked and the loader cannot
  • find a .so. ldd shows which; I
  • install the package that owns it
  • (apt-file or dnf provides) or fix the library path. For portable tools I prefer static
  • builds or a container image."

Interview questions

Q1What is the difference between apt and dpkg?

dpkg installs and queries individual .deb packages; apt is the front end that reads repositories, resolves dependencies, downloads and calls dpkg.

Q2How do you find which package owns a file?

dpkg -S /path on Debian/Ubuntu, rpm -qf /path on RHEL-based systems.

Q3What causes "error while loading shared libraries"?

A dynamically linked program cannot find one of its .so files; ldd shows which. Install the providing package or fix the library path.

Q4What is the difference between static and dynamic linking?

Dynamic programs load shared libraries at start-up (smaller, share updates); static programs include the library code (bigger, portable, but must be rebuilt for library fixes).

Q5What does make do?

Runs build commands from a Makefile, rebuilding a target only when its dependencies are newer.

Q6How do you stop a package from being upgraded?

apt-mark hold <pkg> on Debian and Ubuntu, or dnf versionlock on RHEL-based systems; unhold it when you are ready to upgrade.

Q7Why prefer distribution packages over source builds?

They receive security updates through normal patching; a source build makes you responsible for every update.

Q8Why run apt update before apt install?

To refresh the local package index so apt knows the current versions and locations.