Linux commands
Browse topics

Installing packages on Debian and Ubuntu with apt

Version
unversioned
Review
Draft

Summary#

apt installs, upgrades and removes packages on Debian, Ubuntu and related distributions. dpkg records what is installed.

Syntax#

sh
sudo apt update
sudo apt install nginx
sudo apt upgrade
sudo apt remove nginx
sudo apt purge nginx
apt-cache policy nginx
apt search nginx
dpkg -s bash
dpkg -L bash

apt update refreshes the package lists and installs nothing. remove keeps configuration files and purge deletes them. Scripts should use apt-get and apt-cache, whose output is more stable than that of apt.

Example#

This example only reads the package database.

sh
(
  set -eu
  dpkg -s bash | grep '^Package:'
  apt-cache policy bash | head -n 1
  dpkg-query -W -f='${db:Status-Status}\n' bash
)

Expected output#

Text
Package: bash
bash:
installed

Common mistakes#

Unable to locate package often means the lists are stale or empty, the name is wrong, or the package lives in a repository you have not enabled. Run sudo apt update and check with apt-cache policy. Could not get lock means another package manager is running; wait for it to finish rather than deleting lock files. In scripts and images, pass -y only when you accept the listed changes, and run apt-get update in the same step as the install. Review removals before confirming an upgrade.

Version applicability#

This entry describes APT 3 on Debian-based distributions.

Sources#

Attribution
DevLookup contributors
License
CC-BY-4.0