Tutorial: Mastering APT

We already peeked at APT’s history on our first Linux Inside and took a tour on Netrunner’s default suite of APT’s front ends on Visual Guide: Muon. Today we want to take a look at the back end itself. Using the command line has many advantages,  it’s faster once you the hang of it.

The first thing to know is APT needs superuser permissions for some actions, this is a security measure, so before executing some APT command you usually need to invoke

sudo

Using sudo is really easy, you just put it before any command [app] you want to give superusers power to. There are two main different types of commands apt-get and apt-cache. The former is mostly for installing, upgrading, removing packages and updating your repositories. The later is mostly to search packages, descriptions, dependencies.

Let’s start with apt-cache, the syntax is the following

apt-cache [commands] package

The most common commands are:

  • unmet – Show unmet dependencies.
  • search – Search packages available on the repositories.
  • show – Shows information about a package
  • depends – Shows the dependencies of a package
  • rdepends – Show reverse dependencies of  a package

Command search is the most used feature of apt-cache, for example:

sudo apt-cache search web browser

From what I’ve seen people rarely use apt-cache much. Which I think is a mere consequence of how good search is on front ends, there’re as fast, easier to read, show screenshots, and it’s just an overall better experience. Not so with apt-get, which is used everywhere, its forums and web sites favorite form to tell someone how to install an app.

Let’s see its syntax:

sudo apt-get install [options] [commands] packages

Options (which I omitted for apt-cache) are very useful, here are some of them:

  • -d – Download only – do NOT install or unpack archives
  • -y – Assume Yes to all queries and do not prompt
  • -f – Attempt to correct a system with broken dependencies in place
  • -u – Show a list of upgraded packages as well
  • -b – Build the source package after fetching it
Most of the options are self-explanatory, we we will deal with the ones that aren’t in a moment.  All commands for apt-get are really useful, so here’s the full list:
  • update – Retrieve new lists of packages
  • upgrade – Perform an upgrade
  • install – Install new packages
  • remove – Remove packages
  • autoremove – Remove automatically all unused packages
  • purge – Remove packages and config files
  • source – Download source archives
  • build-dep – Configure build-dependencies for source packages
  • dist-upgrade – Distribution upgrade
  • dselect-upgrade – Follow dselect selections
  • clean – Erase downloaded archive files
  • autoclean – Erase old downloaded archive files
  • check – Verify that there are no broken dependencies
  • changelog – Download and display the changelog for the given package
  • download – Download the binary package into the current directory

Reader of our last couple of articles probably recognize most of the options, as front ends are just a face for them. According to our syntax, to install a package you just need to run

sudo apt-get install lyx

APT by default will ask you to confirm:

Need to get 453 MB of archives.
After this operation, 784 MB of additional disk space will be used.
Do you want to continue [Y/n]?

If you’re sure that what you wrote is right you can bypass this by combining apt-get install with the options -y:

sudo apt-get -y install lyx

One thing to keep in mind is that you can install, remove, purge multiple packages with one command by separating packages with spaces, as in

sudo apt-get install chromium lyx arora

Users new to Linux usually feel threaten by the idea of compiling their own software, these days most won’t ever need to compile anything, but if they want to APT can help make the process less painful, say you want to compile certain app, you can download and build its source code using only APT

sudo apt-get -b source kate

But let say the version you want to install isn’t available in your repositories and you don’t want to suffer a dependency hell, you can ask APT to build all dependencies for a specific package in the following way

sudo apt-get buid-dep kate

Adding repositories

It’s possible to add repositories using apt, in the past, you had to modify a list manually (/etc/apt/sources.list) or use a front end. Doing so is very straight forward

sudo add-apt-repository [deb line]

A deb line is composed in the following way: deb/deb-src url version section. For example: deb http://archive.canonical.com/ubuntu/ oneiric partner. Usually web pages will offer a complete deb line, so users don’t need to worry much about it. If available (and if we don’t want to get warned about unsigned packages often) we can add its key

sudo apt-key add [key location]

Repositories on the Personal Packages Archives for Ubuntu, better known as PPA, are even simpler to add, thanks to something known as ppa line:

sudo add-apt-repository ppa:[name/section]

For example, adding chromium’s daily repository is done by running

sudo add-apt-repository ppa:chromium-daily/beta

This ppa line is available for all repositories from Ubuntu’s PPA, another advantage is that you don’t need to manually add the gpg key.

Solving common problems

If your system has broken dependencies some front ends refuse to open and you can’t install or remove any app as long as you have such packages, to fix this, more often than not, running

sudo apt-get -f install

will do the trick.

Another common issue is

Could not get lock /var/lib/dpkg/lock – open (11: Resource temporarily unavailable)
E: Unable to lock the administration directory (/var/lib/dpkg/), is another process using it?

This happens because more than one app is accessing the package manager, the reason why only one at a time can access it is because otherwise there could be conflicts between different actions by different applications. The first thing to do is checking if you don’t have another package manager open, like Synaptic. If you don’t, or closed them, and the error persist, you may want to check the running process, you can do this in Menu (run) > System > System Monitor, there search for apt, this means that apt is doing something on the background, you should be careful, interfering with this process if it’s doing something important can be potentially disastrous. If you recall running an APT command and closing the console try to remember what you were doing, to know if it’s safe to kill this process. If you don’t recall it, perhaps it would be better to try again later.

If you already check, and there isn’t another package manager open, and apt isn’t running, and dpkg is still locked (sometimes this endures even reboots), then you need to get rid of this lock manually, this is rather easy:

sudo rm -f /var/lib/dpkg/lock

Another relatively common error is

E: dpkg was interrupted

This problem caused because something got in the way of dpkg (a blackout is a common cause) while it was still installing/removing packages is easily solved by running

sudo dpkg –configure -a

WordPress automatically transforms double – into –, so it is  – -configure (no spaces) instead of –configure.

A, thankfully, not so common problem is when a package needs to be re-configured (usually because an upgrade messed something up), this can be extremely problematic, specially if the package is an essential part of the operating system, like the X Server. You can accomplish this by running:

sudo dpkg-reconfigure [package]

Depending on the package, you may be asked configuration questions.

And that covers all important topics. This should help new users see how the command line can be easy to use too, and how efficient and fast it can be, faster than front ends. I would like to ask readers if they think any important thing is missing from this article, and if so, what? We may update it to include it.

[sharedaddy]

2 thoughts on “Tutorial: Mastering APT

  1. Great article dude! And great articles in general! You can also add “dpkg -i package” ,also doesn’t “apt-get build-dep” installs package deps rather than build them? Anyway I am not very advance user so maybe I wrong about that.

  2. Pingback: Plasma just hit a “Homerun” | Netrunner Magazine

Leave a Reply