= DNF and APT compared

There are https://opensource.com/article/18/1/how-install-apps-linux[many ways to get applications onto a Linux system].
Some, like Flatpak and containers, are new.
Others, like DEB and RPM, are classic formats that have withstood the test of time.
There isn't a universal installer for /any/ OS.
Today, all major operating systems use a mix of app stores (both first and third party), drag-and-drop installs, and install wizards because there are innumerable developers delivering software.
Different developers have different requirements for the code they deliver, and this informs the install method they each choose.

Linux pioneered the concept of a /package manager/, a command to install, manage, and uninstall applications.
Two common package manager commands are `apt` and `dnf`.
The `apt` command manages DEB packages, while `dnf` manages RPM packages.
The two are not strictly exclusive of one another, in theory, although in practise a Linux distribution generally chooses on one or the other.
It's theoretically possible to run both on one system, but package installs would overlap, versioning would be difficult, and in the end the commands themselves would be redundant to one another.
However, if you work in a mixed Linux environment, interacting with workstations running one distribution and servers running another, then you may well have a need to know both.

== Searching for applications

Before you can install an application with a package manager, you usually need to know the name of the package.
Usually, the application name and the package name are the same.
The process to verify the name of the package you want to install is exactly same on both `dnf` and `apt`:

[source,bash]
----
$ sudo dnf search zsh
====== Name Exactly Matched: zsh ======
zsh.x86_64 : Powerful interactive shell
[...]
----

With `apt`:

[source,bash]
----
$ sudo apt search zsh
Sorting... Done
Full Text Search... Done
csh/stable 20110502-4+deb10u1 amd64
 Shell with C-like syntax

ddgr/stable 1.6-1 all
 DuckDuckGo from the terminal

direnv/stable 2.18.2-2 amd64
 Utility to set directory specific environment variables

draai/stable 20180521-1 all
 Command-line music player for MPD
[...]
----

To get relevant results from `apt` earlier in the search, you can use https://opensource.com/article/18/5/getting-started-regular-expressions[regex]:

[source,bash]
----
apt search ^zsh
Sorting... Done
Full Text Search... Done
zsh/stable 5.7.1-1 amd64
 shell with lots of features
[...]
----

== Finding a package that provides an application

However, there are commands that come bundled with other commands, all in one package.
When that happens, you can use your package manager to learn what package provides what you need.
The `dnf` and `apt` commands diverge on how they accomplish a search for this kind of metadata.

On `dnf`:

[source,bash]
----
$ sudo dnf provides pgrep
procps-ng-3.3.15-6.el8.x86_64 : System and process monitoring utilities
Repo        : baseos
Matched from:
Filename    : /usr/bin/pgrep
----

The `apt` command uses a subcommand, `apt-file`.
To use `apt-file`, you must first install it, and then prompt it to update its cache.

[source,bash]
----
$ sudo apt install apt-file
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
 libapt-pkg-perl libexporter-tiny-perl liblist-moreutils-perl libregexp-assemble-perl
The following NEW packages will be installed:
 apt-file libapt-pkg-perl libexporter-tiny-perl liblist-moreutils-perl libregexp-assemble-perl
0 upgraded, 5 newly installed, 0 to remove and 14 not upgraded.
Need to get 297 kB of archives.
After this operation, 825 kB of additional disk space will be used.
Do you want to continue? [Y/n] y

$ sudo apt-file update
[...]
----

With `apt-file` installed, you can use it to search for a command.
You can cast a very wide net by just searching for the command, but if you happen to know the expected path of the command, it's more accurate:

[source,bash]
----
$ sudo apt-file search /usr/bin/pgrep
pgreplay: /usr/bin/pgreplay
procps: /usr/bin/pgrep
----

== Installing applications

Installing applications is essentially an identical process with `apt` and `dnf`:

[source,bash]
----
$ sudo apt install zsh
----

With `dnf`, you can install a single package using the same option:

[source,bash]
----
$ sudo dnf install zsh
----

Many RPM-based distributions feature install _groups_, which is a means to collect sometimes superficially related applications into one easily installable target.
For instance, the *Design Suite* group in Fedora contains popular creative applications, and because many artists who want one creative application are highly likely to want similar applications, installing the whole group is an easy and quick way to get a sensible start on building a digital studio.
You can view available groups with `group list` (use `-v` to see the group names without spaces):

[source,bash]
----
$ sudo dnf group list -v
[...]
Available Groups:
  Container Management (container-management)
  RPM Development Tools (rpm-development-tools)
  Design Suite (design-suite)
  Development Tools (development)
[...]
----

Install an RPM group using the `group install` subcommands:

[source,bash]
----
$ sudo dnf group install design-suite
----

You can alternately use the `@` notation to reduce typing:

[source,bash]
----
$ sudo dnf install @design-suite
----

== Upgrading applications

One advantage of using a package manager is that the package manager is aware of _all_ the applications it has ever installed.
That means you don't have to go hunting for updated versions of applications.
Instead, you have only to tell your package manager to scan for updates.

The subcommands used by `dnf` and `apt` are slightly different.
Because `apt`  keeps a cache of information that itself requires regular updating, it uses the `upgrade` subcommand for application updates.

[source,bash]
----
$ sudo apt upgrade
----

By contrast, `dnf` updates metadata every time you use the command, so the `update` and `upgrade` subcommands are interchangeable.

[source,bash]
----
$ sudo dnf upgrade
----

This is the same as:

[source,bash]
----
$ sudo dnf update
----

== Removing applications

If you've ever tried to remove an application manually on any platform, then you know that there are inevitably left-over files, such as preference files or assets or icons, scattered all around your hard drive after the application itself has been removed.
Yet another advantage to using a package manager is that your package manager has knowledge of _every single file_ installed with a package.

[source,bash]
----
$ sudo dnf remove zsh
----

The `remove` subcommand is also used for `apt`:

[source,bash]
----
$ sudo apt remove zsh
----

Removing a package with `apt` doesn't remove any modified user configuration files, just in case the removal was an accident.
If you want `apt` to remove an application and its configuration files, use `purge` on an application you've previously removed.

[source,bash]
----
$ sudo apt purge zsh
----

Both `apt` and `dnf` (even with `purge`) doesn't remove data or configuration files in your home directory.
To remove data from your home directory, you must do so manually (they're usually found in `~/.config` and `~/.local`.)

== Learning package management

Whether your Linux distribution of choice favours `apt` or `dnf`, the purpose of the commands are broadly identical.
They help you install, update, and remove packages.
These two, being the most common package managers, largely mirror one another's most important syntactical elements, so switching between them is pretty easy.

There are some advanced features of each, such as repository management, that diverge substantially, but those tend not to be run as frequently as the classic sequence of `search` and `install`.

Regardless of which of these two package managers you use more often, you can download both our https://opensource.com/downloads/apt-cheat-sheet[apt cheat sheet] and https://opensource.com/downloads/dnf-cheat-sheet[dnf cheat sheet] so you can have the most important syntax close at hand when you need it the most.