On this page
- What do apt and dnf actually do?
- How do I update the system?
- How do I search for and inspect packages?
- How do I install and remove software?
- How do I keep a package at its current version?
- How do I add a third party repository safely?
- How do I see what changed and undo it?
- How do I free disk space used by packages?
- How do I fix a broken or locked package manager?
- Questions
What do apt and dnf actually do?
Both are front ends that download packages from signed repositories, resolve dependencies and hand the files to the low level tool: dpkg on Debian based systems and rpm on Red Hat based ones. You almost never need the low level tools to install software, but they are handy for asking questions about what is already on disk.
Every image you deploy comes with its repositories configured, so packages from the official archive work right away. Installing software from the distribution first, and from a vendor repository second, keeps upgrades predictable. Downloading random binaries or piping scripts into a shell should be the last resort. If you are still deciding which family to use, choosing a distribution compares them.
How do I update the system?
On Ubuntu and Debian, update refreshes the package lists and upgrade installs newer versions. full-upgrade may also remove or add packages when dependencies change, which is needed for some kernel updates:
sudo apt update
apt list --upgradable
sudo apt upgrade
sudo apt full-upgradeOn Rocky Linux and AlmaLinux, dnf refreshes metadata automatically:
sudo dnf check-update
sudo dnf upgrade
sudo dnf upgrade --securityThe last line installs only updates flagged as security fixes. After a kernel or core library update, find out whether a reboot is needed: on Ubuntu and Debian look for /var/run/reboot-required, and on Rocky and Alma run sudo dnf needs-restarting -r. To have security fixes applied for you, follow automatic security updates.
How do I search for and inspect packages?
Search by keyword, then read details before installing:
apt search redis
apt show redis-server
apt policy redis-server
dnf search redis
dnf info redis
dnf provides /usr/bin/digapt policy shows the installed version, the candidate version and which repository provides it; this is the quickest way to see whether a third party repository is winning over the official one. dnf provides answers the frequent question of which package contains a missing command. On Debian based systems, apt-file search bin/dig does the same after sudo apt install apt-file and sudo apt-file update.
For packages already installed, list their files or find the owner of a file:
dpkg -L nginx | head
dpkg -S /usr/sbin/nginx
rpm -ql nginx | head
rpm -qf /usr/sbin/nginxHow do I install and remove software?
Install one or several packages by name. Add -y in scripts to skip the confirmation prompt:
sudo apt install nginx htop
sudo dnf install nginx htopRemoving works in two steps on apt. remove keeps configuration files, purge deletes them too, and autoremove clears dependencies nothing needs anymore:
sudo apt remove nginx
sudo apt purge nginx
sudo apt autoremove
sudo dnf remove nginx
sudo dnf autoremovednf removes unused dependencies as part of remove by default, and it keeps changed configuration files with an .rpmsave suffix. To list what is installed, use apt list --installed or dnf list installed, piped through grep when looking for one name.
How do I keep a package at its current version?
Sometimes an upgrade must wait, for instance a database server you want to move by hand, or a kernel module that needs testing. On apt, mark the package as held:
sudo apt-mark hold postgresql-16
apt-mark showhold
sudo apt-mark unhold postgresql-16On Rocky and Alma, install the versionlock plugin and lock the package:
sudo dnf install python3-dnf-plugin-versionlock
sudo dnf versionlock add mariadb-server
dnf versionlock list
sudo dnf versionlock delete mariadb-serverHeld packages also stop receiving security fixes, so write down why each hold exists and remove it as soon as you can. You can also exclude a package permanently with an exclude= line in /etc/dnf/dnf.conf, though versionlock is easier to audit.
How do I add a third party repository safely?
Use the vendor instructions, and make sure the signing key is limited to that one repository. On current Ubuntu and Debian, keys go in /etc/apt/keyrings and the source file names the key with signed-by:
sudo install -d -m 0755 /etc/apt/keyrings
curl -fsSL https://repo.example.net/key.gpg | sudo gpg --dearmor -o /etc/apt/keyrings/example.gpg
echo "deb [signed-by=/etc/apt/keyrings/example.gpg] https://repo.example.net/apt stable main" | sudo tee /etc/apt/sources.list.d/example.list
sudo apt updateUbuntu 24.04 and Debian 13 keep their own sources in the newer deb822 format, in /etc/apt/sources.list.d/ubuntu.sources or debian.sources. Debian 12 still uses /etc/apt/sources.list. Both formats work side by side.
On Rocky and Alma, repositories are .repo files in /etc/yum.repos.d. EPEL and CRB are the usual additions:
sudo dnf install epel-release
sudo dnf config-manager --set-enabled crb
dnf repolistCheck dnf module list when a package offers several streams, for example different PHP or Node.js versions.
How do I see what changed and undo it?
apt writes every action to plain text logs, which you can read with less or search with grep:
less /var/log/apt/history.log
zgrep -h "Install:" /var/log/apt/history.log* | tail
grep " install " /var/log/dpkg.log | taildnf keeps a numbered transaction database with a built in undo:
dnf history
dnf history info 14
sudo dnf history undo 14Undo installs or removes the packages of that transaction again, but it cannot restore older versions that are no longer in the repositories, and it does not touch your data. For real rollback, rely on daily backups.
How do I free disk space used by packages?
Downloaded package files stay in a cache after installation. Clearing it is safe and often frees hundreds of megabytes:
sudo apt autoremove
sudo apt clean
du -sh /var/cache/apt
sudo dnf autoremove
sudo dnf clean all
du -sh /var/cache/dnfOld kernels are another common space user. apt autoremove removes all but the running and the newest kernel, and dnf keeps three by default, controlled by installonly_limit in /etc/dnf/dnf.conf. The disk usage guide shows how to find other large folders.
How do I fix a broken or locked package manager?
An error saying the dpkg lock is held usually means an automatic update is running. Wait a few minutes and check with ps aux | grep -E "apt|dpkg"; do not delete lock files while a process holds them. If an install was interrupted, finish it:
sudo dpkg --configure -a
sudo apt -f installOn Rocky and Alma, rebuild metadata and check the database after a crash:
sudo dnf clean all
sudo dnf makecache
sudo rpm --rebuilddbSignature errors after adding a repository mean the key is missing or expired; reimport it from the vendor.
Questions about apt and dnf package management
What is the difference between apt and apt-get?
apt is the friendlier interactive command with progress bars. apt-get has a stable output format and is better inside scripts. They use the same repositories and cache.
Is yum still available on Rocky and Alma?
Yes, yum is a compatibility name that runs dnf on version 9, so old instructions still work. Prefer dnf in new notes.
Should I use snap or Flatpak on a server?
Native packages are simpler to manage and update. Ubuntu delivers a few programs as snaps by default; use them when a vendor recommends it, otherwise stick with apt.
How often should I update?
Apply security updates at least weekly, or automatically. Bigger version changes deserve a quick read of release notes and a backup first.
Can I install a .deb or .rpm file I downloaded?
Yes: sudo apt install ./file.deb or sudo dnf install ./file.rpm resolve dependencies. You lose automatic updates unless the vendor also offers a repository.
More in Server basics
- Create a sudo user
- Add a swap file
- Change the hostname
- Time zone and time sync
- File and directory permissions
- Choose a Linux distribution
- Upgrade Ubuntu to the next LTS
- Upgrade Debian to a new major release
- Rocky or AlmaLinux to a new major version
- Automatic security updates
- Run an app as a systemd service
- Schedule jobs with cron and systemd timers
- Environment variables on Linux
- Check disk space and find what fills it
- Inspect processes and memory
- Read the hardware and system report
- Reboot and shut down safely
- Customise the login message (MOTD)
- Remote desktop with Xfce and xrdp
- SELinux on Rocky Linux and AlmaLinux
- Keep sessions alive with tmux
- SFTP-only users in a chroot
- First-hour checklist for a new server
- Upgrade FreeBSD to a new release