Linux7 min readPublished: 30 May 2024Updated: 9 Nov 2026

Linux Package Managers: apt and dnf for Beginners

A beginner's guide to apt and dnf: installing, searching, removing and holding packages, what a repository is, and the equivalent command on each distribution.

G7Cloud Engineering
Platform team
Share:
Abstract illustration representing linux package managers: apt and dnf for beginners.
  • Debian and Ubuntu use apt. AlmaLinux, Rocky, RHEL and Fedora use dnf, which replaced yum but still answers to that name.
  • apt update refreshes the package lists and installs nothing. apt upgrade is the one that changes your system.
  • dnf has no separate update step: it refreshes its package lists before each command.
  • When a package exists, never install by piping a script from the internet into a shell. The package manager can update and remove a package; it cannot touch the script's leftovers.
  • Adding a third-party repository means trusting whoever runs it with root on your server. Add few, and know why each one is there.

What is a package manager and why does it matter?

A package manager installs software and everything it needs, records what it put where, and can update or remove it cleanly later. Debian and Ubuntu use apt; AlmaLinux, Rocky and RHEL use dnf. Software installed by hand or by a script from a website sits outside that record, so nothing patches or removes it.

DistributionPackage managerPackage format
Ubuntu, Debianapt.deb
AlmaLinux, Rocky, RHELdnf (formerly yum).rpm
Fedoradnf.rpm
Alpineapk.apk
openSUSEzypper.rpm

On a web server you will meet apt and dnf. On RHEL-family systems, yum still works as another name for dnf.

curl | bash is not an install method

Piping a script straight into a shell runs code you have not read, as root (the admin account that can change anything), with no record kept. Your patching routine cannot update it and you cannot cleanly remove it. If a package exists, use it. If only a script exists, download it, read it, then run it.

How do I use apt on Debian and Ubuntu?

apt splits refreshing the catalogue from changing the system. apt update downloads the latest package lists and installs nothing. apt upgrade acts on what it found. Most apt problems beginners hit come from an out-of-date catalogue, which is why people type the two together.

The apt commands you need
# Refresh the catalogue. Installs nothing.
sudo apt update

# Install
sudo apt install nginx
sudo apt install -y nginx php8.3-fpm mariadb-server   # -y skips the prompt

# Search and inspect
apt search redis
apt show nginx
apt list --installed | grep php

# Remove
sudo apt remove nginx            # binaries go, config stays
sudo apt purge nginx             # config goes too
sudo apt autoremove              # tidy up orphaned dependencies

# What is upgradable?
apt list --upgradable

# Which package provides a file I have?
dpkg -S /usr/sbin/nginx

# Which package would provide a command I do not have?
sudo apt install -y apt-file && sudo apt-file update
apt-file search bin/htop
Use apt when you type commands yourself. Scripts should use apt-get, whose output stays the same across releases.

remove keeps your configuration, purge does not

apt remove nginx leaves /etc/nginx in place, so reinstalling later brings your setup back. apt purge deletes it. Use remove unless you want a clean slate, and copy the config directory first either way.

How do I use dnf on AlmaLinux and Rocky?

dnf refreshes its package lists by itself, so there is no separate update step. It also keeps a history of every change it makes, which you can inspect and undo. That history is its biggest practical advantage over apt.

The dnf commands you need
# Install
sudo dnf install nginx
sudo dnf install -y nginx php-fpm mariadb-server

# Search and inspect
dnf search redis
dnf info nginx
dnf list installed | grep php

# Remove
sudo dnf remove nginx
sudo dnf autoremove

# Updates
sudo dnf check-update
sudo dnf update
sudo dnf update --security        # security patches only

# Which package owns a file?
dnf provides /usr/sbin/nginx
dnf provides '*/bin/htop'         # works for files you do not have yet

# Transaction history, and undo
sudo dnf history
sudo dnf history info 42
sudo dnf history undo 42
dnf provides also finds the package for a file you do not have yet. On apt you need the extra apt-file tool shown above.
Taskaptdnf
Refresh catalogueapt updateautomatic
Installapt install pkgdnf install pkg
Remove, keep configapt remove pkgdnf remove pkg
Remove config tooapt purge pkgno direct equivalent
Upgrade everythingapt upgradednf update
Security onlyunattended-upgradednf update --security
Which package owns a filedpkg -S /pathdnf provides /path
List installedapt list --installeddnf list installed
Undo a transactionno equivalentdnf history undo <id>
Prevent upgradesapt-mark hold pkgdnf versionlock add pkg

Keep this table to hand if you work on both. People look up the last two rows most often.

What is a repository and when should I add one?

A repository is a server that holds packages and their signatures, and your package manager trusts it. Adding one is how you get software your distribution does not ship, such as a newer PHP for a WordPress version upgrade. But whoever runs that repository can then install software as root on your machine, on every future update. Treat it as a trust decision.

Adding a repository properly
# Debian / Ubuntu: modern, key stored separately from the system keyring
curl -fsSL https://packages.example.com/gpg \
  | sudo gpg --dearmor -o /usr/share/keyrings/example-archive-keyring.gpg

echo 'deb [signed-by=/usr/share/keyrings/example-archive-keyring.gpg] \
  https://packages.example.com/apt stable main' \
  | sudo tee /etc/apt/sources.list.d/example.list

sudo apt update

# RHEL family
sudo dnf config-manager --add-repo https://packages.example.com/example.repo
dnf repolist

# EPEL, which many common tools live in
sudo dnf install -y epel-release
Never use the old apt-key add. It trusts the key for every repository on the system, not only the one you are adding.

Two repositories offering the same package is a problem

When a third-party repository ships a newer PHP or MySQL than your distribution, upgrades can flip between the two versions and break things in ways that are hard to trace. Set which one wins (its priority), or stick with the distribution's version. This is the usual cause of a puzzling dependency failure.

Holding a package at a known-good version
# Debian / Ubuntu
sudo apt-mark hold nginx
apt-mark showhold
sudo apt-mark unhold nginx

# RHEL family
sudo dnf install -y python3-dnf-plugin-versionlock
sudo dnf versionlock add nginx
sudo dnf versionlock list
sudo dnf versionlock delete nginx
Hold a package once you have found a version that works, and write down why. A forgotten hold means missed security patches.

How do I fix common package manager errors?

ErrorCauseFix
Unable to locate packageOut-of-date catalogue, or wrong repositorysudo apt update, then check the name with apt search
Could not get lock /var/lib/dpkg/lockAnother apt process is runningWait for it; check with ps aux | grep -i apt
dpkg was interruptedA previous install did not finishsudo dpkg --configure -a
Held broken packagesConflicting dependency requirementssudo apt install -f, then read what it proposes
No space left on device/boot or / is fullClear old kernels with apt autoremove --purge
NO_PUBKEY / signature errorMissing or expired repository keyRe-import the key with the signed-by method above

Rows two and three cover most of what people hit. If the cause is a full disk, checking disk usage finds it in one command.

Recovering a stuck package system
# Something else holding the lock?
ps aux | grep -iE '[a]pt|[d]pkg|[u]nattended'

# Finish an interrupted installation
sudo dpkg --configure -a

# Repair broken dependencies
sudo apt install -f

# Only if you are certain nothing is running:
sudo rm /var/lib/dpkg/lock-frontend /var/lib/dpkg/lock
sudo dpkg --configure -a
Removing the lock files while apt is running will corrupt the package database. Check with ps first, every time.

unattended-upgrades is usually what holds the lock

On Ubuntu, automatic security updates run shortly after boot and hold the apt lock while they work. Waiting two minutes is nearly always the right answer, and far better than forcing the lock open.

Once you are comfortable installing and removing packages, the next step is patching a live server without breaking it. How to safely update and patch a Linux server covers the order to do it in. If you would rather not look after the operating system at all, our managed WordPress hosting keeps it patched for you.

Frequently asked questions

What is the difference between apt and apt-get?

apt is the newer front end for typing commands yourself, with a progress bar and friendlier output. apt-get is the older one, with output that does not change between releases. Use apt by hand and apt-get in scripts, where a change in output would break your automation.

Is yum the same as dnf?

dnf replaced yum from RHEL 8 onward, with better dependency handling and a history you can undo. yum still exists on those systems as another name for dnf, so old commands and guides keep working. Use dnf for new work.

Do I need to run apt update before every install?

Not every time, but before the first install of a session and after adding a repository. Without it, apt works from an out-of-date catalogue. It may report "unable to locate package" for software that exists, or try to fetch a version that has been replaced.

What is the difference between apt remove and apt purge?

remove deletes the program but leaves its configuration files in /etc, so reinstalling brings your setup back. purge deletes the configuration too. Use remove unless you want to start clean, and copy the config directory either way.

How do I stop a package being upgraded?

Run sudo apt-mark hold packagename on Debian and Ubuntu, or sudo dnf versionlock add packagename on the RHEL family. Write down why: a forgotten hold quietly blocks security patches for that package.

Is it safe to add third-party repositories?

It is a trust decision. Whoever runs the repository can install software as root on your server on every future update. Add only ones you would trust with that, prefer official vendor repositories, and never use the old apt-key add, which trusts the key for every repository.

Put this into practice on G7Cloud

Every site runs in its own dedicated container behind ScaleShield, with daily backups that are restore-tested every night. Start on the free plan, no card needed.

About G7Cloud Engineering

Platform team

Articles written by the engineers who build and run G7Cloud: UK managed hosting and the AI Website Builder. We write about what we operate every day: containers, backups, databases, and the small-business websites that run on them.

More about G7Cloud →