apt Command in Linux

By 

Updated on

10 min read

Use apt Command in Ubuntu/Debian

apt (short for Advanced Package Tool) is a command-line utility for installing, updating, removing, and otherwise managing deb packages on Ubuntu, Debian, and related Linux distributions. It combines the most frequently used commands from the apt-get and apt-cache tools with different default values of some options.

apt is designed for interactive use. Prefer using apt-get and apt-cache in your shell scripts as they are backward compatible between the different versions and have more options and features.

Most of the apt commands must be run as a user with sudo privileges.

This guide explains the most common apt commands for day-to-day package management.

Updating Package Index (apt update)

The APT package index is a database that holds records of available packages from the repositories enabled in your system.

To update the package index, run the command below. This will pull the latest changes from the APT repositories:

Terminal
sudo apt update

Always update the package index before upgrading or installing new packages.

Installing Packages (apt install)

Installing packages is as simple as running the following command:

Terminal
sudo apt install package_name

If you want to install multiple packages with one command, specify them as a space-separated list:

Terminal
sudo apt install package1 package2

To install local deb files provide the full path to the file. Otherwise, the command will try to retrieve and install the package from the APT repositories.

Terminal
sudo apt install /full/path/file.deb

To install a specific version of a package, append the version string to the package name. Run apt policy package_name first to see which versions the enabled repositories offer:

Terminal
sudo apt install package_name=version

When the same package is available from more than one release, you can append the release name to select that version. For example, this command installs a package from Debian backports while preferring dependencies from the default release:

Terminal
sudo apt install package_name/trixie-backports

The -t option (long form --target-release) sets a target release for the entire transaction. This can pull additional dependencies from backports, so use it when the selected package requires them:

Terminal
sudo apt install -t trixie-backports package_name

Upgrading Packages (apt upgrade)

Regularly updating your Linux system is one of the most important aspects of overall system security.

To upgrade the installed packages to their latest versions run:

Terminal
sudo apt upgrade

The command does not upgrade packages that require removal of installed packages.

To upgrade one already-installed package without installing it if it is absent, use install with --only-upgrade:

Terminal
sudo apt install --only-upgrade package_name

It is always a good idea to configure automatic security updates .

Full Upgrading (apt full-upgrade)

The difference between upgrade and full-upgrade is that the latter will remove installed packages if that is needed to upgrade the whole system.

Terminal
sudo apt full-upgrade

Be extra careful when using this command.

Skipping the Confirmation Prompt

Before it changes anything, apt shows what it plans to do and waits for you to type Y. The -y option (long form --yes) answers that prompt in advance. Use it when you already know and trust the proposed transaction:

Terminal
sudo apt install -y package_name

The same option works with the upgrade commands:

Terminal
sudo apt upgrade -y
Terminal
sudo apt full-upgrade -y

A common one-liner refreshes the package index and upgrades everything in a single step:

Terminal
sudo apt update && sudo apt upgrade -y

The -y in that line applies only to the second command. Although apt update does not normally prompt, it can request confirmation when repository release information changes, so the one-liner is not guaranteed to run unattended. Review the proposed changes before using -y, even with install, because resolving package conflicts can remove installed packages. For scripts and Dockerfiles, use apt-get rather than apt, as its output format stays stable across versions.

Reinstalling a Package (apt install –reinstall)

If a package is broken or you want to restore its files, reinstall it with:

Terminal
sudo apt install --reinstall package_name

Fixing Broken Dependencies

When an installation is interrupted, or a package is installed with dpkg, which does not resolve dependencies on its own, you can end up with packages that are unpacked but not configured. In that state apt refuses to continue and points you at the fix:

output
You might want to run 'apt --fix-broken install' to correct these.
The following packages have unmet dependencies:

Run the command with no package name to install whatever is missing and finish the configuration:

Terminal
sudo apt install -f

The -f option is short for --fix-broken, so sudo apt --fix-broken install does the same thing. This is also the standard follow-up after installing a deb file with dpkg -i.

Removing Packages (apt remove)

To remove an installed package type the following:

Terminal
sudo apt remove package_name

You can also specify multiple packages, separated by spaces:

Terminal
sudo apt remove package1 package2

The remove command will uninstall the given packages, but it may leave some configuration files behind. If you want to remove the package including all configuration files, use purge instead of remove:

Terminal
sudo apt purge package_name

Remove Unused Packages (apt autoremove)

Whenever a new package that depends on other packages is installed on the system, the package dependencies will be installed too. When the package is removed, the dependencies will stay on the system. These leftover packages are no longer used by anything else and can be removed.

To remove the unneeded dependencies use the following command:

Terminal
sudo apt autoremove

To remove unused packages and their configuration files, run:

Terminal
sudo apt autoremove --purge

Listing Packages (apt list)

The list command allows you to list the available, installed, and upgradable packages. Like search, show, and policy, it only reads package data that is already on disk, so it does not need sudo.

To list all available packages use the following command:

Terminal
apt list

The command will print a list of all packages, including information about the versions and architecture of the package. To find out whether a specific package is installed, you can filter the output with the grep command.

Terminal
apt list | grep package_name

To list only the installed packages type:

Terminal
apt list --installed

Getting a list of the upgradable packages may be useful before actually upgrading the packages:

Terminal
apt list --upgradable

This command allows you to search for a given package in the list of the available packages:

Terminal
apt search package_name

If found, the command will return the packages whose name matches the search term.

Package Information (apt show)

The information about the package dependencies, installation size, the package source, and so on might be useful before removing or installing a new package.

To retrieve information about a given package, use the show command:

Terminal
apt show package_name

Package Policy (apt policy)

Use apt policy to see which version will be installed and which repositories it comes from:

Terminal
apt policy package_name

This is helpful when troubleshooting version conflicts or pinned packages.

Cleaning Package Cache (apt clean / apt autoclean)

To remove cached package files and free disk space, use:

Terminal
sudo apt clean

To remove only outdated cached packages, use:

Terminal
sudo apt autoclean

Troubleshooting

E: Could not get lock /var/lib/dpkg/lock-frontend
Another process is already using the package system, usually unattended-upgrades or a package manager left open in another window. Wait for it to finish and run the command again. If the process appears frozen, identify it with sudo lsof /var/lib/dpkg/lock-frontend, terminate it gracefully, and run sudo dpkg --configure --pending to finish any interrupted configuration. Never delete dpkg lock files by hand, as doing so can corrupt the package database or filesystem.

E: Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied)
The command changes the system and needs root privileges, so prefix it with sudo. Read-only commands such as apt list and apt search do not need it.

E: Unable to locate package
Either the package index is out of date or the package is not in the repositories you have enabled. Run sudo apt update first. If the package is still not found, it may live in a repository that is not enabled on your system, such as universe on Ubuntu, or in a third-party repository you have to add yourself.

W: GPG error and NO_PUBKEY
The signing key for one of your repositories is missing or expired, so apt will not trust its package list. Download the current key from the repository provider, store it under /etc/apt/keyrings, and point the repository entry at that file with the signed-by option.

The following packages have been kept back
On Ubuntu, this often means that an update is being released gradually through phased updates. Run apt policy package_name and look for a phased percentage. If the package is phased, wait and APT will install it when the update reaches your system. Packages can also be kept back because of dependency changes or package holds. Do not jump directly to full-upgrade; use it only when the proposed solution genuinely requires removals, and read the removal list before confirming.

Quick Reference

For a printable quick reference, see the APT cheatsheet .

CommandDescription
sudo apt updateRefresh the package index
sudo apt install <pkg>Install a package
sudo apt install -y <pkg>Install without the confirmation prompt
sudo apt install <pkg>=<version>Install a specific version
sudo apt install -fFix broken dependencies
sudo apt upgradeUpgrade all installed packages
sudo apt full-upgradeUpgrade, removing packages if needed
sudo apt remove <pkg>Remove a package (keep config files)
sudo apt purge <pkg>Remove a package and its config files
sudo apt autoremoveRemove unused dependency packages
apt list --installedList installed packages
apt list --upgradableList packages with pending upgrades
apt search <pkg>Search for a package
apt show <pkg>Show package details
apt policy <pkg>Show available versions and sources
sudo apt cleanRemove all cached package files
sudo apt autocleanRemove outdated cached packages

FAQ

What does apt stand for?
APT is short for Advanced Package Tool, the package management system used by Debian and the distributions built on it. The apt command is the front end you type at the terminal, while apt-get, apt-cache, and apt-mark are separate tools that belong to the same system.

What does sudo apt do?
sudo runs the command that follows it with root privileges, and apt is the package manager. Every apt command that changes the system, such as install, remove, upgrade, or update, needs those privileges, which is why most of the examples above start with sudo.

Do apt search, apt list, and apt show require sudo?
No. Those commands only read package data that is already on your disk, so any user can run them. Adding sudo does not change what they return.

Is it safe to use apt install -y?
It is safe when you know what changes APT will make. The -y option skips the normal confirmation, and even installing a named package can remove conflicting packages. Run the command without -y first when you are unsure, then review the package list before confirming or adding the option to controlled automation.

What is the difference between apt update and apt upgrade?
apt update refreshes the local record of which packages and versions are available and installs nothing. apt upgrade installs the newer versions that the refreshed record revealed. Run update first, otherwise upgrade works from stale information.

Conclusion

For day-to-day package management, sudo apt update followed by sudo apt upgrade covers most of what you need. For a comparison of when to use apt versus apt-get in scripts, see apt vs apt-get: What Is the Difference? .

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page