Save 10%Pay yearly on any plan. 6% half-yearly, 3% quarterly. Help centreWhatsAppTelegram TeamsOpen a ticket

APT Command Examples for Ubuntu and Debian

The apt commands you need on Ubuntu and Debian, with examples: update, install, remove, search, hold, clean up, and what to do when apt reports an error.

3 min readBy Soumya

Reviewed by the AmazingRDP technical team

APT command examples for Ubuntu and Debian

Practise on a server of your own: a Linux VPS with root access. See Linux VPS from $6.99

APT, the Advanced Package Tool, installs, updates and removes software on Debian, Ubuntu and their relatives. It fetches packages from repositories (signed collections maintained by the distribution), works out what else each one needs, and keeps the whole system consistent. Nearly everything you install on these systems should come through it. The commands below need root rights, hence sudo.

The daily four

sudo apt update
sudo apt upgrade
sudo apt install nginx
sudo apt remove nginx
CommandDoes
apt updateDownloads the current lists of available packages. Changes nothing on the system
apt upgradeInstalls newer versions of what is installed. Never removes a package
apt installInstalls a package and what it depends on
apt removeRemoves a package; keeps its configuration files

Always run update before install or upgrade, or apt works from an old list and may ask for files that no longer exist.

Updating

sudo apt update && sudo apt upgrade
sudo apt full-upgrade
apt list --upgradable

full-upgrade may also remove packages when that is needed to complete an upgrade; read its summary before confirming. The last line shows what would be updated.

Installing

sudo apt install git curl htop
sudo apt install nginx=1.24.0-2ubuntu7
sudo apt install ./package.deb
sudo apt install --no-install-recommends imagemagick
sudo apt reinstall nginx
sudo apt install -y unzip
  • Several packages go in one command.
  • name=version installs a particular version.
  • ./file.deb installs a downloaded package and fetches its dependencies, which plain dpkg -i does not.
  • --no-install-recommends leaves out optional extras; useful on small servers.
  • -y answers "yes" for scripts.

Removing and cleaning

sudo apt remove nginx
sudo apt purge nginx
sudo apt autoremove
sudo apt clean
CommandRemoves
removeThe program
purgeThe program and its system-wide configuration files
autoremoveDependencies nothing needs any more, old kernels included
cleanDownloaded package files in the cache

Read the list autoremove shows before agreeing. When a disk fills up, autoremove and clean are the first things to try: see disk space on Linux and ncdu.

Finding and inspecting

apt search "web server"
apt show nginx
apt list --installed
apt list --installed | grep php
apt policy nginx
dpkg -L nginx
dpkg -S /usr/sbin/nginx
  • show prints the description, size and dependencies.
  • policy shows the installed version, the candidate, and which repository each comes from.
  • dpkg -L lists the files a package installed; dpkg -S answers "which package does this file belong to?"

Holding a package back

sudo apt-mark hold nginx
apt-mark showhold
sudo apt-mark unhold nginx

A held package is skipped by upgrades. Use it briefly and deliberately; a package held for a year misses a year of security fixes.

Repositories

Sources are listed in /etc/apt/sources.list and in files under /etc/apt/sources.list.d/. Software makers often provide their own repository, added with a signing key and one file; after that, apt updates their software along with the system. Add only repositories from makers you trust: a repository can replace any package on your system. Examples in Node.js from NodeSource and VS Code on Ubuntu.

apt, apt-get and the rest

ToolUse it
aptAt the keyboard: progress bars, colours, sensible defaults
apt-get, apt-cacheIn scripts: their output format is kept stable
dpkgThe layer underneath, for single package files and queries
snap, flatpakSeparate systems for self-contained applications

When apt complains

MessageFix
"Could not get lock /var/lib/dpkg/lock"Another apt is running, often the automatic updater. Wait a few minutes. Do not delete the lock file
"dpkg was interrupted"sudo dpkg --configure -a
"Unmet dependencies"sudo apt --fix-broken install
"Unable to locate package"Run apt update; check the spelling; the package may be in a repository that is not enabled
"404 Not Found" during updateThe release is no longer supported, or a third-party repository has moved
"NO_PUBKEY" or signature errorsThe repository's signing key is missing or expired; follow its maker's current instructions
"Temporary failure resolving"The server cannot reach DNS: see DNS on Linux

Automatic security updates

sudo apt install unattended-upgrades
sudo dpkg-reconfigure -plow unattended-upgrades

This installs security fixes daily by itself, which a server on the internet should have. Other distributions use other tools: installing RPM packages. A server to practise on: Linux VPS.

Questions

What is the difference between apt update and apt upgrade?

apt update refreshes the list of available packages. apt upgrade installs the newer versions.

What is the difference between apt remove and apt purge?

remove deletes the program but keeps its configuration files. purge deletes those too.

What is the difference between apt and apt-get?

apt is the friendlier command for interactive use. apt-get has stable output and is preferred in scripts.

How do I fix broken packages in Ubuntu?

Run sudo dpkg --configure -a, then sudo apt --fix-broken install.