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.
- Updated Nov 27, 2026
- Markdown
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.
- 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#
sudo apt update
apt list --upgradable
sudo apt upgrade
sudo apt install htop curl
sudo apt install -y nginxRun 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#
apt search "web server"
apt show nginx
apt policy nginx
apt list --installed | grep nginx
dpkg -L nginx | head
dpkg -S /usr/sbin/nginxapt 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#
sudo apt remove nginx
sudo apt purge nginx
sudo apt autoremove
sudo apt cleanremovedeletes the program and leaves config files in/etc.purgedeletes the program and its system config. It does not delete your data in/var/libor home folders, and some packages, such as database servers, ask before touching data.autoremovedeletes dependencies no longer needed. Read its list before you confirm, because it can include things you rely on without having installed them explicitly.cleanempties 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#
sudo apt-mark hold nginx
apt-mark showhold
sudo apt-mark unhold nginxA 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.
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 updateReplace 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:
| Task | apt | dnf |
|---|---|---|
| Refresh metadata | apt update | dnf makecache (done automatically) |
| Upgrade all | apt upgrade | dnf upgrade |
| Install | apt install pkg | dnf install pkg |
| Remove | apt remove pkg | dnf remove pkg |
| Search | apt search word | dnf search word |
| Show info | apt show pkg | dnf info pkg |
| List installed | apt list --installed | dnf list --installed |
| Clean up | apt autoremove | dnf autoremove |
| Which package owns a file | dpkg -S path | rpm -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#
apt policy htop
dpkg -l htop
which htop
sudo apt update 2>&1 | tail -n 3apt 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 -aandsudo apt -f installto 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 byapt. Usesnap list. - Old distribution versions. The apt version of a tool can lag behind. Check
apt policybefore assuming a feature exists. - Piping a script into bash (
curl ... | sudo bash) skips signature checks. Download, read, then run.
Troubleshooting#
| Symptom | Likely cause | Fix |
|---|---|---|
E: Unable to locate package htop | Index is stale or the repository is missing | sudo apt update, then check universe is enabled |
E: Could not get lock /var/lib/dpkg/lock-frontend. It is held by process 1234 | Another apt or unattended-upgrades is running | Wait, check ps -p 1234, and retry |
W: GPG error ... NO_PUBKEY ABCDEF1234567890 | The repository key is missing | Install the vendor’s key into /etc/apt/keyrings and reference it with signed-by |
The following packages have been kept back | Upgrade needs new dependencies | Use sudo apt full-upgrade after reading the plan |
E: Unmet dependencies. Try 'apt --fix-broken install' | Half-finished or conflicting install | sudo apt --fix-broken install |
dpkg: error: dpkg frontend lock is locked by another process | Parallel run | Wait for the other process to finish |
E: Sub-process /usr/bin/dpkg returned an error code (1) | A package script failed | Read the lines above the error and fix that package |
404 Not Found [IP: ...] on update | Repository no longer has that release | Fix the URL or remove the stale .list file |
Quick reference#
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, infoRelated lessons#
- Keep a server updated: security updates and unattended upgrades.
- Install Docker on Ubuntu: a real third-party repository with a keyring.
- Create a sudo user: the account you run apt from.
- Initial server setup checklist: where updates fit on a new server.
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 learned
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
Keep going: guides and tutorials
- Install a Self-Hosted PaaS on Your Own Server in Five Minutes One installer download, one setup token, one sample app. The full Levelrail install on a fresh Linux server, with the checks and the fixes.
- Manage Secrets Self-Hosted: Env Vars and Master Key Rotation Set env vars and encrypted secrets on a self-hosted server, apply changes safely, and rotate the master key with no downtime without locking yourself out.