On Ubuntu, run apt update to refresh package lists, then review what apt upgrade proposes before accepting. If an update fails or packages are kept back, diagnose the cause before using full-upgrade. For unfamiliar machines, check VERSION_ID in /etc/os-release and inspect the configured repositories. A release upgrade is a separate operation.
apt Basics: install, remove, search, update, upgrade
Everything starts with the package index. Before you install or upgrade anything, refresh it:
sudo apt update
The command refreshes indexes from configured sources, including .sources and .list files under /etc/apt/sources.list.d/. Review download errors before relying on the results. Updating the index leaves installed package versions unchanged.
Installing a package is exactly what you’d expect:
sudo apt install vim
Want to install a specific version? Append the version string:
sudo apt install vim=2:9.0.1000-4ubuntu2
Removing a package:
sudo apt remove vim
That leaves config files behind. If you want everything gone - package files and configuration:
sudo apt purge vim
Searching the package index:
apt search "text editor"
For a tighter listing when you know the exact name:
apt list --installed | grep vim
Upgrading all installed packages:
sudo apt upgrade
The Ubuntu apt manual distinguishes apt upgrade, which can install new dependencies and keeps existing packages installed, from full-upgrade, which can remove packages. Plain apt-get upgrade neither adds nor removes packages by default. Check for holds and phased updates before assuming the solver needs to remove anything. Preview a full upgrade before deciding whether to apply it.
apt -s full-upgrade
Check proposed removals for desktop, kernel and service packages you depend on. An unexpected removal is a reason to inspect package origins and dependencies first.
apt vs apt-get: When to Use Which
Here’s the short version: use apt when you’re typing commands interactively; use apt-get and apt-cache in scripts, cron jobs, and Dockerfiles.
Why? apt was introduced in Ubuntu 14.04 as a friendlier front end. It merges functionality from apt-get, apt-cache, and apt-mark into a single command, adds a progress bar, and color-codes output. It’s genuinely nicer to use at a terminal prompt. But its output format isn’t considered a stable API - the Debian team reserves the right to change how apt displays information between releases. Scripts that parse apt output can break silently after a dist-upgrade.
apt-get output, on the other hand, has been stable for decades. If you’re writing a provisioning script, a CI pipeline, or anything that will run unattended, reach for apt-get install -y and apt-cache search. Your future self will thank you. For a more detailed breakdown of the apt and apt-get ecosystem, the Ubuntu Community Help Wiki is an excellent reference maintained by the community.
Practically speaking, the commands map almost one-to-one:
Interactive (apt) | Scripting (apt-get / apt-cache) |
|---|---|
apt install | apt-get install |
apt remove | apt-get remove |
apt update | apt-get update |
apt upgrade | apt-get upgrade |
apt full-upgrade | apt-get dist-upgrade |
apt search | apt-cache search |
apt show | apt-cache show |
Note the full-upgrade / dist-upgrade naming difference. Same operation, different label.
Pinned Packages and Hold
Sometimes you need a package to stay put. Maybe a kernel update broke your Nvidia driver last time. Maybe your application depends on a specific library version. That’s what package holds are for.
Mark a package as held:
sudo apt-mark hold linux-image-generic
Verify what’s held:
apt-mark showhold
Release the hold when you’re ready:
sudo apt-mark unhold linux-image-generic
Held packages will not be upgraded by apt upgrade or apt full-upgrade. They show up in the “kept back” list as a reminder.
For more granular control - say, you want to pin a package to a specific repository or version range - edit /etc/apt/preferences.d/. This example uses a fictitious package and version pattern:
Package: example-package
Pin: version 1.2.*
Pin-Priority: 1001
A priority above 1000 can make a downgrade eligible. Review the candidate with apt policy PACKAGE and simulate the transaction.
Useful Flags and Patterns
A handful of flags make daily work noticeably smoother.
Dry run - see what would happen without changing anything:
sudo apt install --dry-run nginx
Reinstall - restore files shipped by a package when its installation is damaged:
sudo apt install --reinstall openssh-server
A reinstall can retain your existing configuration. Check the relevant configuration and service status separately.
Download only - grab .deb files without installing:
sudo apt install --download-only build-essential
The files land in /var/cache/apt/archives/. Speaking of that directory: it grows forever unless you clean it. Free the space:
sudo apt clean
Or keep only the latest cached version of each package:
sudo apt autoclean
Autoremove - clean up packages that were pulled in as dependencies but are no longer needed:
apt -s autoremove
Review the list before running the operation with sudo. A package can be marked as automatic even when another application or a local workflow still depends on it.
Common Pitfalls
Upgrading against stale indexes. Refresh the package lists before comparing versions. If a repository failed to refresh, inspect its error before proceeding.
Identify who holds the dpkg lock
Use the PID reported by APT and check it with ps -fp PID. To identify processes using the lock files, run sudo fuser -v /var/lib/dpkg/lock-frontend /var/lib/dpkg/lock without a kill option. An unattended update or an open Software Updater may be doing legitimate work. Wait for it to finish and retry the original command.
The locks are held by processes and are released when those processes exit. Leave the lock files in place. Deleting them can let two package managers modify the database at once. For a confirmed frozen process, make sure the old package manager has exited before starting another. See the Debian dpkg team FAQ.
Finish an interrupted configuration
Once the lock is free, inspect the package state with dpkg --audit. If APT reports an interrupted configuration, sudo dpkg --configure -a resumes pending configuration. For broken dependencies, use apt -s --fix-broken install to inspect the plan first. Run apt check afterwards and check the affected service or application. If the failure repeats, inspect /var/log/apt/term.log and /var/log/dpkg.log and keep the original error with the package name and Ubuntu version.
Mixing PPAs carelessly. Third-party PPAs can overwrite official packages with untested versions. Before adding a PPA, check what packages it provides (apt policy package-name after adding). Remove PPAs you no longer need with sudo add-apt-repository --remove ppa:owner/name.
Assuming a held-back package needs removal of dependencies. Check apt-mark showhold, apt policy PACKAGE and any phased-update message. Only consider a full upgrade after reviewing its simulation.
Purging when you meant to remove. apt purge deletes configuration files system-wide. If you purge a database server, its config (and sometimes data directory references) go with it. Use remove first, verify you don’t need the configs, then purge if you want a truly clean slate.
Going deeper with Linux? If package management is the skill you’re building right now, a structured course can accelerate the process considerably. Mastering Linux - The Comprehensive Guide covers package management, shell scripting, networking and systemd.
Related Reading
When you are choosing an installation source for a desktop app, choose between DEB, Snap and Flatpak before adding a repository.
