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;
aptfetches 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.
aptis 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:
| Command | Role | Use it for |
|---|---|---|
apt | Friendly front end | Interactive installs, search, show |
apt-get | The stable, script-friendly interface | Shell scripts (its output never changes) |
apt-cache | Queries the package lists | search, 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 updatefirst. “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 upgradeand stopping.upgradenever removes packages; if the output mentions “kept back”, finish the job withsudo apt full-upgrade. - Letting
autoremoverun 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 -iwith expectations. Manual.debinstalls that fail leave the system half-broken untilsudo apt install -frepairs it. - Editing sources by hand without backup. The repository configuration lives in
/etc/apt/sources.listand/etc/apt/sources.list.d/; copy the file before editing, and alwaysapt updateafter changing it. - Running
aptas a normal user. Installing and removing need root;aptwill tell you (Could not open lock file) rather than half-do the work.
Related Pages
| Page | Why it is here |
|---|---|
| Package Management | The concept section — packages, repos, and why managers exist. |
| dnf | The same job on Fedora and RHEL systems. |
| pacman | The same job on Arch Linux. |
| snap | Cross-distro packages that live beside apt on Ubuntu. |
| flatpak | Desktop apps delivered independently of apt. |
| sudo | Why most of these commands need it. |
apt Cheatsheet
| Command | What it does |
|---|---|
sudo apt update | Refresh the lists of available packages. |
sudo apt install htop | Install a package (and its dependencies). |
sudo apt install -y git curl | Install several packages without asking. |
sudo apt upgrade | Upgrade every installed package, safely. |
sudo apt full-upgrade | Upgrade, allowing packages to be removed if needed. |
sudo apt remove htop | Uninstall a package, keep its config. |
sudo apt purge htop | Uninstall a package and its config files. |
sudo apt autoremove | Remove orphaned dependency packages. |
apt search editor | Search package names and descriptions. |
apt show nginx | Show details of one package. |
apt list --installed | List installed packages. |
apt list --upgradable | List packages with newer versions available. |
apt depends curl | Show what a package needs. |
apt rdepends curl | Show what needs a package. |
apt download htop | Download a .deb into the current directory. |
sudo apt install -f | Repair broken dependencies after a failed dpkg install. |
dpkg -i file.deb | Install a local .deb (no dependency resolution). |
dpkg -L htop | List files owned by a package. |
dpkg -S /usr/bin/htop | Find which package owns a file. |