Chapter 2: apt and .deb Packages


apt is the package manager for Debian-based Linux distributions, including Ubuntu. It handles downloading, installing, upgrading, and removing software, resolving dependencies automatically. It is the first tool you should reach for when installing software on Ubuntu.


How apt Works

apt manages packages from repositories — servers that host pre-compiled software. A package is a .deb file containing the software binary, configuration, and metadata. The package database on your machine tracks what is installed and what updates are available.

Before installing anything, update the local package index to fetch the latest package lists from the repositories:

sudo apt update

Essential Commands

Installing and removing packages

sudo apt install package-name          # install a package
sudo apt install package1 package2     # install multiple at once
sudo apt remove package-name           # remove (keep config files)
sudo apt purge package-name            # remove including config files
sudo apt autoremove                    # remove unused dependencies

Upgrading

sudo apt upgrade                       # upgrade all installed packages
sudo apt full-upgrade                  # upgrade, removing packages if needed

Searching and inspecting

apt search keyword                     # search package names and descriptions
apt show package-name                  # show package details
apt list --installed                   # list all installed packages
apt list --installed | grep name       # filter by name

apt vs apt-get

You will see both apt and apt-get in documentation. apt is the newer, more user-friendly interface. apt-get is the older plumbing layer that is still widely used in scripts because its output format is stable. For interactive use, prefer apt. In shell scripts, apt-get is conventional.


PPAs: Personal Package Archives

PPAs allow third-party developers to distribute packages through Ubuntu’s infrastructure. They are useful for software not in the official repositories or for getting newer versions:

sudo add-apt-repository ppa:developer/repo-name
sudo apt update
sudo apt install package-name

Trust warning: PPAs run arbitrary code on your system. Only add PPAs from developers you trust. Check the PPA page on Launchpad for activity and user comments before adding.

To remove a PPA:

sudo add-apt-repository --remove ppa:developer/repo-name

Installing a .deb File Directly

When a developer provides a .deb file for direct download (common for apps like VS Code, Chrome, Discord), install it with dpkg:

sudo dpkg -i package.deb

dpkg does not resolve dependencies. If the install fails with dependency errors, fix them with:

sudo apt -f install

This tells apt to fetch and install whatever is missing, then retry the original package install.

A cleaner alternative that handles dependencies automatically:

sudo apt install ./package.deb

Note the ./ prefix — it tells apt to treat the argument as a local file rather than a repository package name.


Holding a Package Version

To prevent a specific package from being upgraded (useful for pinning a known-good version):

sudo apt-mark hold package-name        # hold at current version
sudo apt-mark unhold package-name      # release the hold
apt-mark showhold                      # list held packages

Finding Which Package Owns a File

dpkg -S /usr/bin/python3               # which package installed this file
dpkg -L package-name                   # list all files from a package

Cleaning Up

sudo apt autoremove                    # remove orphaned dependencies
sudo apt clean                         # delete cached .deb files
sudo apt autoclean                     # delete only outdated cached files

Key Takeaways


← Chapter 1: SSH Keys Table of Contents Chapter 3: Snap Packages →