Chapter 7 · Lesson 10 of 11
Linux for servers

Package management with apt on Ubuntu and Debian (and dnf)

apt installs and updates software on Debian and Ubuntu from signed repositories; apt update refreshes the package lists and apt upgrade installs the newer versions.

By the end you will

Install, upgrade and remove packages with apt, hold a version, add a third-party repository with a keyring, and find the dnf equivalents on Fedora and RHEL.

Time
25 min
Level
Intermediate
Check
4 questions
You need
  • An Ubuntu or Debian server you can reach over SSH
  • A user with sudo access

Almost everything you install on an Ubuntu or Debian server comes through apt. It resolves dependencies, verifies signatures, and keeps a record of what is installed, so you can undo a change cleanly. Most mistakes come from skipping apt update, using purge when you meant remove, or adding a repository with an unsafe key. This lesson goes further than keep a server updated.

How it works#

A repository is a web server holding packages and an index. The list of repositories lives in /etc/apt/sources.list and in files under /etc/apt/sources.list.d/ (.list files, or .sources files on newer releases). apt update downloads the index from each one, so apt knows which versions exist. apt install and apt upgrade then read that local index. The packages are installed with dpkg, which apt wraps.

Index and package files are signed. apt checks the signature against keys it trusts, so a tampered download is refused. Installs write into the directories explained in Linux filesystem layout, such as /usr/bin for programs and /etc for config, and need root, so you use sudo.

The root user can install packages, and so can sudo users.

Use apt for interactive work and apt-get in scripts, because apt output can change between versions.

Step 1: Update, upgrade and install#

bash
sudo apt update
apt list --upgradable
sudo apt upgrade
sudo apt install htop curl
sudo apt install -y nginx

Run sudo apt update first, every time. It prints Reading package lists... Done and how many packages can be upgraded. apt list --upgradable shows what would change. apt upgrade installs newer versions and never removes packages. If something is kept back, it needs a new dependency or a removal. apt full-upgrade (the old dist-upgrade) allows that, so read the list of packages it will remove before you answer Y. -y answers yes automatically, which is fine in a provisioning script, but avoid it for a first look at a big upgrade.

A kernel update takes effect after a reboot. When /var/run/reboot-required exists, plan one. To keep patches coming automatically, install unattended-upgrades, described in keep a server updated.

Step 2: Search and inspect#

bash
apt search "web server"
apt show nginx
apt policy nginx
apt list --installed | grep nginx
dpkg -L nginx | head
dpkg -S /usr/sbin/nginx

apt show prints version, size, dependencies and description. apt policy shows the installed version, the candidate, and which repository each version comes from, which helps when a package is older than you expected. dpkg -L pkg lists the files a package installed, and dpkg -S path tells you which package owns a file.

Step 3: Remove, purge and clean up#

bash
sudo apt remove nginx
sudo apt purge nginx
sudo apt autoremove
sudo apt clean
  • remove deletes the program and leaves config files in /etc.
  • purge deletes the program and its system config. It does not delete your data in /var/lib or home folders, and some packages, such as database servers, ask before touching data.
  • autoremove deletes dependencies no longer needed. Read its list before you confirm, because it can include things you rely on without having installed them explicitly.
  • clean empties the download cache in /var/cache/apt/archives, which frees space.

Before a purge of a service with data, take a backup, see backups and the 3-2-1 rule.

Step 4: Hold a package#

bash
sudo apt-mark hold nginx
apt-mark showhold
sudo apt-mark unhold nginx

A held package is not upgraded or removed by apt until you unhold it. Use it when a new version breaks your app and you need time to test. Remember to unhold later, or you will miss security fixes. Pin a specific version at install time with sudo apt install nginx=1.24.0-2ubuntu7 using a version from apt policy output.

Step 5: Add a third-party repository safely#

Distribution repositories can be old. Vendors such as Docker publish their own. The current safe pattern stores the key in a dedicated file and ties it to one repository with signed-by. Avoid the deprecated apt-key add, which trusts a key for every repository.

bash
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://example.com/repo/key.gpg -o /etc/apt/keyrings/example.asc
sudo chmod a+r /etc/apt/keyrings/example.asc
echo "deb [signed-by=/etc/apt/keyrings/example.asc] https://example.com/repo stable main" | sudo tee /etc/apt/sources.list.d/example.list
sudo apt update

Replace example.com with the vendor’s real URL from their official install page, and verify the key fingerprint they publish before you trust it. curl -fsSL fails on HTTP errors, hides progress, and follows redirects. Writing the file uses sudo tee, as explained in pipes, redirects and xargs. The Docker lesson install Docker on Ubuntu follows this pattern with the real addresses.

Step 6: dnf on Fedora, RHEL, Rocky and Alma#

The Red Hat family uses dnf and .rpm packages. The ideas match:

Taskaptdnf
Refresh metadataapt updatednf makecache (done automatically)
Upgrade allapt upgradednf upgrade
Installapt install pkgdnf install pkg
Removeapt remove pkgdnf remove pkg
Searchapt search worddnf search word
Show infoapt show pkgdnf info pkg
List installedapt list --installeddnf list --installed
Clean upapt autoremovednf autoremove
Which package owns a filedpkg -S pathrpm -qf path

dnf remove does not keep config separately in the way purge does, and repositories are .repo files in /etc/yum.repos.d/.

Check it worked#

bash
apt policy htop
dpkg -l htop
which htop
sudo apt update 2>&1 | tail -n 3

apt policy should show a non-empty Installed: line. After adding a repository, apt update should list it with Get: or Hit: and no NO_PUBKEY or GPG error line.

Edge cases and gotchas#

  • Locks. Only one apt process runs at a time. Unattended upgrades or another session may hold the lock. Wait, do not delete lock files.
  • Interrupted dpkg. After a crash, run sudo dpkg --configure -a and sudo apt -f install to finish the job.
  • Config prompts. An upgrade may ask whether to keep your edited config. Keep yours unless you know the new default, and diff later.
  • Mixing releases is risky. Adding a repository for a different Ubuntu release can pull in incompatible libraries.
  • Snap packages are separate. Some packages, such as lxd, install as snaps and are not listed by apt. Use snap list.
  • Old distribution versions. The apt version of a tool can lag behind. Check apt policy before assuming a feature exists.
  • Piping a script into bash (curl ... | sudo bash) skips signature checks. Download, read, then run.

Troubleshooting#

SymptomLikely causeFix
E: Unable to locate package htopIndex is stale or the repository is missingsudo apt update, then check universe is enabled
E: Could not get lock /var/lib/dpkg/lock-frontend. It is held by process 1234Another apt or unattended-upgrades is runningWait, check ps -p 1234, and retry
W: GPG error ... NO_PUBKEY ABCDEF1234567890The repository key is missingInstall the vendor’s key into /etc/apt/keyrings and reference it with signed-by
The following packages have been kept backUpgrade needs new dependenciesUse sudo apt full-upgrade after reading the plan
E: Unmet dependencies. Try 'apt --fix-broken install'Half-finished or conflicting installsudo apt --fix-broken install
dpkg: error: dpkg frontend lock is locked by another processParallel runWait for the other process to finish
E: Sub-process /usr/bin/dpkg returned an error code (1)A package script failedRead the lines above the error and fix that package
404 Not Found [IP: ...] on updateRepository no longer has that releaseFix the URL or remove the stale .list file

Quick reference#

bash
sudo apt update
sudo apt upgrade
sudo apt full-upgrade
sudo apt install pkg
sudo apt remove pkg      # keep config
sudo apt purge pkg       # remove config too
sudo apt autoremove
apt search word
apt show pkg
apt policy pkg
apt list --installed
sudo apt-mark hold pkg
# dnf: install, remove, upgrade, search, info

For a deploy that depends on a package install, see Deploy a Go service with Docker.

Next#

Installed packages take disk space. See how to measure it in disk usage and mounts.

Check what you learned4 questions
1What does sudo apt update do?
2Which command removes a package and its system configuration files?
3Why does a modern third-party repository use a keyring file with signed-by?
4What is the difference between apt upgrade and apt full-upgrade?

Frequently asked questions

What is the difference between apt update and apt upgrade?

apt update downloads the latest package lists from the repositories and installs nothing. apt upgrade installs newer versions of packages you already have, so run update first.

What is the difference between apt remove and apt purge?

remove deletes the program but keeps its config files in /etc. purge deletes the program and its system config files, but not user data under home folders or /var/lib.

How do I stop a package from being upgraded?

Run sudo apt-mark hold package. It stays at its current version until you run sudo apt-mark unhold package. List holds with apt-mark showhold.

What is the dnf equivalent of apt install?

On Fedora, RHEL, Rocky and Alma, use sudo dnf install package. The rough pairs are apt remove and dnf remove, apt search and dnf search, apt upgrade and dnf upgrade.

Sources

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios