Menu
Use Debian package management
“Install, upgrade, remove and inspect .deb packages with dpkg, apt-get, apt-cache and apt, and find which package holds a file.”
On Debian, Ubuntu, Linux Mint and their relatives, almost every piece of software arrives as a .deb package. Packages bring tidy installs, clean removals, automatic security updates, and a database that knows which file belongs to what. You use the tools every day, and the exam goes deep: commands, options and status codes.
There are two layers:
dpkgis the low-level tool. It installs, removes and queries.debfiles that are already on your disk. It doesn’t download anything and doesn’t resolve dependencies.- APT (
apt-get,apt-cache,apt) sits on top. It downloads packages from repositories, works out dependencies and then callsdpkgto do the actual install.
Follow along in a container: docker run -it --rm debian (you’re root there, so drop the sudo). Run apt-get update first: container images ship without a package list.
What a .deb is#
A package file name tells you a lot:
nginx_1.22.1-9_amd64.deb
│ │ │ └ architecture (amd64, arm64, i386, or all)
│ │ └ Debian revision of the packaging
│ └ upstream version
└ package nameInside is a compressed archive of files to install, plus control data: the description, dependencies (Depends, Recommends, Conflicts), and scripts that run before and after install or removal. dpkg records everything it installs in its database under /var/lib/dpkg/: status holds the state of every package and info/<package>.list the files each one owns.
dpkg: working with .deb files#
| Command | What it does |
|---|---|
dpkg -i file.deb | Install (or upgrade) a package file |
dpkg -r package | Remove a package, keep its config files |
dpkg -P package | Purge: remove it and its config files |
dpkg -l [pattern] | List packages with their status |
dpkg -s package | Status and details of an installed package |
dpkg -L package | List the files an installed package owns |
dpkg -S /path/or/pattern | Find which installed package owns a file |
dpkg -I file.deb | Show the control info of a package file (not installed) |
dpkg -c file.deb | List the contents of a package file |
dpkg -V [package] | Verify installed files against their checksums |
dpkg --configure -a | Finish configuring packages left half-done |
dpkg --get-selections | List all packages and their selection state |
Install a downloaded package, then see its status:
apt-get download tree # saves tree_2.1.0-1_amd64.deb in the current folder
dpkg -I tree_*.deb
sudo dpkg -i tree_*.deb
dpkg -l treeSelecting previously unselected package tree.
(Reading database ... 6090 files and directories currently installed.)
Preparing to unpack tree_2.1.0-1_amd64.deb ...
Unpacking tree (2.1.0-1) ...
Setting up tree (2.1.0-1) ...
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name Version Architecture Description
+++-==============-============-============-=====================================
ii tree 2.1.0-1 amd64 displays an indented directory tree, in colorThe first two letters matter. The first is what you want (i install, r remove, p purge, h hold, u unknown), the second what the package is (i installed, c only config files left, n not installed, U unpacked, F half-configured, H half-installed). So:
ii: installed and fine.rc: removed, config files still there.dpkg -Porapt-get purgecleans it up.iUoriF: an install broke halfway.dpkg --configure -ausually finishes it.
dpkg -s bash shows the same state in words (Status: install ok installed), plus version, size, dependencies and description.
When dpkg -i fails because a dependency is missing, the package is left unconfigured. Let APT fetch what’s missing:
sudo apt-get install -fdpkg-reconfigure#
Many packages ask questions while they install, like your timezone, keyboard layout or locale. A system called debconf asks them and stores the answers. dpkg-reconfigure asks them again for a package that’s already installed and applies the new answers:
sudo dpkg-reconfigure tzdata # pick a new timezone
sudo dpkg-reconfigure locales # choose and generate locales
sudo dpkg-reconfigure -plow keyboard-configuration # show every question, even the minor onesapt-get: install from repositories#
| Command | What it does |
|---|---|
apt-get update | Download fresh package lists from the repositories. Do this first. |
apt-get upgrade | Upgrade installed packages, never adding or removing any |
apt-get dist-upgrade | Upgrade and allow adding or removing packages (needed for new kernels, release upgrades) |
apt-get install pkg | Install a package and its dependencies. Also upgrades a single package. |
apt-get install ./file.deb | Install a local file and fetch its dependencies |
apt-get install --reinstall pkg | Reinstall the same version |
apt-get remove pkg | Remove, keep config files |
apt-get purge pkg | Remove including config files |
apt-get autoremove | Remove dependencies nothing needs any more |
apt-get clean / autoclean | Empty the download cache /var/cache/apt/archives/ (autoclean: only files that can’t be downloaded again) |
apt-get download pkg | Download the .deb to the current folder without installing |
apt-get source pkg | Download the source package (needs deb-src lines) |
Useful options: -y (answer yes), -s (simulate: show what would happen), -d (download only), --no-install-recommends.
sudo apt-get update
sudo apt-get install curlReading package lists... Done
Building dependency tree... Done
The following additional packages will be installed:
ca-certificates libcurl4 libssl3 ...
The following NEW packages will be installed:
ca-certificates curl libcurl4 libssl3 ...
0 upgraded, 21 newly installed, 0 to remove and 0 not upgraded.
Need to get 4,912 kB of archives.
Do you want to continue? [Y/n]To stop a package from being upgraded, put it on hold with sudo apt-mark hold nginx (and apt-mark unhold to release it).
apt-cache: ask the package lists#
apt-cache reads the local copy of the repository lists, so it needs no root and works for packages you haven’t installed.
| Command | What it shows |
|---|---|
apt-cache search word | Packages whose name or description matches |
apt-cache show pkg | Full details: version, size, dependencies, description |
apt-cache showpkg pkg | Versions, dependencies and reverse dependencies, in raw form |
apt-cache depends pkg | What the package needs |
apt-cache rdepends pkg | What needs the package |
apt-cache policy pkg | Installed version, candidate version, and which repository each comes from |
apt-cache stats | Numbers about the package cache |
apt-cache policy curlcurl:
Installed: (none)
Candidate: 7.88.1-10+deb12u8
Version table:
7.88.1-10+deb12u8 500
500 http://deb.debian.org/debian bookworm/main amd64 Packages
500 http://deb.debian.org/debian-security bookworm-security/main amd64 Packagesapt: the friendly front end#
apt combines the most used parts of apt-get and apt-cache with a progress bar and color. It’s what you’ll type day to day; scripts should keep using apt-get, whose output stays stable.
| apt | Older equivalent |
|---|---|
apt update, apt upgrade, apt install, apt remove, apt purge, apt autoremove | apt-get with the same word |
apt full-upgrade | apt-get dist-upgrade |
apt search, apt show | apt-cache search, apt-cache show |
apt list --installed, apt list --upgradable | dpkg -l, apt-get -s upgrade |
One difference: apt upgrade may install new dependencies, while apt-get upgrade never does. aptitude is another, older front end with a text-mode menu; you may see it mentioned.
Repositories: /etc/apt/sources.list#
APT downloads from the repositories listed in /etc/apt/sources.list and in files under /etc/apt/sources.list.d/. The classic one-line format:
deb http://deb.debian.org/debian bookworm main contrib non-free non-free-firmware
deb http://deb.debian.org/debian-security bookworm-security main
deb-src http://deb.debian.org/debian bookworm mainEach line is: type (deb for binary packages, deb-src for source), the mirror URL, the suite (release name like bookworm or noble, or stable), then one or more components. Debian has main (free software), contrib, non-free and non-free-firmware. Ubuntu uses main, restricted, universe and multiverse.
Newer systems use the multi-line deb822 format in .sources files instead. Ubuntu 24.04 ships /etc/apt/sources.list.d/ubuntu.sources, and the Debian container has debian.sources:
Types: deb
URIs: http://archive.ubuntu.com/ubuntu/
Suites: noble noble-updates noble-backports
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpgAfter changing either, run apt-get update. APT checks the repository’s GPG signature on every update and refuses lists it can’t verify. Signed-By (or [signed-by=...] in one-line format) names the key that’s allowed to sign that repository. Extra settings for APT itself go in /etc/apt/apt.conf.d/.
Find which package has a file#
For installed packages, dpkg knows every file:
dpkg -S libz.so.1
dpkg -L zlib1gzlib1g:amd64: /lib/x86_64-linux-gnu/libz.so.1.2.13
zlib1g:amd64: /lib/x86_64-linux-gnu/libz.so.1
/.
/lib
/lib/x86_64-linux-gnu
/lib/x86_64-linux-gnu/libz.so.1.2.13
/usr
/usr/share
/usr/share/doc
...Tip
On “merged /usr” systems, /bin is a link to /usr/bin, and older packages still record the /bin path. If dpkg -S /usr/bin/ls says no path found, try dpkg -S /bin/ls, or a pattern that matches both: dpkg -S '*bin/ls'.
For packages that aren’t installed, use apt-file, which downloads the file lists of the whole repository:
sudo apt-get install apt-file
sudo apt-file update
apt-file search libssl.so.3libssl3: /usr/lib/x86_64-linux-gnu/libssl.so.3You can also search on packages.debian.org or packages.ubuntu.com.
Check a package’s integrity#
dpkg -V (or --verify) compares installed files with the checksums recorded at install time. No output means nothing changed:
sudo dpkg -V openssh-server??5?????? c /etc/ssh/sshd_configThe 5 means the file’s checksum differs and c marks a config file, so here someone simply edited sshd_config. Changes to files that aren’t config files deserve a closer look. The debsums package offers the same check with friendlier output.dpkg -V borrowed its output format from rpm -V, which you’ll meet in the next byte. The ? marks are checks dpkg doesn’t do, like file owner and permissions.
Exam checklist#
dpkgworks on local.debfiles with no dependency resolution. APT downloads and resolves dependencies.dpkg -iinstall,-rremove (keeps config),-Ppurge,-llist,-sstatus,-Lfiles of a package,-Spackage of a file,-Iand-con a.debfile,-Vverify,--configure -afinish.dpkg -lstatus:iiinstalled,rcremoved with config left.dpkg-reconfigure pkgasks the debconf questions again (tzdata,locales).apt-get updaterefreshes lists;upgradenever adds or removes packages,dist-upgrademay.install -ffixes broken dependencies.removevspurge,autoremove,clean.apt-cache search,show,showpkg,depends,rdepends,policy.aptis the interactive front end:apt full-upgrade=apt-get dist-upgrade, plusapt list --installed.- Repositories:
/etc/apt/sources.listand/etc/apt/sources.list.d/, lines likedeb URL suite components. Runapt-get updateafter a change. - Package of a file:
dpkg -S(installed),apt-file search(not installed).
Answer these 3 questions to commit this byte to your learning path.
Reset. This byte isn't committed any more. Answer the questions again to commit it.
$ git commit -m "learn(lpic-102): Use Debian package management"
[main d1b0e04] learn(lpic-102): Use Debian package management
1 byte changed, 3 insertions(+)
See it on your path Already committed . Try the questions again any time, it won't undo anything. To uncommit it and start over: