Linux Command Line · Lesson 33 of 43
Installing software with apt and dnf
Install, update, search and remove software with the apt and dnf package managers, understand repositories, and install safely from trusted sources.
UNDERSTAND IT. THEN TRY IT.
What you can do after this lesson
You will install, update and remove software the Linux way, from a command line and without hunting for download pages. A package manager is one of the best things about Linux: one command installs a program, everything it needs, and keeps it updated along with the rest of the system.
Installing software changes a system and needs administrator rights, so the commands in this lesson are shown as text. Run them only on a machine you own, ideally a virtual machine or a test server.
What a package manager does
A package is an archive holding a program, its files and information about what other packages it needs (its dependencies). A repository is a server that stores many packages and a catalogue of them. The package manager talks to the repositories, works out what to install, downloads it, verifies cryptographic signatures, installs the files and records what was installed so it can later remove or update it cleanly.
Each family of distributions has its own tools:
| Family | Package format | Main command |
|---|---|---|
| Debian, Ubuntu, Linux Mint | .deb |
apt |
| Fedora, RHEL, AlmaLinux, Rocky | .rpm |
dnf |
| Arch Linux | .pkg.tar.zst |
pacman |
| Alpine | .apk |
apk |
| openSUSE | .rpm |
zypper |
This lesson shows apt and dnf, which cover the most common systems. The ideas are the same everywhere; only the words differ.
apt on Debian and Ubuntu
Start by refreshing the package index, the local copy of what the repositories offer. Do this before installing:
sudo apt updateThen the everyday commands:
apt search json look for packages by keyword
apt show jq details of one package
sudo apt install jq htop install one or more packages
sudo apt remove htop remove a package but keep its settings
sudo apt purge htop remove it with its settings
sudo apt autoremove remove dependencies nobody needs any more
apt list --installed what is installed
sudo apt upgrade install newer versions of everything installedapt update and apt upgrade are different: update refreshes the catalogue and installs nothing, while upgrade installs newer versions of what you already have. Run them in that order, sudo apt update && sudo apt upgrade, to bring a system up to date. apt shows what it plans to do and asks for confirmation; -y answers yes automatically, which is useful in scripts but dangerous at the keyboard.
dnf on Fedora, Red Hat and AlmaLinux
dnf has nearly the same shape. It has no separate update-the-catalogue step like apt update: it downloads the repository metadata again when its cached copy has expired, and --refresh forces that:
dnf search json
dnf info jq
sudo dnf install jq htop
sudo dnf remove htop
sudo dnf upgrade
sudo dnf autoremove
dnf list --installedOlder Red Hat systems use yum, the predecessor of dnf. Its commands are very similar, and many current systems still accept yum as another name for dnf.
Finding out what a package gave you
After installation, you may want to know which files a package put on the system or which package owns a file:
dpkg -L jq files installed by a package (Debian, Ubuntu)
dpkg -S /usr/bin/jq which package owns this file
rpm -ql jq files installed by a package (Fedora, Red Hat)
rpm -qf /usr/bin/jq which package owns this fileThis answers questions such as "where did it put its configuration?" and "which package do I need to get this command?". Many systems also suggest the right package when you type an unknown command, as in "Command 'jq' not found, but can be installed with: sudo apt install jq".
Trusting software
Packages from your distribution's repositories are signed and checked by the distribution, so they are by far the safest source. Be more careful with the other ways to install software:
- Third-party repositories (a vendor's own
aptordnfsource) are fine for well-known projects, but you must trust the vendor. Adding one means they can supply updates to your whole system. - Language package managers such as
pip,npmandgeminstall into your project or user account. Avoid usingsudowith them. - Downloaded
.deband.rpmfiles can be installed withsudo apt install ./file.deborsudo dnf install ./file.rpm. Make sure that they come from the official site. - "Pipe to shell" installers, such as
curl https://example.com/install.sh | sudo bash, run code you have not read with full rights. Download the script first, read it, and then run it, or prefer a package. - Compiling from source is possible, but it lives outside the package manager's records, so updates and removal are your job.
Keeping a system healthy
Update regularly, because most security fixes arrive as package updates. On servers, many administrators enable unattended security updates (unattended-upgrades on Debian and Ubuntu, dnf-automatic on Fedora and Red Hat). Do not mix repositories from different releases, and read the list of changes before approving a large upgrade. If a package manager reports a lock, another process, perhaps an automatic update, is using it. Wait a minute and try again, and do not delete lock files.
Common mistakes
Forgetting apt update. You may install an old version, or get "package not found".
Using apt commands on Fedora, or dnf on Ubuntu. Check the family of your distribution with cat /etc/os-release.
Using -y blindly. Read what will be removed. A removal request can take many packages with it.
Installing from random websites. Prefer repositories, and verify any download.
Exercise
On a test machine or virtual machine, find out which family your distribution belongs to by reading /etc/os-release. Update the index, search for tree, show its details, install it, run tree on a folder, find out which package owns /usr/bin/tree, and then remove it again.
KNOWLEDGE CHECK
A quick check. You’ve got this.
Try each question. Every answer comes with an explanation.
Ready for the next small win?
Mark the lesson complete when you are ready.