The apt Command in Linux: Common Commands and Examples

The apt Command in Linux: Common Commands and Examples

The confirmation for `sudo apt install nano` comes only after APT calculates dependencies. I’m struck by the step between repository information and the dependency plan APT presents.

Inspect the local index APT uses to calculate package changes. Compare it with the installed package state dpkg tracks.

What apt manages on a Debian-based system

APT, the Advanced Package Tool, manages Debian packages through a high-level command-line interface. The apt command is intended for interactive use, with common actions for finding packages and changing installed software, as described in the Debian apt manual.

Repositories publish package records and archives, while your system keeps a local index of package names, versions, and dependencies. APT reads that index to plan a change, then uses dpkg to install or remove package files.

LayerWhat it providesWhat changes
RepositoryPackage records and downloadable archivesSource data maintained by the distribution or repository owner
APT indexLocal information about available package versions and dependenciesRefresh it with apt update
dpkgInstalled package files and local package stateAPT requests package changes through it

Check your distribution and permissions first

APT is the package manager on Debian-family distributions, not all Linux distributions. If you use Ubuntu, our Ubuntu version check helps identify the release before you follow release-specific instructions.

The version command reports the installed APT program, not the distribution release or the packages offered by configured repositories. It is a quick check that the apt executable is available.

apt --version
  • Use administrator privileges for commands that install, upgrade, remove, or purge system packages.
  • Searches and package-detail queries read the local index and do not change installed software.
  • Read the proposed transaction before confirming it, especially when APT can remove packages.

Refresh, install, and upgrade packages

The update action downloads package information from configured sources. It refreshes the local index, so it does not install newer versions of software already on your system.

sudo apt update

Install names the package you want APT to add, and APT calculates the required dependencies before it asks you to confirm. For example, this command requests Nano from the enabled repositories.

sudo apt install nano

Upgrade applies available versions of installed packages without removing installed packages. If an upgrade needs a removal, apt upgrade leaves that package back rather than removing another package to complete the change.

sudo apt upgrade

Unlike apt upgrade, full-upgrade may remove installed packages when APT needs removals to complete the dependency plan. Review those removals before accepting the transaction, as the apt manual distinguishes this behavior from upgrade.

sudo apt full-upgrade
CommandWhat it changesUse it when
apt updateRefreshes available package informationThe local package index needs new repository data
apt installAdds a package and any required dependenciesYou want a package from configured sources
apt upgradeUpgrades installed packages without removing installed packagesYou want eligible package updates applied
apt full-upgradeMay remove installed packages to complete upgradesYou accept the full transaction after reviewing its removals

APT can install a local .deb file and resolve dependencies through configured repositories. Ubuntu’s local-package instructions explain the syntax, and our Ubuntu DEB installation guide walks through the path and checks.

Find a package and inspect its candidate

Search looks through package information in the local index, so its results can include a package whose description matches your terms. The same search can show an installed marker, but use an installed-only query when you need a package inventory.

apt search '^apt$'
Terminal output from apt search showing the apt package in the package index
APT search returns package records from the local package index. The installed marker is shown only when it applies.

The example output marks apt as installed. Search results still depend on the local repository data, so an available match is not by itself proof that a package is installed.

apt show apt
apt policy apt
apt list --installed apt

The show action prints package details, policy separates installed and candidate versions, and list can filter for installed packages. I saw apt policy print the installed and candidate fields separately, so compare those fields before requesting a particular version.

For a full inventory rather than one package, follow our guide to listing installed packages with apt. The candidate version and its repository priority can explain why APT selects one version over another.

Remove a package without deleting the wrong data

Remove, purge, and autoremove do different jobs. The focused apt removal guide covers the full procedure, while this table shows what each action targets.

ActionEffectCheck before confirming
removeRemoves packaged software and usually leaves package-level configuration filesConfirm the package name and dependent packages
purgeRemoves the package and its package-level configuration filesIt does not remove personal files stored in your home directory
autoremoveProposes removing automatic dependencies that are no longer requiredKeep any package you still want, even if it was installed as a dependency
sudo apt remove nano
sudo apt purge nano
sudo apt autoremove

APT lists the packages it plans to remove and waits for confirmation before continuing. The first retained screenshot shows the confirmation prompt and a typed response, but it does not show a completed command.

APT confirmation prompt before removing Nano
APT asks for confirmation before removing Nano. The package details vary by system.

The typed y accepts the proposed transaction, but the prompt alone does not prove that removal finished. The next image shows APT reporting the removal action and its update to the editor alternative.

APT reports Nano removal and an editor alternative update
APT reports Nano removal and updates the editor alternative. Package versions and trigger messages vary by system.

The removal line confirms that APT started the action. Wait for the command to return to the shell, then check the installed-package list if you need confirmation.

When APT will not proceed

A stale index can hide a package available from an enabled source. Refresh the index, then verify the package name and source before changing installation methods.

sudo apt update

An upgrade that leaves packages back can reflect a dependency change that would require removing another installed package. Check held packages and inspect the candidate before choosing a broader transaction.

apt-mark showhold

A beginner on the Linux Mint forum asked, “I know APT Install but how am I suppose to know that “-f” is for dependences ?” The apt-get manual lists -f as shorthand for –fix-broken, which asks APT to repair an existing dependency problem and can propose package changes.

sudo apt-get --fix-broken install

Use apt for interactive work and apt-get in scripts

APT provides several command-line tools with overlapping jobs. The apt command uses defaults for interactive terminal use, while apt-get and apt-cache expose dedicated interfaces for package actions and metadata queries.

I chose apt-get and apt-cache for script examples because the Debian apt manual says they preserve backward compatibility as much as possible. The same manuals explain apt-get and apt-cache in more detail.

ToolUse it forWhat to remember
aptInteractive package managementDesigned for terminal users, with human-oriented defaults
apt-getPackage changes in scripts and automationIts command-line behavior aims for backward compatibility
apt-cacheRead-only package metadata queriesIt reads the local index, which can be stale

That warning is about scripts, not interactive apt use. Scripts should call dedicated tools because their behavior aims for backward compatibility across versions.

Inspect the planned change before accepting it

APT’s simulation mode calculates a package plan without applying changes and runs without the locks used for a live transaction. Treat that output as a preview, not a guarantee of what a later install will do.

apt-get --simulate install nano

Read the proposed packages and any removals before running the live command with administrator privileges. A package plan is useful only when you understand which installed software it will keep and which it will change.

Common questions about apt

Can apt install a local .deb file?

Yes. APT can install a local .deb package and resolve dependencies from the repositories configured on your system. Use the file path when the package is not in the current directory.

How do I open the full apt manual?

Run man apt to read the manual installed on your system. Our guide to reading Linux manual pages explains the pager controls.