Package Management: apt, snap & flatpak
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:
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.gpgresolute 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
sudo apt update
sudo apt upgrade
sudo apt install htop tree
sudo apt remove tree
sudo apt autoremoveHit: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:
| Command | What it does |
|---|---|
apt update | Downloads the latest list of available packages. Installs nothing |
apt upgrade | Installs newer versions of installed packages |
apt full-upgrade | Like upgrade, but may also remove packages when needed to finish |
apt install name | Installs a package and its dependencies |
apt remove name | Removes a package but keeps its system config files |
apt purge name | Removes a package and its config files |
apt autoremove | Removes 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:
sudo apt install htopInstalling: 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
apt search "image viewer"
apt show nginx
apt list --installed | grep python3
apt list --upgradableapt 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:
dpkg -L htop | head -n 3
dpkg -S /usr/bin/ssh/. /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.
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 updateThe .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.
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:
sudo apt install flatpak
flatpak remote-add --if-not-exists flathub https://dl.flathub.org/repo/flathub.flatpakrepo
flatpak install flathub org.gimp.GIMPOn 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:
| Task | Ubuntu / Debian | Fedora / RHEL |
|---|---|---|
| Install | sudo apt install htop | sudo dnf install htop |
| Upgrade everything | sudo apt update && sudo apt upgrade | sudo dnf upgrade |
| Remove | sudo apt remove htop | sudo dnf remove htop |
| Search | apt search htop | dnf search htop |
| Which package owns a file | dpkg -S /usr/bin/ssh | rpm -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
upgradewithoutupdate. apt only knows about old versions, so nothing new is installed. - Using
removewhen you want a clean slate. Config files stay behind; usepurge. - Installing random
.debfiles orcurl ... | sudo bashscripts 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 updaterefreshes package lists;sudo apt upgradeinstalls newer versions.apt install,remove,purgeandautoremovemanage individual packages;apt searchandapt showfind and describe them.dpkg -Llists a package's files anddpkg -Sfinds which package owns a file.- Ubuntu 26.04 uses APT 3.1: deb822
.sourcesfiles,Signed-Bykeys andapt history-*commands;apt-keyis gone. - Snap and flatpak bundle apps with their libraries; Fedora uses
dnfwith.rpmpackages.
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
sudo apt update
apt list --upgradable
sudo apt install cowsay tree
dpkg -L cowsay | grep /usr/games
sudo apt purge cowsay
sudo apt autoremove/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?
update only refreshes the package lists; upgrade installs newer versions.
2. Which command removes a package together with its configuration files?
purge removes config files too; remove leaves them behind.
3. Which command tells you which package installed /usr/bin/ssh?
dpkg -S searches installed packages for the file.
4. What replaced apt-key for repository keys on Ubuntu 26.04?
Keys are stored as separate files and tied to their own repository with Signed-By.
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
