$_ CLI In Depth
Learning progress 0%
lesson packages services cron

Package Managers: apk and apt

6 min read Module 11: Packages, Services & Scheduled Jobs Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:18

What a package manager keeps

A package is an archive of files plus metadata. The metadata includes a name, a version, an architecture, a list of dependencies, and often a list of the files it installs. The package manager keeps a local copy of that metadata for everything installed on the machine. It also keeps an index of the packages available from remote repositories, so it can find an update or a dependency that is not yet on disk.

Those two pieces explain most of the behavior you see. The installed database answers questions about this machine. The repository index answers questions about what could be installed. A command that changes the system needs both, and a command that only reads the installed database can work offline.

apk on Alpine

Alpine Linux uses apk. Its main subcommands are add to install, del to remove, upgrade to update, and info to query. Repositories are listed in /etc/apk/repositories. Installing and removing need administrative rights, so an ordinary user runs only the query commands.

The installed database is a plain text file, /lib/apk/db/installed. Each package is a block of lines. A line that starts with P: gives the package name, a V: line gives its version, an A: line gives the architecture, and an S: line gives the compressed size. You can read the block for one package directly, which is useful when a query command is not available.

BASH2 lines
grep -c "^P:" /lib/apk/db/installed
grep -A1 "^P:bash$" /lib/apk/db/installed

The first command counts the installed packages, one P: line each. The second prints the name line for bash and the line after it, which holds the version. The output is the same for every user, and it needs no network.

Warning

Some apk query subcommands consult the repository index, and when the machine has no network they print a warning about fetching it. Read the local database when you only need facts about installed software. Keep the repository commands for machines that are online and that you are allowed to change.

apt on Debian and Ubuntu

Debian and Ubuntu use apt-get and apt for the same kinds of work, with dpkg underneath as the low-level tool that unpacks a single package. The installed database is /var/lib/dpkg/status, which has a similar block format. The dpkg -S command finds the package that owns a file, and dpkg -L lists the files of a package. Those two commands answer the questions you usually ask of apk.

This course machine uses apk, so the commands above are the ones you can run. The apt commands are described here so that you recognize them on other systems. A command written for one distribution rarely runs unchanged on another, because the package names, the repository layout, and the database paths differ.

Ownership: which package owns this file

The most useful question to ask about a file is which package installed it. If a file is missing or changed, you need to know whether the package manager put it there, so that you can reinstall the package rather than guess. On Alpine the answer comes from the F: and R: lines of the installed database, where a directory line is followed by file lines. On Debian the same question is answered by dpkg -S.

You can find the package for bash in the database by reading its block. Its files are listed below its header, which is useful when you want to know what a package has placed on disk.

BASH1 lines
grep -A3 "^P:bash$" /lib/apk/db/installed

The block contains a few header lines, and the file lists appear further down. The exact lines depend on the package version, so read them in the lab instead of memorizing them.

It helps to think of the installed database as a ledger that the package manager writes to in a single careful step. When you install a package, the manager adds its file list, its version, and its dependency links in one transaction. When you remove one, it removes the entries in the reverse order. A half-finished change leaves the ledger inconsistent, which is why package managers take a lock while they work and why a second command may wait or refuse to run. If you find a lock file left behind after an interrupted run, do not delete it until you have checked that no manager process is still running.

Version numbers deserve a note too. A version such as 5.3.9-r1 has two parts: the upstream version of bash and a revision number that the Alpine maintainers add when they rebuild the package. Two installed packages with the same upstream version but different revisions are not the same build, so compare the whole string when you check whether a fix is present.

Dependencies and why removal is safe

A package can depend on other packages, and the package manager refuses to remove a package that other installed packages need. That is the reason a removal may report that a second package must go too. The database records each dependency, so the manager can answer the question before it changes anything. Reading the database gives you the same picture, although you will need to follow the links by hand.

Tip

Before you remove a package on a real system, ask the manager what depends on it. The answer is cheaper than repairing a system that lost a library it needed.

Hands-on: read the installed database

Work through the database from the outside in. Count the packages, read the block for one package, and then save the count and the bash version to files. The checks in the lab read those files, so run the commands in /workspace.

BASH3 lines
grep -c "^P:" /lib/apk/db/installed > count.txt
grep -A1 "^P:bash$" /lib/apk/db/installed > bash-pkg.txt
cat count.txt bash-pkg.txt

The first file holds one number, which is the count of installed packages. The second holds the package name and the line that starts with V:. Together they give you a small, offline inventory of the machine, which is the first step of most package investigations.

Terminal lab · 2 steps · real Linux in your browser

Read the installed package database

Count the installed packages and read one package block, using only the local database.

01
Count the installed packages
Each installed package has one P: line in the Alpine database. grep -c counts those lines, and the result goes into count.txt. ``bash grep -c "^P:" /lib/apk/db/installed > count.txt `` The file holds a single number, which is the count of installed packages.
Show hint for step 1
Use grep -c with the anchored pattern ^P: and redirect the count to count.txt.
02
Read the block for bash
Save the name line for bash and the line after it, which holds the version, in bash-pkg.txt. ``bash grep -A1 "^P:bash$" /lib/apk/db/installed > bash-pkg.txt `` The file should show P:bash and then a V: line.
Show hint for step 2
Use grep -A1 with the exact pattern ^P:bash$ so that only the bash block matches.
Check yourself Question 1 of 4
Concept Check Alpine installed database recall

Which file is the installed-package database on Alpine Linux?

Recap & Key Takeaways

A package manager keeps an installed database and a repository index, and the two answer different questions.
On Alpine, /lib/apk/db/installed is a plain text file in which each P: line starts a package block with its V: version.
Some apk query commands consult the repository index, so read the installed database when the machine is offline.
dpkg -S and dpkg -L answer the ownership and file-list questions on Debian and Ubuntu.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue