Understanding APT, APT-Cache and Their Frequently Used Commands
If you’ve ever used Debian or a Debian based distribution 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 on 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, and 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. It 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 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 a number 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 laboured for hours on these tasks, this was one of those “sun parting the clouds” moments in our Linux lives!
Understanding APT Configuration
This first file we are going to look at is one of APT’s configuration files.
$ sudo cat /etc/apt/sources.list
deb http://us-west-2.ec2.archive.ubuntu.com/ubuntu/ precise main deb-src http://us-west-2.ec2.archive.ubuntu.com/ubuntu/ precise main deb http://us-west-2.ec2.archive.ubuntu.com/ubuntu/ precise-updates main deb-src http://us-west-2.ec2.archive.ubuntu.com/ubuntu/ precise-updates main deb http://us-west-2.ec2.archive.ubuntu.com/ubuntu/ precise universe deb-src http://us-west-2.ec2.archive.ubuntu.com/ubuntu/ precise universe deb http://us-west-2.ec2.archive.ubuntu.com/ubuntu/ precise-updates universe deb-src http://us-west-2.ec2.archive.ubuntu.com/ubuntu/ precise-updates universe deb http://security.ubuntu.com/ubuntu precise-security main deb-src http://security.ubuntu.com/ubuntu precise-security main deb http://security.ubuntu.com/ubuntu precise-security universe deb-src http://security.ubuntu.com/ubuntu precise-security universe
As you can probably deduce from my sources.list file, I’m using Ubuntu 12.04 (Precise Pangolin). I’m also using three repositories:
- Main Repository
- Universe Repository
- Ubuntu Security Repository
The syntax of this 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 server you quite well and you will never need to edit it at all.
However, there are times that you might want to add third-party repositories. You would simple enter them using the same format, and then run the update command:
$ sudo apt-get 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 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
blogilo - graphical blogging client drivel - Blogging client for the GNOME desktop drupal6-mod-views - views modules for Drupal 6 drupal6-thm-arthemia - arthemia theme for Drupal 6 gnome-blog - GNOME application to post to weblog entries lekhonee-gnome - desktop client for wordpress blogs libmarkdown-php - PHP library for rendering Markdown data qtm - Web-log interface program tomboy-blogposter - Tomboy add-in for posting notes to a blog wordpress - weblog manager wordpress-l10n - weblog manager - language files wordpress-openid - OpenID plugin for WordPress wordpress-shibboleth - Shibboleth plugin for WordPress wordpress-xrds-simple - XRDS-Simple plugin for WordPress zine - Python powered blog engine
What is APT-Cache?
Apt-cache is a command that simply queries the APT cache. We passed the search parameter to it, stating that, obviously, we want to search APT for it. As we can see above, searching for “wordpress” returned a number of 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
Versions: 3.3.1+dfsg-1 (/var/lib/apt/lists/us-west-2.ec2.archive.ubuntu.com_ubuntu_dists_precise_universe_binary-amd64_Packages) Description Language: File: /var/lib/apt/lists/us-west-2.ec2.archive.ubuntu.com_ubuntu_dists_precise_universe_binary-amd64_Packages MD5: 3558d680fa97c6a3f32c5c5e9f4a182a Description Language: en File: /var/lib/apt/lists/us-west-2.ec2.archive.ubuntu.com_ubuntu_dists_precise_universe_i18n_Translation-en MD5: 3558d680fa97c6a3f32c5c5e9f4a182a Reverse Depends: wordpress-xrds-simple,wordpress wordpress-shibboleth,wordpress 2.8 wordpress-openid,wordpress wordpress-l10n,wordpress 2.8.4-2 Dependencies: 3.3.1+dfsg-1 - libjs-cropper (2 1.2.1) libjs-prototype (2 1.7.0) libjs-scriptaculous (2 1.9.0) libphp-phpmailer (2 5.1) libphp-simplepie (2 1.2) libphp-snoopy (2 1.2.4) tinymce (2 22.214.171.124+dfsg0) apache2 (16 (null)) httpd (0 (null)) mysql-client (0 (null)) libapache2-mod-php5 (16 (null)) php5 (0 (null)) php5-mysql (0 (null)) php5-gd (0 (null)) mysql-server (2 5.0.15) wordpress-l10n (0 (null)) Provides: 3.3.1+dfsg-1 - Reverse Provides:
This shows us that wordpress 3.3.1 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-get install wordpress
That command will install WordPress-3.3.1 and all dependencies that are not currently installed.
Of course, that is not all you can do with APT. Some other useful commands are as follow:
NOTE: It is a good practice to run apt-get update before running any series of APT commands. Remember, apt-get 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-get remove wordpress
Unfortunately, the apt-get remove command leave all of the configuration files intact. To remove those as well, you’ll want to use apt-get purge:
$ sudo apt-get purge wordpress
Every now and then, you might run across a situation where there are broken dependencies. This usually happens when you don’t run apt-get update properly, mangling the database. Fortunately, APT has a fix for it:
$ sudo apt-get –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-get clean
This is just a small fraction of APT, APT-Cache and some of its useful commands. There are still lot to learn and explore some more advanced commands at below article.
As always, please have a look at the man pages for even more options. Once one gains a familiarity with APT, it is possible to write awesome Cron scripts to keep the system up to date.