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

URL: https://levelrail.com/learn/linux/package-management-with-apt/
Updated: 2026-11-27

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](https://levelrail.com/learn/linux/linux-filesystem-layout/)), such as `/usr/bin` for programs and `/etc` for config, and need root, so you use [sudo](https://levelrail.com/learn/glossary/#sudo)).

The [root user](https://levelrail.com/learn/glossary/#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](https://levelrail.com/learn/server-basics/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](https://levelrail.com/learn/deploy-operate/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](https://levelrail.com/learn/linux/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

```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

| 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

```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
```

## Related lessons

- [Keep a server updated](https://levelrail.com/learn/server-basics/keep-a-server-updated/)): security updates and unattended upgrades.
- [Install Docker on Ubuntu](https://levelrail.com/learn/docker/install-docker-on-ubuntu/)): a real third-party repository with a keyring.
- [Create a sudo user](https://levelrail.com/learn/server-basics/create-a-sudo-user/)): the account you run apt from.
- [Initial server setup checklist](https://levelrail.com/learn/server-basics/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](https://levelrail.com/blog/deploy-go-service-docker/)).

## Next

Installed packages take disk space. See how to measure it in [disk usage and mounts](https://levelrail.com/learn/linux/disk-usage-and-mounts/)).

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

- [Ubuntu manpages, apt(8)](https://manpages.ubuntu.com/manpages/noble/en/man8/apt.8.html)
- [Ubuntu manpages, sources.list(5)](https://manpages.ubuntu.com/manpages/noble/en/man5/sources.list.5.html)
