Package Management: apt, snap & flatpak

Linux · Lesson 19 of 32 7 min read Updated

On Windows you usually download an installer from a website. On Linux you install software from repositories: online collections of packages that your distribution builds, tests and signs. A package manager downloads the right package, checks its signature, installs everything it depends on, and later updates it along with the rest of the system.

This lesson teaches apt, Ubuntu's package manager, in detail, then shows the other systems you will meet: dpkg, snap, flatpak and dnf on Fedora.

Packages and repositories

A package is an archive containing a program's files plus information about it: version, description, and dependencies (other packages it needs). Debian and Ubuntu use .deb packages; Fedora and Red Hat use .rpm.

Ubuntu's repositories are split into components: main (supported by Canonical), universe (community maintained), restricted (proprietary drivers) and multiverse (software with licence restrictions). On Ubuntu 26.04 the list of repositories lives in /etc/apt/sources.list.d/ubuntu.sources, written in the newer deb822 format:

Text
Types: deb
URIs: http://archive.ubuntu.com/ubuntu/
Suites: resolute resolute-updates resolute-backports
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

resolute is the code name of 26.04. Signed-By names the key used to check that packages really come from Ubuntu. Older systems used one-line entries in /etc/apt/sources.list; you will still see that format in old guides.

The everyday apt commands

Terminal
sudo apt update
sudo apt upgrade
sudo apt install htop tree
sudo apt remove tree
sudo apt autoremove
Output
Hit:1 http://archive.ubuntu.com/ubuntu resolute InRelease
Get:2 http://security.ubuntu.com/ubuntu resolute-security InRelease [126 kB]
Get:3 http://archive.ubuntu.com/ubuntu resolute-updates InRelease [126 kB]
Hit:4 http://archive.ubuntu.com/ubuntu resolute-backports InRelease
Fetched 252 kB in 1s (241 kB/s)
12 packages can be upgraded. Run 'apt list --upgradable' to see them.

The output above is from apt update. These five commands do different things, and mixing up the first two is the most common beginner error:

CommandWhat it does
apt updateDownloads the latest list of available packages. Installs nothing
apt upgradeInstalls newer versions of installed packages
apt full-upgradeLike upgrade, but may also remove packages when needed to finish
apt install nameInstalls a package and its dependencies
apt remove nameRemoves a package but keeps its system config files
apt purge nameRemoves a package and its config files
apt autoremoveRemoves dependencies nothing needs any more

Always run apt update before installing or upgrading, so apt knows about the newest versions. Installing a package shows a summary first:

Terminal
sudo apt install htop
Output
Installing:
  htop

Summary:
  Upgrading: 0, Installing: 1, Removing: 0, Not Upgrading: 12
  Download size: 171 kB
  Space needed: 434 kB / 18.2 GB available
...

If more packages are needed, apt lists them and asks Continue? [Y/n]. Add -y to answer yes automatically in scripts.

Searching and inspecting

Terminal
apt search "image viewer"
apt show nginx
apt list --installed | grep python3
apt list --upgradable

apt show prints the version, size, dependencies and a description. To find out where a package's files went, or which package owns a file, use the lower-level tool dpkg:

Terminal
dpkg -L htop | head -n 3
dpkg -S /usr/bin/ssh
Output
/.
/usr
/usr/bin
openssh-client: /usr/bin/ssh

dpkg installs and queries individual .deb files, but it does not download anything or resolve dependencies. If you download a .deb file from a vendor, install it with sudo apt install ./file.deb (the ./ tells apt it is a local file), so apt fetches any dependencies.

Adding a third-party repository

Some software, such as Docker or a newer Node.js, comes from the vendor's own repository. The modern, safe pattern has three steps: save the vendor's signing key into /etc/apt/keyrings, add a .sources file that names that key, then update.

Terminal
sudo install -d -m 0755 /etc/apt/keyrings
curl -fsSL https://packages.example.com/key.gpg | sudo tee /etc/apt/keyrings/example.asc > /dev/null
sudo nano /etc/apt/sources.list.d/example.sources
sudo apt update

The .sources file contains Types, URIs, Suites, Components and Signed-By: /etc/apt/keyrings/example.asc, exactly like Ubuntu's own file. Because the key is tied to one repository, that vendor cannot sign packages that replace Ubuntu's. Always copy the exact URLs from the vendor's official documentation; adding a repository means trusting that vendor with root access to your machine.

Snap and flatpak

Snap (made by Canonical) and flatpak are "universal" package formats. An app arrives bundled with its own libraries and runs in a sandbox, so the same package works on many distributions and updates independently of the system. Ubuntu installs Firefox as a snap by default.

Terminal
snap list
sudo snap install code --classic
sudo snap refresh

--classic gives the app normal access to your files instead of the strict sandbox (code editors need this). Snaps update themselves automatically in the background.

Flatpak is the format most other desktop distributions prefer, with apps from the Flathub store:

Terminal
sudo apt install flatpak
flatpak remote-add --if-not-exists flathub https://dl.flathub.org/repo/flathub.flatpakrepo
flatpak install flathub org.gimp.GIMP

On servers, prefer apt packages: they are smaller, update with the rest of the system and are what documentation usually assumes.

Other distributions: dnf

Fedora, Red Hat Enterprise Linux, Rocky and AlmaLinux use .rpm packages and the dnf command. The ideas are the same:

TaskUbuntu / DebianFedora / RHEL
Installsudo apt install htopsudo dnf install htop
Upgrade everythingsudo apt update && sudo apt upgradesudo dnf upgrade
Removesudo apt remove htopsudo dnf remove htop
Searchapt search htopdnf search htop
Which package owns a filedpkg -S /usr/bin/sshrpm -qf /usr/bin/ssh

dnf refreshes its package lists automatically, so there is no separate update step. Arch Linux uses pacman -S to install and pacman -Syu to update.

Common mistakes

  • Running upgrade without update. apt only knows about old versions, so nothing new is installed.
  • Using remove when you want a clean slate. Config files stay behind; use purge.
  • Installing random .deb files or curl ... | sudo bash scripts from blogs. You are giving that code root. Use official repositories or the vendor's documented method.
  • Interrupting an upgrade. Closing the terminal mid-upgrade can leave packages half-configured. If it happens, run sudo dpkg --configure -a.

Key points

  • sudo apt update refreshes package lists; sudo apt upgrade installs newer versions.
  • apt install, remove, purge and autoremove manage individual packages; apt search and apt show find and describe them.
  • dpkg -L lists a package's files and dpkg -S finds which package owns a file.
  • Ubuntu 26.04 uses APT 3.1: deb822 .sources files, Signed-By keys and apt history-* commands; apt-key is gone.
  • Snap and flatpak bundle apps with their libraries; Fedora uses dnf with .rpm packages.

Exercise

On your Ubuntu system: refresh the package lists, see which packages can be upgraded, install cowsay and tree, find out which files cowsay installed in /usr/games, then remove cowsay completely including its configuration, and clean up unused dependencies.

Show solution
Terminal
sudo apt update
apt list --upgradable
sudo apt install cowsay tree
dpkg -L cowsay | grep /usr/games
sudo apt purge cowsay
sudo apt autoremove
Output
/usr/games
/usr/games/cowsay
/usr/games/cowthink

dpkg -L lists every file from the package, and grep keeps the ones in /usr/games. purge removes the package and its config, and autoremove deletes dependencies that were installed only for it.

Quick Quiz

1. What does sudo apt update do?

2. Which command removes a package together with its configuration files?

3. Which command tells you which package installed /usr/bin/ssh?

4. What replaced apt-key for repository keys on Ubuntu 26.04?

Learn Linux with a mentor

Free lessons get you started. Our Ethical Hacking & Cyber Security training adds real projects and code reviews from working developers.

  • Project-based learning
  • Mentor code reviews
  • Free counselling call