Package Management with apt Beginner 30 min
Lab overview

Package Management with apt

30 min 4 checked steps Beginner
The situation

The shop team needs a web server and two small utilities on a fresh Ubuntu machine, and an old monitoring agent that was replaced months ago has to go, config and all. You'll do it exactly as on a production server, with apt and dpkg, from a real package repository built into the lab machine.

By the end of this lab you will be able to
  • Refresh the package index and search for software with apt
  • Install packages and find out exactly which files they added
  • Find which package a file belongs to with dpkg
  • Remove and purge packages and clean up unused dependencies

Software comes in packages

On Ubuntu and Debian you almost never download programs from websites. Software comes as packages (.deb files) from repositories: servers the distribution maintains, full of tested, signed packages.

text
 repository  ──apt update──▶  package index   ──apt install──▶  files on disk
 (thousands of .debs)         (what exists,                     (+ dependencies,
                               which version)                     recorded by dpkg)

Two tools work together:

ToolJob
aptthe high-level tool: finds packages in repositories, works out dependencies, downloads, installs, upgrades
dpkgthe low-level tool underneath: installs one .deb and keeps the database of what's installed and which files belong to which package

This lab machine has no internet, so it uses a small offline repository at /opt/shack/apt-repo. Every command works exactly as it would against Ubuntu's real mirrors.

The steps
1
Update and searchRefresh the package list and find out which nginx version this machine can install.
2
Install packagesInstall two command-line tools and run them.
3
Inspect what got installedInstall nginx and find out which package owns its main program.
4
Remove and clean upCompletely remove the old monitoring agent, including its config files.
How this lab works
  1. Press Start lab. You get your own Ubuntu machine on the right, just for you.
  2. Read each step. Commands with a ▶ Run button can be sent to the terminal in one click, but typing them yourself helps you remember.
  3. Do the Your task part, then press Check my work. The check looks at the machine and tells you what is right or still missing.
  4. Stuck? Open the hint. Nothing you do here can break anything real.
Step 1 of 4

Update and search

Objective

Refresh the package list and find out which nginx version this machine can install.

apt doesn't search the internet when you install something. It looks in a local package index: the list of every package and version available in your repositories. apt update downloads a fresh copy of that index. It installs nothing. On a new server, and before every install session, run apt update first.

1. Before updating

bash
apt-cache policy nginx
text
(no output)

The index is empty, so apt doesn't know nginx exists. That's exactly what a fresh cloud image or Docker container looks like, and why Dockerfiles always start with apt-get update.

2. Refresh the index

bash
apt update
text
Get:3 file:/opt/shack/apt-repo ./ Packages [5364 B]
Reading package lists...
Building dependency tree...
Reading state information...
All packages are up to date.

(Shortened.) Get: lines are index files being fetched, one set per repository. On a real server you'd see Ubuntu's mirrors here, such as http://archive.ubuntu.com/ubuntu noble InRelease. The last line also tells you whether updates are waiting for already-installed packages.

3. Search, show and check versions

bash
apt search nginx
apt show nginx
apt-cache policy nginx
CommandAnswers
apt search WORDwhich packages mention this word?
apt show PKGwhat is it, what does it depend on, how big is it?
apt-cache policy PKGis it installed, which version would be installed, and from which repository?
text
nginx/unknown 1.24.0-2ubuntu7.21 amd64
  small, powerful, scalable web/proxy server

nginx-common/unknown 1.24.0-2ubuntu7.21 all
  small, powerful, scalable web/proxy server - common files

Package: nginx
Version: 1.24.0-2ubuntu7.21
Section: httpd
Installed-Size: 1356 kB
Depends: libc6 (>= 2.34), libcrypt1 (>= 1:4.1.0), libpcre2-8-0 (>= 10.22), libssl3t64 (>= 3.0.0), zlib1g (>= 1:1.1.4), iproute2, nginx-common (= 1.24.0-2ubuntu7.21)

nginx:
  Installed: (none)
  Candidate: 1.24.0-2ubuntu7.21
  Version table:
     1.24.0-2ubuntu7.21 500
        500 file:/opt/shack/apt-repo ./ Packages

(Parts of each.) In apt-cache policy:

LineMeaning
Installed: (none)not installed yet
Candidate:the version apt install would install
Version tableevery version available, with its source and priority (500 = normal)

The version 1.24.0-2ubuntu7.21 reads as: upstream nginx 1.24.0, Debian packaging revision 2, Ubuntu's 7.21th update of it. The Ubuntu part changes with security fixes.

Refresh the package index, then save the candidate version of nginx (just the version string) to /root/nginx-version.txt.

Expected result: the file contains the same version apt-cache policy nginx shows as Candidate.

Hint: the commands, explained
text
apt update
apt-cache policy nginx
echo 1.24.0-2ubuntu7.21 > /root/nginx-version.txt

What each part does

  • apt update: download the latest package lists from the repositories (it installs nothing).
  • apt-cache policy nginx: show the Installed version (none yet) and the Candidate, the version apt would install.
  • Copy the Candidate version exactly from the output into the file. The one shown here is an example; yours may differ.
Step 2 of 4

Install packages

Objective

Install two command-line tools and run them.

apt install works out everything a package needs (its dependencies), downloads them all, and installs them in the right order. You ask for two packages; apt may install five.

1. Install

bash
apt install -y figlet cowsay

-y answers "yes" to the confirmation prompt (essential in scripts). Without it, apt shows the plan and asks first, which is worth reading on production servers.

text
The following additional packages will be installed:
  libtext-charwidth-perl
Suggested packages:
  filters cowsay-off
The following NEW packages will be installed:
  cowsay figlet libtext-charwidth-perl
0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded.
Need to get 0 B/161 kB of archives.
After this operation, 887 kB of additional disk space will be used.
Get:1 file:/opt/shack/apt-repo ./ libtext-charwidth-perl 0.04-11build3 [9358 B]
Get:2 file:/opt/shack/apt-repo ./ cowsay 3.03+dfsg2-8 [18.6 kB]
Get:3 file:/opt/shack/apt-repo ./ figlet 2.2.5-3 [133 kB]
Unpacking libtext-charwidth-perl:amd64 (0.04-11build3) ...
Unpacking cowsay (3.03+dfsg2-8) ...
Unpacking figlet (2.2.5-3) ...
Setting up libtext-charwidth-perl:amd64 (0.04-11build3) ...
Setting up figlet (2.2.5-3) ...
Setting up cowsay (3.03+dfsg2-8) ...

(Shortened.) Read the plan:

SectionMeaning
additional packagesdependencies pulled in automatically (cowsay is written in Perl and needs this library)
Suggested packagesoptional extras, not installed
NEW packageseverything that will be installed
Unpacking / Setting updpkg extracting files, then running each package's setup scripts

2. Use them

bash
figlet DevOps
/usr/games/cowsay "Hello from apt"
text
 ____              ___            
|  _ \  _____   __/ _ \ _ __  ___ 
| | | |/ _ \ \ / / | | | '_ \/ __|
| |_| |  __/\ V /| |_| | |_) \__ \
|____/ \___| \_/  \___/| .__/|___/
                       |_|        
 ________________
< Hello from apt >
 ----------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

cowsay installs into /usr/games, which isn't in root's PATH here, so it needs its full path. dpkg -L cowsay would have told you where it went (next step).

Install figlet and cowsay.

Expected result: both packages show as installed, and figlet ok prints a banner.

Hint: the commands, explained
text
apt install -y figlet cowsay

What each part does

  • apt install -y figlet cowsay: install both packages, plus anything they depend on. -y answers yes to the confirmation.
  • You'll see Setting up figlet ... lines. Try figlet hi afterwards.
Step 3 of 4

Inspect what got installed

Objective

Install nginx and find out which package owns its main program.

dpkg keeps a database of every installed package and every file it owns. That answers two questions you'll ask constantly: "what did this package put on my server?" and "which package does this file belong to?"

1. Install nginx

bash
apt install -y nginx
text
The following additional packages will be installed:
  nginx-common
The following NEW packages will be installed:
  nginx nginx-common
Setting up nginx-common (1.24.0-2ubuntu7.21) ...
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
Setting up nginx (1.24.0-2ubuntu7.21) ...
invoke-rc.d: could not determine current runlevel
invoke-rc.d: WARNING: No init system and policy-rc.d missing! Defaulting to block.

(Shortened.) The package also enabled a systemd service: on a real server nginx would be running right after this. The invoke-rc.d warnings appear because this lab machine has no systemd, so the service can't be started; that's expected here.

2. Files in a package, and the package of a file

bash
dpkg -L figlet | head -12
dpkg -S /usr/sbin/nginx
dpkg -S /usr/bin/ls
CommandAnswers
dpkg -L PKGwhich files did this package install?
dpkg -S PATHwhich package owns this file?
text
/.
/etc
/etc/emacs
/etc/emacs/site-start.d
/etc/emacs/site-start.d/50figlet.el
/usr
/usr/bin
/usr/bin/chkfont
/usr/bin/figlet-figlet
/usr/bin/figlist
/usr/bin/showfigfonts
/usr/share
nginx: /usr/sbin/nginx
coreutils: /usr/bin/ls

Even ls comes from a package (coreutils). If dpkg -S says "no path found", the file wasn't installed by any package: someone created it by hand or it was generated at runtime.

3. What's installed?

bash
dpkg -l nginx figlet
apt list --installed | grep nginx
text
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  figlet         2.2.5-3            amd64        Make large character ASCII banners out of ordinary text
ii  nginx          1.24.0-2ubuntu7.21 amd64        small, powerful, scalable web/proxy server

The first two letters are the state: ii = desired install, actually installed. You'll meet rc in the next step.

  1. Install nginx.
  2. Find out which package owns /usr/sbin/nginx and save the package name to /root/owner.txt.

Expected result: nginx is installed and /root/owner.txt names the package that owns /usr/sbin/nginx.

Hint: the commands, explained
text
apt install -y nginx
dpkg -S /usr/sbin/nginx | cut -d: -f1 > /root/owner.txt

What each part does

  • apt install -y nginx: install the nginx web server package.
  • dpkg -S /usr/sbin/nginx: Search which installed package owns this file. It prints package: /path.
  • cut -d: -f1: split at the colon and keep field 1, the package name. > saves it.
Step 4 of 4

Remove and clean up

Objective

Completely remove the old monitoring agent, including its config files.

There are two ways to uninstall. apt remove deletes the program but keeps its configuration files, in case you reinstall. apt purge deletes the configuration too. On servers, leftover configs of software you no longer use are clutter at best and a security risk at worst (old credentials, old endpoints).

1. The old agent

bash
dpkg -l legacy-agent | tail -1
dpkg -L legacy-agent
text
ii  legacy-agent   2.3.1        all          Old metrics agent (replaced by node-exporter)
/.
/etc
/etc/legacy-agent
/etc/legacy-agent/agent.conf
/usr
/usr/bin
/usr/bin/legacy-agent

A program and a config file in /etc.

2. remove: the config stays

bash
apt remove -y legacy-agent
dpkg -l legacy-agent | tail -1
ls /etc/legacy-agent
text
The following packages will be REMOVED:
  legacy-agent
Removing legacy-agent (2.3.1) ...
rc  legacy-agent   2.3.1        all          Old metrics agent (replaced by node-exporter)
agent.conf

State rc: desired removed, only config files remain. The program is gone; agent.conf is still there.

3. purge: everything goes

bash
apt purge -y legacy-agent
dpkg -l legacy-agent
ls /etc/legacy-agent
text
The following packages will be REMOVED:
  legacy-agent*
Purging configuration files for legacy-agent (2.3.1) ...
dpkg-query: no packages found matching legacy-agent
ls: cannot access '/etc/legacy-agent': No such file or directory

The * after the name in the plan marks a purge. Now dpkg has no record of it at all.

4. Clean up leftovers

bash
apt autoremove -y
dpkg -l | grep '^rc'

autoremove removes dependencies that were installed automatically and are no longer needed by anything. grep '^rc' lists every package that left config behind (apt purge $(dpkg -l | awk '/^rc/ {print $2}') cleans them all).

Remove legacy-agent completely, including its configuration files. Leave nginx installed.

Expected result: dpkg -l legacy-agent finds nothing, and /etc/legacy-agent/agent.conf no longer exists.

Hint: the commands, explained
text
apt purge -y legacy-agent

(If you already ran apt remove, apt purge still removes the leftover config.)

What each part does

  • apt purge: remove the package and its configuration files (plain apt remove keeps the config in /etc).
  • Afterwards dpkg -l legacy-agent shows nothing or un (not installed), and its /etc files are gone.
Lab complete

Brilliant work!

You managed software on this server exactly as on any Ubuntu or Debian production machine: refreshed the index, checked versions, installed packages and their dependencies, traced files to packages, and removed an old agent without leaving anything behind.

Takeaways:

  • apt update refreshes the package index (installs nothing). Always first.
  • apt search, apt show, apt-cache policy (Installed vs Candidate) before installing.
  • apt install -y PKG resolves dependencies; read the plan on production.
  • dpkg -L PKG lists a package's files; dpkg -S FILE finds a file's package; dpkg -l shows states (ii, rc).
  • apt remove keeps config (rc); apt purge removes it; apt autoremove clears unused dependencies.

Next: the dpkg, Packages and Repositories lesson.

root@devopsshack offline

Your Linux machine is ready to launch

Starting gives you a private Ubuntu server for up to 45 minutes. It is deleted when you finish.

Feedback