Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Packages and Services

Linux apt and apt-get commands

Reference for the Linux apt and apt-get commands: update the index, install, remove, upgrade, search and inspect packages on Debian and Ubuntu, with scripting notes.

apt is the everyday package manager on Debian, Ubuntu, Linux Mint and their relatives. It downloads packages from the repositories listed in /etc/apt/sources.list and /etc/apt/sources.list.d/, resolves dependencies, and hands the actual unpacking to dpkg.

There are three related front ends. apt is the friendly interface for people (progress bars, colours, one command for most jobs). apt-get and apt-cache are the older, stable interfaces whose output and options are meant for scripts. Installing, removing and upgrading need root; searching and showing do not.

Terminal
sudo apt update
sudo apt install PACKAGE...
apt search WORD

Try it

The sandbox has no package index, no network and no root, so these commands are read-only:

CONSOLE
$ sudo apt update
Hit:1 http://deb.debian.org/debian bookworm InRelease
Reading package lists... Done
12 packages can be upgraded. Run 'apt list --upgradable' to see them.

$ sudo apt install -y tree
$ apt show tree | head -4
Package: tree
Version: 2.1.0-1
Priority: optional
Section: utils

$ apt-cache policy tree
tree:
  Installed: 2.1.0-1
  Candidate: 2.1.0-1

What we can run is the version logic apt relies on, through dpkg:

Bash How Debian compares versions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
apt [OPTIONS] COMMAND [ARGUMENTS]
apt-get [OPTIONS] COMMAND [ARGUMENTS]
apt-cache COMMAND [ARGUMENTS]
Command Description
update Download fresh package lists from the repositories. Does not upgrade anything.
upgrade Install newer versions of installed packages without removing any.
full-upgrade (apt-get dist-upgrade) Upgrade, and also remove or add packages when needed to resolve dependencies.
install PKG... Install packages and their dependencies; PKG=VERSION pins a version.
remove PKG... Remove packages but keep configuration files.
purge PKG... Remove packages and their configuration files.
autoremove Remove dependencies no longer needed by anything.
search WORD Search names and descriptions.
show PKG Show details (version, size, dependencies, description).
list [--installed] [--upgradable] List packages.
policy PKG (apt-cache) Show installed and candidate versions and their sources.
download PKG Fetch the .deb file only.
clean, autoclean Delete cached downloads in /var/cache/apt/archives.
Common option Description
-y Answer yes to prompts (needed in scripts and Dockerfiles).
-s, --simulate Show what would happen without doing it.
--no-install-recommends Skip "recommended" extras; keeps images small.
-f, --fix-broken Try to repair broken dependencies.

Returns: 0 on success, 100 for most errors (a package not found, a failed download), and 1 for some others.

Usage notes

  • update before install. The package list is cached; without update, you may get "Unable to locate package" or old versions. update changes the list only; upgrade changes the system.
  • apt vs apt-get in scripts. apt warns "does not have a stable CLI interface" when its output is parsed. Use apt-get (and apt-cache) in scripts, with -y and DEBIAN_FRONTEND=noninteractive to avoid prompts.
  • Dockerfile pattern. RUN apt-get update && apt-get install -y --no-install-recommends PKG && rm -rf /var/lib/apt/lists/* in a single layer keeps the image small and the index fresh.
  • Locks. "Could not get lock /var/lib/dpkg/lock" means another apt or dpkg is running (often the background updater). Wait; do not delete lock files unless you are sure nothing runs.
  • remove vs purge. Use purge to start clean; remove keeps /etc settings for a later reinstall.
  • Hold a package. sudo apt-mark hold PKG prevents upgrades; unhold releases it.
  • Which package owns a file? dpkg -S /path (installed) or apt-file search (any package, needs the apt-file package).
  • Trust. Repositories are verified with signing keys kept in /etc/apt/keyrings/ or /usr/share/keyrings/. Add third-party repositories only from sources you trust; avoid the deprecated apt-key.
  • PPAs (Ubuntu) add extra repositories with add-apt-repository; they are not reviewed by the distribution.