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.
- 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.
$ 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.
$ 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.
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. 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.
$ 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.
- 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.
- 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.
- "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."