Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

apt Command Guide for Installing Software on Debian and Ubuntu

apt is Debian and Ubuntu’s all-in-one package manager — it installs, updates, and removes software while tracking every dependency for you.


What is apt, and Why Use It?

apt (Advanced Package Tool) is the command you run whenever you want to add software to a Debian or Ubuntu system. It talks to the configured repositories (servers full of packages), downloads what you ask for, and keeps the whole system consistent: every installed package’s dependencies are checked and installed alongside it, and removal is exact because every file’s owner is recorded. apt is the modern, human-friendly front end; dpkg is the low-level tool underneath it, and apt-get is the older interface that apt deliberately borrows its safest commands from.

Why Learn apt?

  • It is the front door to Ubuntu and Debian — servers, desktops, WSL, and Docker images all run on these, and every one of them installs software with apt.
  • Dependencies are automatic — you name the package you want; apt fetches everything it needs too.
  • Safe by design — transactions are tracked, so an interrupted install can be repaired with one command.
  • It teaches the model — once you understand repos, packages, and dependencies here, dnf, pacman, and every other manager is the same idea with different spelling.

apt is the difference between “I need this program” and “I need this program, working, on this machine, tonight” — it does the plumbing while you name the thing you want.


1. Updating the Package Lists

Before installing anything, tell apt what exists. This downloads the lists of available packages from every configured repository — it does not install anything yet.

sudo apt update

Expect output like Hit:1 http://archive.ubuntu.com ... — every repository answered. If a repository says Err or Ign, note it: a failing repo means some packages will not be found.

Run apt update before every install or upgrade. It is the single most skipped step in Linux, and the cause of most “the package does not exist” confusion.


2. Installing a Package

sudo apt install htop

apt resolves dependencies, shows what it will do, and asks for confirmation (Y/n). To skip the question in scripts:

sudo apt install -y htop

Install several at once, or a specific version:

sudo apt install git curl tmux
sudo apt install nginx=1.24.0-2ubuntu7   # exact version pinning

Installing something you already have? apt upgrades it to the newest version in the repositories instead — install and upgrade are two names for overlapping work.


3. Upgrading Installed Packages

sudo apt upgrade

apt upgrade installs the newest version of every package that is already installed — it never removes packages to satisfy a change, which is what makes it the safe everyday choice.

sudo apt full-upgrade

full-upgrade (older spelling: dist-upgrade) is allowed to remove or replace packages when the cleanest upgrade path requires it. Use it after a distribution release upgrade, or when apt upgrade says a package is “kept back”.

To see what would change before committing:

apt list --upgradable          # what has newer versions available
apt upgrade --dry-run          # full simulation, no changes

4. Removing and Purging Packages

sudo apt remove htop

remove uninstalls the package but keeps its configuration files, in case you change your mind.

sudo apt purge htop            # remove the package AND its config files
sudo apt autoremove            # remove packages installed as dependencies that nothing needs anymore

autoremove is quietly essential: every install that pulled in dependencies leaves those dependencies behind when you remove the main package. Run autoremove after big removals and after kernel upgrades to reclaim space.

To remove several packages at once:

sudo apt remove git curl

5. Searching and Inspecting Packages

You rarely remember exact package names. apt searches its lists for you:

apt search editor              # anything matching "editor" in name or description
apt show nginx                  # full details: version, size, dependencies, description
apt list --installed | grep -i python   # what you already have
apt depends curl                # what curl itself needs
apt rdepends curl               # what would break if curl were removed

apt show is the pre-install checklist: read the version, the dependencies, and whether the package is from the main repository or a third-party one before you commit.


6. Downloading Without Installing

Sometimes you want the .deb file itself — to install it elsewhere, or to keep it:

sudo apt install --download-only htop    # downloads to /var/cache/apt/archives
apt download htop                        # downloads into the current directory

The cached .deb files in /var/cache/apt/archives are also your offline insurance: after an upgrade, the old versions sit there until you clean them.

sudo apt clean           # delete all cached .deb files
sudo apt autoclean       # delete only cached files that can no longer be downloaded

apt vs. apt-get vs. apt-cache

apt was introduced to give humans one friendly command; the older tools remain for scripts and habit:

CommandRoleUse it for
aptFriendly front endInteractive installs, search, show
apt-getThe stable, script-friendly interfaceShell scripts (its output never changes)
apt-cacheQueries the package listssearch, show, policy, depends

The overlap is intentional: apt search is apt-cache search with nicer output, and apt install behaves like apt-get install. In scripts, prefer apt-get — its interface is guaranteed stable across releases, while apt’s output may gain prettier formatting.

# same job, two spellings
apt search image viewer
apt-cache search image viewer

dpkg: the Low-Level Tool

Underneath everything sits dpkg, the tool that actually unpacks .deb files. You rarely need it, but you will meet it when a manual download lands on your disk:

dpkg -i htop_3.2.2-1_amd64.deb     # install a local .deb file
dpkg -l | grep htop                 # list installed packages (status "ii" = installed)
dpkg -L htop                        # list every file the htop package owns
dpkg -S /usr/bin/htop               # which package does this file belong to?
dpkg -r htop                        # remove it

The classic trap: dpkg -i somefile.deb installs the package but does not resolve its dependencies. If it fails halfway, fix the system with:

sudo apt install -f        # fix broken dependencies from a failed dpkg install

Rule of thumb: use apt install ./somefile.deb instead — it runs dpkg for you and resolves dependencies. apt never breaks what dpkg started.


Common Pitfalls

  • Forgetting apt update first. “E: Unable to locate package” or “E: Unable to correct problems” after a new release usually means the local lists are stale. Update, then retry.
  • Using apt upgrade and stopping. upgrade never removes packages; if the output mentions “kept back”, finish the job with sudo apt full-upgrade.
  • Letting autoremove run blind. It is safe, but read what it proposes — occasionally it wants to remove something you installed by hand long ago and forgot.
  • Mixing dpkg -i with expectations. Manual .deb installs that fail leave the system half-broken until sudo apt install -f repairs it.
  • Editing sources by hand without backup. The repository configuration lives in /etc/apt/sources.list and /etc/apt/sources.list.d/; copy the file before editing, and always apt update after changing it.
  • Running apt as a normal user. Installing and removing need root; apt will tell you (Could not open lock file) rather than half-do the work.

PageWhy it is here
Package ManagementThe concept section — packages, repos, and why managers exist.
dnfThe same job on Fedora and RHEL systems.
pacmanThe same job on Arch Linux.
snapCross-distro packages that live beside apt on Ubuntu.
flatpakDesktop apps delivered independently of apt.
sudoWhy most of these commands need it.

apt Cheatsheet

CommandWhat it does
sudo apt updateRefresh the lists of available packages.
sudo apt install htopInstall a package (and its dependencies).
sudo apt install -y git curlInstall several packages without asking.
sudo apt upgradeUpgrade every installed package, safely.
sudo apt full-upgradeUpgrade, allowing packages to be removed if needed.
sudo apt remove htopUninstall a package, keep its config.
sudo apt purge htopUninstall a package and its config files.
sudo apt autoremoveRemove orphaned dependency packages.
apt search editorSearch package names and descriptions.
apt show nginxShow details of one package.
apt list --installedList installed packages.
apt list --upgradableList packages with newer versions available.
apt depends curlShow what a package needs.
apt rdepends curlShow what needs a package.
apt download htopDownload a .deb into the current directory.
sudo apt install -fRepair broken dependencies after a failed dpkg install.
dpkg -i file.debInstall a local .deb (no dependency resolution).
dpkg -L htopList files owned by a package.
dpkg -S /usr/bin/htopFind which package owns a file.