- Debian and Ubuntu use apt. AlmaLinux, Rocky, RHEL and Fedora use dnf, which replaced yum but still answers to that name.
apt updaterefreshes the package lists and installs nothing.apt upgradeis 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.
| Distribution | Package manager | Package format |
|---|---|---|
| Ubuntu, Debian | apt | .deb |
| AlmaLinux, Rocky, RHEL | dnf (formerly yum) | .rpm |
| Fedora | dnf | .rpm |
| Alpine | apk | .apk |
| openSUSE | zypper | .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.
# 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/htopapt 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.
# 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 42dnf provides also finds the package for a file you do not have yet. On apt you need the extra apt-file tool shown above.| Task | apt | dnf |
|---|---|---|
| Refresh catalogue | apt update | automatic |
| Install | apt install pkg | dnf install pkg |
| Remove, keep config | apt remove pkg | dnf remove pkg |
| Remove config too | apt purge pkg | no direct equivalent |
| Upgrade everything | apt upgrade | dnf update |
| Security only | unattended-upgrade | dnf update --security |
| Which package owns a file | dpkg -S /path | dnf provides /path |
| List installed | apt list --installed | dnf list installed |
| Undo a transaction | no equivalent | dnf history undo <id> |
| Prevent upgrades | apt-mark hold pkg | dnf 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.
# 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-releaseapt-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.
# 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 nginxHow do I fix common package manager errors?
| Error | Cause | Fix |
|---|---|---|
| Unable to locate package | Out-of-date catalogue, or wrong repository | sudo apt update, then check the name with apt search |
| Could not get lock /var/lib/dpkg/lock | Another apt process is running | Wait for it; check with ps aux | grep -i apt |
| dpkg was interrupted | A previous install did not finish | sudo dpkg --configure -a |
| Held broken packages | Conflicting dependency requirements | sudo apt install -f, then read what it proposes |
| No space left on device | /boot or / is full | Clear old kernels with apt autoremove --purge |
| NO_PUBKEY / signature error | Missing or expired repository key | Re-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.
# 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 -aunattended-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.
About G7Cloud Engineering
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 →