Menu

Linux Installation and Package Management

Step 4 of 6 LPIC-1 102.4weight 3

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

View .md

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:

  • dpkg is the low-level tool. It installs, removes and queries .deb files 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 calls dpkg to 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:

text
nginx_1.22.1-9_amd64.deb
  │      │    │   └ architecture (amd64, arm64, i386, or all)
  │      │    └ Debian revision of the packaging
  │      └ upstream version
  └ package name

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

CommandWhat it does
dpkg -i file.debInstall (or upgrade) a package file
dpkg -r packageRemove a package, keep its config files
dpkg -P packagePurge: remove it and its config files
dpkg -l [pattern]List packages with their status
dpkg -s packageStatus and details of an installed package
dpkg -L packageList the files an installed package owns
dpkg -S /path/or/patternFind which installed package owns a file
dpkg -I file.debShow the control info of a package file (not installed)
dpkg -c file.debList the contents of a package file
dpkg -V [package]Verify installed files against their checksums
dpkg --configure -aFinish configuring packages left half-done
dpkg --get-selectionsList all packages and their selection state

Install a downloaded package, then see its status:

bash
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 tree
text
Selecting 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 color

The 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 -P or apt-get purge cleans it up.
  • iU or iF: an install broke halfway. dpkg --configure -a usually 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:

bash
sudo apt-get install -f

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

bash
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 ones

apt-get: install from repositories#

CommandWhat it does
apt-get updateDownload fresh package lists from the repositories. Do this first.
apt-get upgradeUpgrade installed packages, never adding or removing any
apt-get dist-upgradeUpgrade and allow adding or removing packages (needed for new kernels, release upgrades)
apt-get install pkgInstall a package and its dependencies. Also upgrades a single package.
apt-get install ./file.debInstall a local file and fetch its dependencies
apt-get install --reinstall pkgReinstall the same version
apt-get remove pkgRemove, keep config files
apt-get purge pkgRemove including config files
apt-get autoremoveRemove dependencies nothing needs any more
apt-get clean / autocleanEmpty the download cache /var/cache/apt/archives/ (autoclean: only files that can’t be downloaded again)
apt-get download pkgDownload the .deb to the current folder without installing
apt-get source pkgDownload the source package (needs deb-src lines)

Useful options: -y (answer yes), -s (simulate: show what would happen), -d (download only), --no-install-recommends.

bash
sudo apt-get update
sudo apt-get install curl
text
Reading 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.

CommandWhat it shows
apt-cache search wordPackages whose name or description matches
apt-cache show pkgFull details: version, size, dependencies, description
apt-cache showpkg pkgVersions, dependencies and reverse dependencies, in raw form
apt-cache depends pkgWhat the package needs
apt-cache rdepends pkgWhat needs the package
apt-cache policy pkgInstalled version, candidate version, and which repository each comes from
apt-cache statsNumbers about the package cache
bash
apt-cache policy curl
text
curl:
  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 Packages

apt: 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.

aptOlder equivalent
apt update, apt upgrade, apt install, apt remove, apt purge, apt autoremoveapt-get with the same word
apt full-upgradeapt-get dist-upgrade
apt search, apt showapt-cache search, apt-cache show
apt list --installed, apt list --upgradabledpkg -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:

/etc/apt/sources.listtext
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 main

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

/etc/apt/sources.list.d/ubuntu.sourcestext
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.gpg

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

bash
dpkg -S libz.so.1
dpkg -L zlib1g
text
zlib1g: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:

bash
sudo apt-get install apt-file
sudo apt-file update
apt-file search libssl.so.3
text
libssl3: /usr/lib/x86_64-linux-gnu/libssl.so.3

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

bash
sudo dpkg -V openssh-server
text
??5?????? c /etc/ssh/sshd_config

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

  • dpkg works on local .deb files with no dependency resolution. APT downloads and resolves dependencies.
  • dpkg -i install, -r remove (keeps config), -P purge, -l list, -s status, -L files of a package, -S package of a file, -I and -c on a .deb file, -V verify, --configure -a finish.
  • dpkg -l status: ii installed, rc removed with config left.
  • dpkg-reconfigure pkg asks the debconf questions again (tzdata, locales).
  • apt-get update refreshes lists; upgrade never adds or removes packages, dist-upgrade may. install -f fixes broken dependencies. remove vs purge, autoremove, clean.
  • apt-cache search, show, showpkg, depends, rdepends, policy.
  • apt is the interactive front end: apt full-upgrade = apt-get dist-upgrade, plus apt list --installed.
  • Repositories: /etc/apt/sources.list and /etc/apt/sources.list.d/, lines like deb URL suite components. Run apt-get update after a change.
  • Package of a file: dpkg -S (installed), apt-file search (not installed).

Byte check

0/3

Answer these 3 questions to commit this byte to your learning path.

  1. You ran dpkg -i app.deb and it failed with unmet dependencies. What’s the quickest fix?
  2. Which command tells you which installed package owns /usr/bin/curl?
  3. dpkg -l nginx shows rc at the start of the line. What does that mean?
Search posts and bytes

Optional account

Keep your progress everywhere

Everything on AminBytes is free to read, with or without an account. Sign in only if you want your commits, streak and merged topics saved, on every device.

or

We keep only your name, avatar and which bytes you finished, and you can delete it all anytime. Privacy