apt Command in Linux

By 

Updated on

5 min read

Use apt Command in Ubuntu/Debian

apt 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 basically 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

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.

If you want to upgrade a single package, pass the package name:

Terminal
sudo apt 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.

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

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.

To list all available packages use the following command:

Terminal
sudo 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
sudo apt list | grep package_name

To list only the installed packages type:

Terminal
sudo apt list --installed

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

Terminal
sudo apt list --upgradeable

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

Terminal
sudo 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
sudo 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
sudo 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

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 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
sudo apt list --installedList installed packages
sudo apt search <pkg>Search for a package
sudo apt show <pkg>Show package details
sudo apt policy <pkg>Show available versions and sources
sudo apt cleanRemove all cached package files
sudo apt autocleanRemove outdated cached packages

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