Home Package management Mastering APT Linux Commands: A Comprehensive Guide With 15 Practical Examples

Mastering APT Linux Commands: A Comprehensive Guide With 15 Practical Examples

The Ultimate Guide to APT: The Command-Line Tool for Managing Software Packages on Debian and Ubuntu Linux.

By Karthick
Published: Updated: 1.9K views

This tutorial provides a concise explanation of the Apt package manager and presents a curated list of 15 useful Apt Linux command examples for efficient package management in Debian and its derivative distributions such as Ubuntu, Linux Mint, PoP!_OS, Elementary OS, Kubuntu, and more.

At the end of this guide, we will present you a handy APT Linux command cheat sheet, so you can keep it nearby and use it as quick reference to learn how to use the APT package manager efficiently.

What is APT in Linux?

APT, or Advanced Package Tool, is a free and open-source software package management system used to install, update, remove, and otherwise manage software packages on Debian and its derivative operating systems, such as Ubuntu and Linux Mint.

APT provides the same functionality as the specialized APT tools, like apt-get and apt-cache, but enables options more suitable for interactive use by default. Meaning - Apt can do everything that apt-get and apt-cache can do and Apt has some default options that are better suited for interactive use, such as the ability to show progress bars and the ability to ask for confirmation before making changes.

APT is the default package manager for Debian-based systems. APT can be used to manage a wide variety of software packages with ease.

Some of the key features of APT include:

  • Ease of use: APT is a relatively easy-to-use tool, even for beginners. The basic commands are straightforward and the syntax is clear.
  • Powerful: APT is a powerful tool that can be used to manage a wide range of software packages. It can be used to install, update, remove, and search for packages without having to worry about dependencies or conflicts.
  • Large number of packages: Apt connects to a centralized software repository that offers a vast array of packages. All the packages in the repository are regularly updated and maintained by the community, ensuring that you always have access to the latest versions of your favorite software.

APT Linux Command Examples

Heads Up: Some of the APT and its sub-commands requires root user or sudo privilege. All the commands that require elevated privilege are prefixed with sudo in this article.

1. Getting Help

To get help for using APT on a Debian-based Linux system, you can consult its help and manual page.

If you are not sure which command to use, you can always use the apt help or apt --help command to get a list of all available commands.

$ apt --help

This will display a brief summary of APT commands and their usage.

Getting APT Command Help
Getting APT Command Help

The "man" command displays the manual pages for various commands, including APT commands. To view the manual for "apt," use:

$ man apt

Similarly, you can use "man" with other APT commands like "apt-get," "apt-cache," etc.

2. Update APT Package Index

The 'apt update' is the first command you will run after installing a new operating system or whenever you want to install any new packages.

$ sudo apt update

The update command downloads the package metadata by connecting to the configured repositories. The repository information is configured either in /etc/apt/source.list file or under /etc/apt/sources.list.d directory.

Each line in the file points to a repository and it may vary with different distributions. For example, Linux Mint maintains its own repository and you can see it in the first line of the file. Other distributions like PoP!_OS also maintains their own repository. Since all these distributions are derived from Ubuntu you will see Ubuntu repositories present in the list.

When you run the 'apt update' command, it will connect to all these repositories and will update the metadata in the local cache and will show the list of packages that can be upgraded, packages that are no more essential, etc.

APT Update Package Index
APT Update Package Index

3. Search for Packages using APT Linux Command

Before installing any packages, you should make sure that the package is available in the configured repositories. You can use the 'apt search' command which accepts the package name as the input and does a regex search.

$ apt search <package-name>

Let’s search for a package named Kitty terminal emulator.

$ apt search kitty
Search for Packages using APT
Search for Packages using APT

If you look at the above output, it does a regex search for the word 'kitty' and returns a similar result.

Further, you can pipe the search output to the grep command to filter the exact match.

$ apt search kitty | grep -i -w kitty

Sample Output:

kitty/stable 0.26.5-5 amd64
kitty-doc/stable 0.26.5-5 all
kitty-shell-integration/stable 0.26.5-5 all
kitty-terminfo/stable 0.26.5-5 all

Heads Up: The 'apt search' command can be submitted without sudo or root privilege.

4. Find Detailed Information about a Package

To display the metadata of a package (package details) such as who is the author of the package, installation size, dependencies, source repository, etc by running any of the following commands. The following commands can be run without sudo or as the root user.

$ apt show kitty
$ apt show -a kitty
$ apt-cache show kitty
Find Detailed Information about a Package
Find Detailed Information about a Package

5. Install Packages using APT

You can install one or more packages by running the 'apt install' command and passing the package names as the arguments. Before running the install command run the 'apt update' command to refresh the package index i.e. refresh software source lists.

$ sudo apt install kitty

Let’s see what happens when you run this command.

  • First, it will check the repository and see if the package is available or not.
  • Second, it will build the dependency tree.
  • It will show you the list of additional packages that will be installed along with the kitty.
  • It will also show you the required disk size.
Install Packages using APT
Install Packages using APT

Finally, it will prompt you for confirmation. Type 'Y' and press ENTER to proceed with the installation. You can also directly pass the -y flag to skip the confirmation step.

$ sudo apt install kitty -y

You can also install multiple packages at once. Just specify the name of the packages with space-separated like below.

$ sudo apt install kitty tmux kitty gparted -y

If you try to install the package which is already installed, then it will just show you that the package is already installed.

Installing an Already Installed Package
Installing an Already Installed Package

Every package has versions tagged. If you wish to install a package with a specific version, simply provide the version number along with the package name like below.

$ sudo apt install kitty=0.21.2

You can get the available package versions using the following command:

$ apt list --all-versions kitty
Listing... Done
kitty/oldstable 0.19.3-1 amd64

6. Check the List of Installed Packages using APT

There are a couple of ways you can check the list of packages that are installed.

1. To list all installed packages, run:

$ apt list --installed

This command will display a list of all the packages that are currently installed on your system. It will show the package names along with their versions and architecture information.

List All Installed Packages
List All Installed Packages

2. You can also refine the results by filtering packages with names that contain a specific string. For instance, you can run the 'apt list' command along with 'grep' to filter packages containing 'kitty' in their name like below. Please note that this command does not require elevated privilege.

$ apt list --installed | grep -i kitty
Filter Installed Packages with Grep Command
Filter Installed Packages with Grep Command

This approach will help you pinpoint packages related to 'kitty' and streamline your search process effectively.

3. The another way to find the installed package is by using the apt-cache command.

$ apt-cache policy kitty
Display Installation Policy for a Package
Display Installation Policy for a Package

4. Every apt operation is recorded in the log file. You can check the log file to see what operations are performed for the given package.

$ tail -n 5 /var/log/apt/history.log

Sample Output:

Start-Date: 2023-08-03  17:46:14
Commandline: apt install kitty
Requested-By: ostechnix (1000)
Install: libb2-1:amd64 (0.98.1-1.1, automatic), kitty-doc:amd64 (0.26.5-5, automatic), librsync2:amd64 (2.3.2-1+b1, automatic), kitty:amd64 (0.26.5-5), libjs-jquery:amd64 (3.6.1+dfsg+~3.5.14-1, automatic), libjs-sphinxdoc:amd64 (5.3.0-4, automatic), libjs-underscore:amd64 (1.13.4~dfsg+~1.11.4-3, automatic), kitty-terminfo:amd64 (0.26.5-5, automatic), kitty-shell-integration:amd64 (0.26.5-5, automatic)
End-Date: 2023-08-03  17:46:17

Related Read: How To List Installed Packages In Linux

7. Reinstall a Package using APT

There may be cases where your package is broken or not functioning as expected. In this case, you can try reinstalling the package using the --reinstall flag.

$ sudo apt install kitty --reinstall
Reinstall a Package using APT
Reinstall a Package using APT

This command will just reinstall the given package but not the dependent packages. If you wish to go with a clean install run the following command.

$ sudo apt purge kitty && sudo apt autoremove && sudo apt install kitty -y

8. Get a List of Packages that can be Updated using APT

To keep the system safe and error-free, you always need to update your software and apply security patches. Unlike the Windows operating system, Linux distributions will not force you to install packages but doing it is essential.

When you run the 'apt update' command, you should see the number of packages that can be upgraded.

Number of Packages to Be Upgraded
Number of Packages to Be Upgraded

You can run the 'apt list --upgradable' command to get the list of packages that are candidates for update.

$ apt list --upgradable

Sample Output:

Listing... Done
base-files/stable 12.4+deb12u1 amd64 [upgradable from: 12.4]
cups-client/stable 2.4.2-3+deb12u1 amd64 [upgradable from: 2.4.2-3]
cups-common/stable 2.4.2-3+deb12u1 all [upgradable from: 2.4.2-3]
cups-core-drivers/stable 2.4.2-3+deb12u1 amd64 [upgradable from: 2.4.2-3]
cups-daemon/stable 2.4.2-3+deb12u1 amd64 [upgradable from: 2.4.2-3]
cups-ipp-utils/stable 2.4.2-3+deb12u1 amd64 [upgradable from: 2.4.2-3]
cups-ppdc/stable 2.4.2-3+deb12u1 amd64 [upgradable from: 2.4.2-3]
cups-server-common/stable 2.4.2-3+deb12u1 all [upgradable from: 2.4.2-3]
cups/stable 2.4.2-3+deb12u1 amd64 [upgradable from: 2.4.2-3]
dbus-bin/stable 1.14.8-2~deb12u1 amd64 [upgradable from: 1.14.6-1]
dbus-daemon/stable 1.14.8-2~deb12u1 amd64 [upgradable from: 1.14.6-1]
[..]

You can also check if updates are available for a single package by either piping the above output to the grep command or running the 'apt-cache policy' command.

Here I am looking for a timeshift utility update. The current version is 21.09.1-1 and the available version is 22.11.2+vera.

$ apt-cache policy timeshift

Sample Output:

timeshift:
  Installed: (none)
  Candidate: 22.11.2-1
  Version table:
     22.11.2-1 500
        500 https://deb.debian.org/debian bookworm/main amd64 Packages

9. Update a Package using APT

To update a single package to the latest version, run the 'apt install <package-name>' command. The 'apt install' command will install the given package if not already installed and will update the package if the latest version is found.

$ sudo apt install timeshift

10. Update all System Packages using APT

To update all the packages in the system, you can run the 'apt upgrade' or 'apt full-upgrade' or 'apt dist-upgrade' command.

$ sudo apt upgrade
$ sudo apt full-upgrade
$ sudo apt dist-upgrade

You will see the number of packages to be upgraded when you run any of the above commands and the size required in disk for the upgrade.

10.1. apt upgrade vs apt full-upgrade vs apt dist-upgrade

Although they all deal with package upgrades, there are some key differences in their behavior:

1. apt upgrade:

  • Purpose: The "apt upgrade" command is used to upgrade installed packages to their latest versions. However, it only upgrades packages if there are no changes in their dependencies that require new packages to be installed or existing packages to be removed.
  • Caution: This command is cautious and avoids making any major changes to the system that could cause disruptions or require additional package handling.

2. apt full-upgrade:

  • Purpose: The "apt full-upgrade" command is essentially an alias for "apt-get dist-upgrade." It performs the same function as "apt upgrade," but it is more aggressive in handling package changes. If necessary to upgrade the system as a whole, "apt full-upgrade" will remove currently installed packages and install new ones, effectively resolving more complex dependency issues.
  • Note: Despite the term "full," it does not imply a complete system upgrade to a new release, but rather a comprehensive package upgrade.

3. apt dist-upgrade:

  • Purpose: The "apt dist-upgrade" command is designed to handle major changes in package dependencies and upgrade to a new distribution release if available. It goes beyond the functionality of "apt upgrade" by intelligently handling changes in package dependencies and resolving conflicts, even if it means removing some packages or installing new ones.
  • Use Case: This command is particularly useful when you want to upgrade to a new version of the distribution, such as moving from Ubuntu 20.04 to 22.04.

In Summary, the key difference between these three commands is the upgrade command will only update the existing packages in the system and will not install or remove any packages as part of the update. But the full-upgrade and dist-upgrade command will install or remove any packages that are necessary as part of the upgrade process.

11. Hold Package Updates

To prevent automatic updates on packages with specific versions, use the "apt-mark" command to mark them as manually installed. For example, to hold the kitty package:

To hold the package:

$ sudo apt-mark hold kitty

To unhold the package:

$ sudo apt-mark unhold kitty

By doing this, you retain control over package updates, ensuring your desired versions remain intact.

You can view the list of held packages using command:

$ apt-mark showhold

12. Remove a Package using APT

Removing a package involves removing the package, dependent packages, and package configurations.

You can use 'apt remove' to remove packages. Here an important point to note is only the given package will be removed but the dependencies and other config files that are part of the package will not be removed.

$ sudo apt remove kitty

Take a look at the below output which shows kitty as the only package to be removed and not the dependent packages.

Remove a Package using APT
Remove a Package using APT

13. Purge Packages

The apt remove command removes the package but leaves its configuration files intact. If you want to remove a package including any configuration files associated with that package, you can use 'apt purge' command as shown below.

$ sudo apt purge kitty

14. Remove Unnecessary Packages

When you install a package using APT, it may bring in additional packages (dependencies) that are necessary for its proper functioning. However, as you use your system and install or remove other packages, some of these dependencies may become unnecessary, as they were only required by packages you no longer have installed. These unused dependencies can take up valuable disk space on your system.

The "apt autoremove" command helps clean up your system by identifying and removing these automatically installed packages that are no longer needed.

$ sudo apt autoremove

Heads Up: The "apt autoremove" command will only remove packages that were installed as dependencies and are no longer required by other packages. It will not remove packages that were explicitly installed by the user.

15. Clean APT Cache

APT caches package files (commonly with the .deb extension) that are downloaded during package installations, updates, or upgrades. The cached files in APT are stored in "/var/cache/apt/archives/" directory. They are used to speed up package installations, provide offline installation capability, handle dependencies efficiently, enable rollbacks, maintain package integrity, and save disk space and bandwidth.

$ ls -l /var/cache/apt/archives/
total 9976
-rw-r--r-- 1 root root  399336 Jun 22 01:18 bind9-dnsutils_1%3a9.18.16-1~deb12u1_amd64.deb
-rw-r--r-- 1 root root  301232 Jun 22 01:18 bind9-host_1%3a9.18.16-1~deb12u1_amd64.deb
-rw-r--r-- 1 root root 1410348 Jun 22 01:18 bind9-libs_1%3a9.18.16-1~deb12u1_amd64.deb
-rw-r--r-- 1 root root  886284 Jan 21  2023 fd-find_8.6.0-3_amd64.deb
-rw-r--r-- 1 root root  160188 Jul 14 22:55 libldb2_2%3a2.6.2+samba4.17.9+dfsg-0+deb12u3_amd64.deb
-rw-r--r-- 1 root root   81512 Jul 14 22:55 libsmbclient_2%3a4.17.9+dfsg-0+deb12u3_amd64.deb
-rw-r--r-- 1 root root   52264 Jul 14 22:55 libwbclient0_2%3a4.17.9+dfsg-0+deb12u3_amd64.deb
-rw-r--r-- 1 root root  759428 Jun 16 02:12 libx11-6_2%3a1.8.4-2+deb12u1_amd64.deb
-rw-r--r-- 1 root root  291808 Jun 16 02:07 libx11-data_2%3a1.8.4-2+deb12u1_all.deb
-rw-r--r-- 1 root root  192148 Jun 16 02:12 libx11-xcb1_2%3a1.8.4-2+deb12u1_amd64.deb
-rw-r----- 1 root root       0 Jun 10 10:43 lock
drwx------ 2 _apt root    4096 Aug  4 12:58 partial
-rw-r--r-- 1 root root 5655676 Jul 14 22:55 samba-libs_2%3a4.17.9+dfsg-0+deb12u3_amd64.deb

You can use "apt clean" or "apt autoclean" command to remove outdated or all cached files.

$ sudo apt clean
$ sudo apt autoclean

The main difference between "apt clean" and "apt autoclean" is that "apt clean" removes all cached package files, while "apt autoclean" only removes outdated cached package files, retaining the latest versions.

Running "apt autoremove" and "apt autoclean" periodically is a good practice to keep your system clean and free from unnecessary packages, thereby optimizing disk space usage.

Related Read: Nala – A Feature-rich Commandline Frontend For APT Package Manager

Linux Apt Command Cheat Sheet

Below is a tabular column listing all the essential APT commands. Print it out and keep it as a reference for easy access:

APT CommandDescription
apt updateUpdate the local package index
apt upgradeUpgrade all installed packages
apt full-upgradeupgrade the system by removing/installing/upgrading packages
apt install [package]Install a new package
apt remove [package]Remove a package
apt autoremoveRemove unused dependencies
apt purge [package]Remove a package along with its configuration
apt dist-upgradeUpgrade to a new distribution release
apt cleanRemove cached package files
apt autocleanRemove partially downloaded package files
apt-mark hold [package]Prevent a package from updating
apt-mark unhold [package]Unhold a package
apt-mark showholdShow the list of packages on hold
apt-cache search [keyword]Search for packages matching a keyword
apt-cache show [package]Display detailed information about a package
apt-cache depends [package]Show the dependencies of a package
apt-cache rdepends [package]Show reverse dependencies of a package
apt-cache policy [package]Display the installation policy for a package
apt-key add [keyfile]Add a GPG key from a key file
apt-key del [keyID]Remove a GPG key by its ID
apt-key listList all GPG keys
apt --helpDisplay help information for apt-get command
apt-cache --helpDisplay help information for apt-cache command
apt-key --helpDisplay help information for apt-key command
man aptOpen the manual page for apt-get command
man apt-cacheOpen the manual page for apt-cache command
man apt-keyOpen the manual page for apt-key command
Apt Command Cheat sheet

Heads Up: In the "Description" column, I've provided a brief description of what each command does. Make sure to replace "[package]" and "[keyword]" with actual package names and keywords, respectively, when using the commands.

Keep this handy reference nearby to quickly access and use these essential APT commands for efficient package management on your Debian-based Linux system.

Frequently Asked Questions (FAQ)

Q: What is APT in Linux?

A: APT (stands for Advanced Package Tool) is a package management system used to install, update, upgrade, search and remove packages in Debian-based Linux distributions.

Q: How does apt differ from apt-get and apt-cache?

A: APT provides a more user-friendly interface and combines functionalities of apt-get and apt-cache. While apt-get and apt-cache are still available for compatibility, APT offers more intuitive and interactive commands for managing software packages.

Q: Which Linux distributions use APT as the default package manager

A: APT is primarily used in Debian and its derivatives, including popular Linux distributions like Ubuntu, Linux Mint, Kubuntu, Xubuntu, Lubuntu, elementary OS, Pop!_OS and many others.

Q: How can I install a new package using APT?

A: To install a package, use the following command: sudo apt install package_name. Replace "package_name" with the name of the package you want to install.

Q: Can I search for packages with specific keywords using APT?

A: Yes, you can search for packages using: apt-cache search keyword

Q: How do I update the package list and upgrade installed packages using APT?

A: To update the package list, run: sudo apt update
To upgrade installed packages, use: sudo apt upgrade

Q: What's the difference between "apt upgrade," "apt full-upgrade," and "apt dist-upgrade"?

A: "apt upgrade" upgrades packages without handling complex dependency changes. "apt full-upgrade" (or "apt-get dist-upgrade") handles package changes more aggressively, and "apt dist-upgrade" can also perform distribution upgrades.

Q: How do I hold a package from automatic updates using apt-mark?

A: To hold a package, use the following command: sudo apt-mark hold package_name

Q: How to unhold a package?

A: If you decide to update a held package, you can unhold it using the following command:
sudo apt-mark unhold package_name
This will allow the package to be updated in future system-wide updates.

Q: What command can you use to check if a package is held?

A: You can check if a package is held using: apt-mark showhold

Q: Can you hold and unhold packages managed by other package managers like Snap or Flatpak using "apt-mark"?

A: No, the "apt-mark" command is specific to APT and applies only to packages managed by APT. Other package managers have their own mechanisms for version locking.

Q: How can I remove a package using APT?

A: To remove a package, use: sudo apt remove package_name
This will remove the package but keep its configuration files. Use apt purge to remove the package along with its configurations.

Q: What is "apt autoremove," and when should I use it?

A: The "apt autoremove" command removes unused packages that were automatically installed as dependencies but are no longer needed. Use it to free up disk space and keep your system clean.

Q: Is it safe to clean the APT cache?

A: Yes, cleaning the APT cache with apt clean or apt autoclean is safe. It removes cached package files that can be re-downloaded if needed.

Conclusion

In this article, we discussed some of the most common APT Linux commands with examples. We also covered the differences between apt upgrade, apt full-upgrade, and apt dist-upgrade. At the end of the guide, we have included a handy APT Linux command cheat sheet and a list of frequently asked questions (FAQs).

We hope this article has given you a better understanding of how to use APT to manage your software packages in Debian-based systems.

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More