Managing Packages with apt on Ubuntu
The core apt commands every Ubuntu Server user needs - updating, installing, removing, searching, holding versions, and cleaning up unused packages.
6 December 2024 3 min read
apt is the package manager you’ll use most on an Ubuntu Server VPS. This covers the everyday commands: keeping your system up to date, installing and removing software, searching for packages, and tidying up afterwards. Everything here applies equally to Debian, since Ubuntu’s package tooling is built directly on top of it.
Updating package lists and upgrading
apt update refreshes the local list of available packages and versions from your configured repositories. It doesn’t install anything itself – run it first, before installing or upgrading anything:
sudo apt update
apt upgrade then installs the newest available versions of your currently installed packages:
sudo apt upgrade
For upgrades that may need to install or remove packages to resolve dependencies (for example a new kernel version), use:
sudo apt full-upgrade
Installing and removing packages
sudo apt install nginx
sudo apt remove nginx
apt remove uninstalls the package but leaves configuration files behind. To remove those as well, use:
sudo apt purge nginx
Searching for packages
apt search nginx
To see details about a specific package before installing it – version, description, dependencies:
apt show nginx
Listing installed packages
apt list --installed
Pipe it through grep to check for a specific package:
apt list --installed | grep nginx
apt vs apt-get
You’ll see both apt and apt-get used in documentation and tutorials. For almost everything covered here, they’re interchangeable – apt is the newer, user-facing command introduced to consolidate the most commonly used features of apt-get and apt-cache into one tool with better default output (progress bars, colour) and more sensible defaults. apt-get still exists and is more stable for use in scripts, since its output format is guaranteed not to change between versions. For interactive, day-to-day use on your VPS, apt is the one to reach for.
Holding a package at its current version
If you need to stop a specific package from being upgraded – for example, you’re pinned to a known-working version of a database engine – you can put it on hold:
sudo apt-mark hold nginx
Held packages are skipped by apt upgrade until you release the hold:
sudo apt-mark unhold nginx
Check what’s currently held with:
apt-mark showhold
Use holds sparingly – a held package won’t receive security patches either, so you’re taking on responsibility for tracking that yourself.
Cleaning up
Over time, installing and removing packages leaves behind dependencies that are no longer needed by anything else on the system. Clear these out with:
sudo apt autoremove
You can also clear apt’s local cache of downloaded .deb files, which is safe to do and can free up meaningful disk space on a small VPS:
sudo apt clean
A sensible routine to run periodically is apt update, followed by apt upgrade, followed by apt autoremove – though for security patches specifically, you may want to automate this. We cover that in our post on unattended-upgrades.