Package Management with apt
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.
- 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.
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:
| Tool | Job |
|---|---|
| apt | the high-level tool: finds packages in repositories, works out dependencies, downloads, installs, upgrades |
| dpkg | the 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.
- Press Start lab. You get your own Ubuntu machine on the right, just for you.
- Read each step. Commands with a ▶ Run button can be sent to the terminal in one click, but typing them yourself helps you remember.
- 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.
- Stuck? Open the hint. Nothing you do here can break anything real.
Update and search
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
apt-cache policy nginx(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
apt updateGet: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
apt search nginx
apt show nginx
apt-cache policy nginx| Command | Answers |
|---|---|
apt search WORD | which packages mention this word? |
apt show PKG | what is it, what does it depend on, how big is it? |
apt-cache policy PKG | is it installed, which version would be installed, and from which repository? |
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:
| Line | Meaning |
|---|---|
Installed: (none) | not installed yet |
Candidate: | the version apt install would install |
Version table | every 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
apt update
apt-cache policy nginx
echo 1.24.0-2ubuntu7.21 > /root/nginx-version.txtWhat 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.
Install packages
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
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.
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:
| Section | Meaning |
|---|---|
additional packages | dependencies pulled in automatically (cowsay is written in Perl and needs this library) |
Suggested packages | optional extras, not installed |
NEW packages | everything that will be installed |
Unpacking / Setting up | dpkg extracting files, then running each package's setup scripts |
2. Use them
figlet DevOps
/usr/games/cowsay "Hello from apt" ____ ___
| _ \ _____ __/ _ \ _ __ ___
| | | |/ _ \ \ / / | | | '_ \/ __|
| |_| | __/\ 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
apt install -y figlet cowsayWhat each part does
apt install -y figlet cowsay: install both packages, plus anything they depend on.-yanswers yes to the confirmation.- You'll see
Setting up figlet ...lines. Tryfiglet hiafterwards.
Inspect what got installed
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
apt install -y nginxThe 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
dpkg -L figlet | head -12
dpkg -S /usr/sbin/nginx
dpkg -S /usr/bin/ls| Command | Answers |
|---|---|
dpkg -L PKG | which files did this package install? |
dpkg -S PATH | which package owns this file? |
/.
/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/lsEven 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?
dpkg -l nginx figlet
apt list --installed | grep nginxDesired=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 serverThe first two letters are the state: ii = desired install, actually installed. You'll meet rc in the next step.
- Install nginx.
- Find out which package owns
/usr/sbin/nginxand 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
apt install -y nginx
dpkg -S /usr/sbin/nginx | cut -d: -f1 > /root/owner.txtWhat 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 printspackage: /path.cut -d: -f1: split at the colon and keep field 1, the package name.>saves it.
Remove and clean up
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
dpkg -l legacy-agent | tail -1
dpkg -L legacy-agentii 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-agentA program and a config file in /etc.
2. remove: the config stays
apt remove -y legacy-agent
dpkg -l legacy-agent | tail -1
ls /etc/legacy-agentThe 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.confState rc: desired removed, only config files remain. The program is gone; agent.conf is still there.
3. purge: everything goes
apt purge -y legacy-agent
dpkg -l legacy-agent
ls /etc/legacy-agentThe 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 directoryThe * after the name in the plan marks a purge. Now dpkg has no record of it at all.
4. Clean up leftovers
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
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 (plainapt removekeeps the config in/etc).- Afterwards
dpkg -l legacy-agentshows nothing orun(not installed), and its/etcfiles are gone.
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 updaterefreshes the package index (installs nothing). Always first.apt search,apt show,apt-cache policy(Installed vs Candidate) before installing.apt install -y PKGresolves dependencies; read the plan on production.dpkg -L PKGlists a package's files;dpkg -S FILEfinds a file's package;dpkg -lshows states (ii,rc).apt removekeeps config (rc);apt purgeremoves it;apt autoremoveclears unused dependencies.
Next: the dpkg, Packages and Repositories lesson.
Install, inspect, update and remove software the way every Ubuntu and Debian server is managed, using a real apt repository built into the lab machine.
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.