Understanding APT, APT-Cache and Their Frequently Used Commands

If you’ve ever used Debian or Debian-based distributions like Ubuntu or Linux Mint, then chances are that you’ve used the apt package system to install or remove software.

Even if you’ve never dabbled in the command line, the underlying system that powers your package manager GUI is the APT system.

Today, we are going to take a look at some familiar commands dive into some less or more frequently used apt commands, and shed some light on this brilliantly designed system.

What is APT?

APT stands for Advanced Package Tool, which was first seen in Debian 2.1 back in 1999. Essentially, APT is a management system for dpkg packages, as seen with the extension *.deb.

It was designed to not only manage packages and updates but also to solve the many dependency issues when installing certain packages.

As anyone who was using Linux back in those pioneer days, we were all too familiar with the term “dependency hell” when trying to compile something from source, or even when dealing with many of Red Hat’s individual RPM files.

APT solved all of these dependency issues automatically, making installing any package, regardless of the size or number of dependencies a one-line command.

To those of us who labored for hours on these tasks, this was one of those “sun parting the clouds” moments in our Linux lives!

Understanding APT Configuration

The /etc/apt/sources.list file is a configuration file that specifies the repositories from which the package manager (apt) retrieves and installs software packages. This file contains a list of URLs (Uniform Resource Locators) for the repositories, along with details like distribution codename and components.

Here’s an example of what an /etc/apt/sources.list file might look like:

sudo cat /etc/apt/sources.list
Listing /etc/apt/sources.list File
Listing /etc/apt/sources.list File

As you can probably deduce from my /etc/apt/sources.list file, I’m using Ubuntu 22.04 (ammy Jellyfish). I’m also using three repositories:

  • Main Repository
  • Universe Repository
  • Ubuntu Security Repository

The syntax of /etc/apt/sources.list file is relatively simple:

deb (url) release repository

The accompanying line is the source file repository. It follows a similar format:

deb-src (url) release repository

This file is pretty much the only thing you’ll ever have to edit using APT, and chances are that the defaults will serve you quite well and you will never need to edit it at all.

However, there are times when you might want to add third-party repositories. You would simply enter them using the same format, and then run the update command:

$ sudo apt update
NOTE: Be very mindful of adding third-party repositories!!! Only add from trusted and reputable sources. Adding dodgy repositories or mixing releases can seriously mess up your system!

We’ve taken a look at our /etc/apt/sources.list file and now know how to update it, so what’s next? Let’s install some packages. Let’s say that we are running a server and we want to install WordPress.

First, let’s search for the package:

sudo apt-cache search wordpress
Search for Package
Search for Package

What is APT-Cache?

Apt-Cache is a command that simply queries the apt-cache. We passed the search parameter to it, stating that we want to search APT for it. As we can see above, searching for “wordpress” returned several packages that related to the search string with a short description of each package.

From this, we see the main package of “wordpress – weblog manager,” and we want to install it. But wouldn’t it be nice to see exactly what dependencies are going to be installed along with it? apt can tell us that as well:

sudo apt-cache showpkg wordpress
List Package Dependencies
List Package Dependencies

This shows us that wordpress 5.8.3 is the version to be installed, the repository it is to be installed from, reverse dependencies, and other packages it depends on, plus their version numbers.

NOTE: (null means that the version is not defined, and the latest version in the repository will be installed.)

Now, the actual install command:

sudo apt install wordpress

That command will install WordPress-5.8.3 and all dependencies that are not currently installed.

Install Package with APT
Install Package with APT

Of course, that is not all you can do with APT. Some other useful apt commands are as follows:

NOTE: It is a good practice to run an apt update before running any series of apt commands. Remember, apt update parses your /etc/apt/sources.list file and updates its database.

Uninstalling a package is just as easy as installing the package:

sudo apt remove wordpress

Unfortunately, the apt remove command leaves all of the configuration files intact. To remove those as well, you’ll want to use apt purge:

sudo apt purge wordpress

Now and then, you might run across a situation where there are broken dependencies. This usually happens when you don’t run the apt update properly, mangling the database. Fortunately, apt is a fix for it:

sudo apt –f install

Since apt downloads all of the *.deb files from the repository right to your machine (stores them in /var/cache/apt/archives) you might want to periodically remove them to free up disk space:

sudo apt clean

This is just a small fraction of apt, apt-cache, and some of its useful commands. There is still a lot to learn and explore some more advanced commands in the below article.

As always, please have a look at the man pages for even more options. Once one gains familiarity with apt, it is possible to write awesome Cron scripts to keep the system up to date.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Rob Krul
Rob is an avid user of Linux and Open Source Software, with over 15 years experience in the tech geek universe. Aside from experimenting with the many flavors of Linux, he enjoys working with BSDs, Solaris, and OS X. He currently works as an Independent IT Contractor.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

2 Comments

Leave a Reply
  1. For most of my software manipulation, I use Synaptic. It avoids many of the pitfalls you mention and it is a GUI front end for apt-get, apt-cache, and apt commands.

    “Only add from trusted and reputable sources.”
    Very sage advice except that sites do not come pre-labeled as “trusted and reputable” “dodgy” or “shady”.

    BTW – who vets PPAs whether they are to be trusted or whether they are dodgy?

    Reply
  2. If you require the patches to be applied on the kernel source, use the ‘patch’ command. If you do a ‘man patch’ you can understand how to use the patch command.

    Once you apply the patch successfully, do the following:

    (1) Re-configure the kernel (if necessary – This will be dependent on what patches you had applied to the kernel)
    (2) If (1) was successful, Re-compile the kernel (by following the same procedure that you had used earlier for the original compilation)
    (3) If (2) was successful, Install the kernel again by doing the ‘make install’ command
    (4) If (3) was successful, Re-boot and boot it up with the newly compiled kernel and see if the applied patches are getting executed correctly.

    Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.