8.8. Avoid Dependency Hell with apt
RPM and DEB packages include all of the dependency information that you need. Each package includes headers that list any other RPMs that are required to make it work. I've illustrated one of the problems you might encounter at the start of the previous annoyance.
While the use of yum is limited to RPM-based distributions, apt can be used on distributions based on both the DEB and RPM package-management systems. There are even a number of apt-capable repositories available for Fedora Core and some of the RHEL rebuild distributions.
But to optimize how apt works on your system, you should customize the related /etc/apt/sources.list configuration file, understand different apt commands, and configure a nightly update. For the best way to configure /etc/apt/sources.list, read the "Find the Right Update Repository" annoyance earlier in this chapter. You can mirror your own apt repository based on the related annoyance in Chapter 11.
8.8.1. The apt Commands
This section provides the briefest of introductions to the apt commands. They vary somewhat for DEB- and RPM-based distributions. For more information, see the Debian version of this HOWTO at http://www.debian.org/doc/manuals/apt-howto/index.en.html and the apt+rpm HOWTO at http://www.ccl.net/cca/software/UNIX/updating-redhat/apt-howto/.
The apt commands, in my opinion, go into much more depth than yum. However, the Fedora and yum developers are working rapidly to close this gap.
With apt-cache, you can search through the repositories that you've configured. A few of the key versions of this command include:
Perhaps the key command for handling apt repositories is apt-get. Once you've configured your repositories, the next thing you should do is run the following command, which synchronizes your cache:
If you see an error from this command, run it again. It may take a couple of cycles to synchronize the cache. You can then run the commands shown in this annoyance and more:
8.8.2. Configuring a Nightly apt Update
You can configure your own cron job for apt updates. If you've read the previous annoyance, you know that there is a standard cron job for yum updates. You can adapt this job for apt.
First, you need to make sure no other script is running dpkg at the same time. That means you need to know the associated lock file that apt creates for your distribution when it is running. For example, when apt is trying to install a package on Debian Linux, it's actually trying to run the dpkg command. In this case, the /var/lib/dpkg/lock file stops others from running dpkg (or apt-get install package, which invokes dpkg) at the same time.
Thus, when you create a cron job for apt, you'll want to make sure there is no current dpkg lock file. The following wrapper in bash makes sure none of the commands within it run unless nobody else is running dpkg on this computer:
if [ -f /var/lib/dpkg/lock ]; then ... fi
Within this stanza, you can add the commands you need. As described earlier, it's important to make sure that the current package cache is up-to-date with the following command:
You may want to repeat this command in the cron job, to make sure there are no problems with the update.
Now you can update your system. If you're satisfied with your update repositories and want to download and install all available updates, you could run the following command, which runs in "quiet" mode (-q) and responds to prompts with a yes (-y):
/usr/bin/apt-get -q -y dist-upgrade
Putting it all together, you'll have the following cron job:
#!/bin/sh if [ -f /var/lib/dpkg/lock ]; then /usr/bin/apt-get update /usr/bin/apt-get update /usr/bin/apt-get -q -y dist-upgrade fi
If you're concerned about specific packages such as the kernel, read about configuring the preferences file in the /etc/apt directory in the following section.
18.104.22.168. Keeping packages in apt
Naturally, you may not want to accept all updates. To keep apt from upgrading everything, you can specify the packages you want to keep in the preferences file in the /etc/apt directory.
The format of /etc/apt/preferences is straightforward. For each package, you'll need three entries: Package, Pin, and Pin-Priority. For example, if you want to make sure that the rsync package is not upgraded, add the following to /etc/apt/preferences:
Package: rsync Pin: version 2.6.3-2 Pin-Priority: 1001
You can make sure apt is aware of your restriction in the policy associated with this package, specifically with the following command:
apt-cache policy rsync
You can do more with the preferences file; details are beyond the scope of this annoyance. For more information, see the apt HOWTO at http://www.debian.org/doc/manuals/apt-howto/ch-apt-get.en.html.