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

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
| Command | Does |
|---|---|
apt update | Downloads the current lists of available packages. Changes nothing on the system |
apt upgrade | Installs newer versions of what is installed. Never removes a package |
apt install | Installs a package and what it depends on |
apt remove | Removes 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=versioninstalls a particular version../file.debinstalls a downloaded package and fetches its dependencies, which plaindpkg -idoes not.--no-install-recommendsleaves out optional extras; useful on small servers.-yanswers "yes" for scripts.
Removing and cleaning
sudo apt remove nginx
sudo apt purge nginx
sudo apt autoremove
sudo apt clean
| Command | Removes |
|---|---|
remove | The program |
purge | The program and its system-wide configuration files |
autoremove | Dependencies nothing needs any more, old kernels included |
clean | Downloaded 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
showprints the description, size and dependencies.policyshows the installed version, the candidate, and which repository each comes from.dpkg -Llists the files a package installed;dpkg -Sanswers "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
| Tool | Use it |
|---|---|
apt | At the keyboard: progress bars, colours, sensible defaults |
apt-get, apt-cache | In scripts: their output format is kept stable |
dpkg | The layer underneath, for single package files and queries |
snap, flatpak | Separate systems for self-contained applications |
When apt complains
| Message | Fix |
|---|---|
| "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 update | The release is no longer supported, or a third-party repository has moved |
| "NO_PUBKEY" or signature errors | The 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.