Debian Package Management
Debian Package Management
Ian Shields
Senior Programmer
IBM
11 May 2010
Learn how to install, upgrade, and manage packages on your Linux® system. This
article focuses on the Advanced Packaging Tool, or APT, which is the package
management system used by Debian and distributions derived from Debian, such as
Ubuntu. You can use the material in this article to study for the LPI 101 exam for
Linux system administrator certification, or just to explore the best ways to add new
software and keep your system current.
Overview
In this article, learn to use the Debian package management tools to manage the
packages on your Linux system. Learn to:
Prerequisites
To get the most from the articles in this series, you should have a basic knowledge
of Linux and a working Linux system on which you can practice the commands
covered in this article. Sometimes different versions of a program will format output
differently, so your results may not always look exactly like the listings and figures
shown here. In particular, much of the output we show is highly dependent on the
packages that are already installed on our systems. Your own output may be quite
different, although you should be able to recognize the important commonalities.
In the past, many Linux programs were distributed as source code, which a user
would build into the required program or set of programs, along with the required
man pages, configuration files, and so on. Nowadays, most Linux distributors use
prebuilt programs or sets of programs called packages, which ship ready for
installation on that distribution. In this article, you will learn about package
management tools that help you install, update, and remove packages. This article
focuses on Advanced Packaging Tool, or APT, the package management system
used by Debian and distributions derived from Debian, such as Ubuntu. Another
article in this series, "Learn Linux 101: RPM and YUM package management,"
covers the Red Hat package management tools.
although we mention some of the other tools so you can pursue them further.
APT, RPM, and YUM (the latter two are package management tools for Red Hat
systems) have many similarities. All can install and remove packages. Information
about installed packages is kept in a database. All have basic command-line
functionality, while additional tools can provide more user-friendly interfaces. All can
retrieve packages from the Internet.
When you install a Linux system, you typically install a large selection of packages.
The set may be customized to the intended use of the system, such as a server,
desktop, or developer workstation. And at some time, you will probably need to
install new packages for added functionality, update the packages you have, or even
remove packages that you no longer need or that have been made obsolete by
newer packages. Let's look at how you do these tasks, and at some of the related
challenges such as finding which package might contain a particular command.
If you did not get the helpful suggestion from the second form of output in Listing 1,
you might check back with your colleague to find out which package to install.
Otherwise, you might just guess that the gcl command is in the gcl package. This is
often a good guess, but not always the right one. We'll see later how to find the right
package. In this case, you need the gcl package, and you install it using the
apt-get command with the install option as shown in Listing 2. Note that
apt-get will determine which extra packages you need to satisfy dependencies
and will then give you a list of all the packages that will be installed. At that point,
you are prompted to continue. In our example, we respond y to install gcl and the
additional required package, libreadline5.
From the output in Listing 2, you see that apt-get has read a package list from
somewhere (more on that shortly), built a dependency tree, and determined that
libreadline5 is a required prerequisite that is not currently installed. You will also
notice the suggestion to install the separate package for the documentation, gcl-doc.
After some additional summary information, including space usage, you are
prompted to continue, and gcl is installed along with the prerequisite package.
Debian packages usually have an extension of .deb, and you see that the packages
are downloaded and unpacked as shown in the line:
Not surprisingly, the documentation does not have any prerequisite packages.
Package locations
In the previous section, you learned how to install a Debian package. But where do
the packages come from? How does apt-get know where to download packages
from? We mentioned that apt-get read a package list from somewhere. The
starting point for that somewhere is /etc/apt/sources.list. The list tells apt-get
where to look for packages, including from a CD-ROM, from your local file system,
or over a network using http or ftp. You can add additional sources in the
/etc/apt/sources.list.d directory.
Listing 4 shows the first few lines of /etc/apt/sources.list on my system. Note that the
distribution CD on the first line is commented out (# in position 1). If you need to
install a lot of new packages that may not have been heavily updated, it may be
worthwhile to uncomment this and install from your distribution CD or DVD. If you
have a broadband network connection or need a lot of updates, it may be more
efficient to just download the additional packages at the latest level from the network
Listing 4. /etc/apt/sources.list
Apt-get and similar tools use a local database to determine what packages are
installed. They can check installed levels against available levels. To do this,
information on available levels is retrieved from the sources listed in
/etc/apt/sources.list and stored on your local system. You use the command
apt-get update to synchronize the information in your local database with the
sources specified in /etc/apt/sources.list. You should do this before installing or
updating any package, and always after modifying /etc/apt/sources.list or adding
files to /etc/apt/sources.list.d.
Notice that the libreadline5 package that we installed as a prerequisite for gcl is not
removed automatically, although one of the output lines tells us that this package,
along with two Linux header packages, is no longer required. The autoremove
function of apt-get (or the equivalent remove function and the --auto-remove
option) will remove the requested packages, along with any packages that were
installed as dependencies but are no longer required by any installed packages. This
includes dependencies installed by packages other than the one or ones you are
trying to remove, such as the linux-headers-2.6.31-14 and
linux-headers-2.6.31-14-generic packages in our example. Listing 6 shows how to
remove gcl, its dependency libreadline5, and the two unrelated Linux header
packages that are no longer required.
If you use the autoremove function of apt-get but do not specify any package
name, then all unused packages that were installed as dependencies will be
removed from your system. You can also use the apt-get purge option to
remove configuration information. See the man page for more information.
Rather than updating individual packages, you can update all packages on your
system using the apt-get upgrade command. Similarly,
apt-get dist-upgrade will help you migrate to a new level of your distribution.
For more information on other capabilities and options for apt-get, see the man
page.
If you check the man page for apt-get, you will find that there are many options. If
you use the apt-get command a lot and find the default options are not to your
liking, you can set new defaults in /etc/apt/apt.conf. A program, apt-config, is
available for scripts to interrogate the apt.conf file. See the man pages for apt.conf
and apt-config for further information.
Figure 1 shows the first question you will be asked if you run
dpkg-reconfigure tzdata. The preset default may not be America; it will reflect
your own system. Navigate around this text-mode screen using the Tab key and the
Another tool that is part of the APT system is the dpkg tool. This is a medium-level
package management tool that can install and remove packages as well as display
status information. Configuration of dpkg can be controlled by /etc/dpkg/dpkg.cfg,
and you may also have a .dpkg.cfg file in your home directory to provide further
configuration.
The dpkg tool uses many files in the /var/lib/dpkg tree in your filesystem. In
particular, the file /var/lib/dpkg/status contains status information about packages on
your system. Listing 8 shows the use of dpkg -s to display the status of the tzdata
package after we updated it and the gcl package after we removed it. Note that the
gcl package still shows some config files as remaining. You can also use the purge
option to purge downloaded package files from the cache and remove configuration
information.
You will often want to know what is in a package or what package a particular file
came from. These are both tasks for dpkg. Listing 9 illustrates the use of dpkg -L
to list the files (including directories) installed by the libparted package. For most
packages, you can just give the package name and not worry about specifying a
particular version. However, some packages may be available in multiple versions,
so you may need to specify a more detailed package name when using dpkg to
interrogate the package information.
To find which package contains a specific file, use the -S option of dpkg, as shown
in Listing 10. The name of the package is listed on the left.
Sometimes a file may not appear to belong to any package. When this occurs, you
may need to do some extra sleuthing to find where a package comes from. For
example, the installation setup step may perform tasks such as creating symbolic
links that are not listed as part of the package contents. A relatively recent addition
to Linux systems is the alternatives system, which is managed using the
update-alternatives command. Alternatives are frequently created for
commands such as java, which might be the openJDK, Sun or IBM version, among
other possibilities.
Listing 11 shows how to use the which command to find what is invoked if we try to
run java. Then we use the ls command to see what that java command is
symbolically linked to. The link to the /etc/alternatives directory is a tip-off that we are
using the alternatives system, so we use the update-alternatives command to
find more information and finally we use the dpkg -S command to confirm that the
java command comes from the openjdk-6-jre-headless. The setup for the
alternatives system would have been done by a post-install script that is part of the
openjdk-6-jre-headless package.
Using aptitude
Earlier, we mentioned that the status for packages is kept in /var/lib/dpkg/status. We
also mentioned that dpkg could do more than just display package information. Now
let's look at the aptitude command, which provides a text-based full-screen
interface (using ncurses) to the APT package management functions. You can use
aptitude to install or remove packages as well as to control status flags that indicate
whether packages should be kept up-to-date or held in their present state, for
example. If you run the aptitude command (as root), you see a screen similar to
Figure 2.
Press Enter to expand or collapse the various selections, then use ctrl-t to access
the menu bar. Figure 3 shows that a new kernel version, 2.6.31.20, is available for
my system, among other available updates. The 'i' in the left column indicates that
the current status is to install the package. The Help menu item explains the various
options you have, including holding a package at its current level rather than
updating it, removing it, or marking it as being automatically installed and thus
eligible for automatic removal. Remember the autoremove option of apt-get?
Now you know how to examine or control which packages are eligible for automatic
removal. Use the keyboard shortcuts described in the Help or use the Package
menu item to change the flags.
You can use the slash ("/") key to search for packages. For example, if you wanted
to reinstall the gcl package that we removed earlier, simply type "/gcl" to search for
it. If the search takes you to something else with gcl in it, such as gcl-doc, press the
"n" key to advance to the next match. Then use the Package menu to mark the
package for installation.
When you are finished, select Actions->Install/remove packages (or press "g") to
apply your selections to the system. You can also select the quit option if you do not
want to apply the changes.
For help at any time, use the menu bar or type "?" (question mark) for help, and then
press the "q" key to exit the help.
The Apply button will install gcl and update any other packages that are scheduled
for update. The Reload button will refresh the package lists. If you are accustomed
to GUI interfaces, you may find synaptic easier to use than apt-get, dpkg, or dselect.
Similarly, you may find that your system includes update-manager, an X Window
System application, specifically tailored to help you keep your system up-to-date. If
installed, it is likely to be started automatically on a regular basis, so that you don't
forget to update. Figure 5 shows how update manager displays the set of updates
that you saw in Figure 2. As was the case with aptitude, the updates are classified
so you know which updates are important security updates.
You saw earlier that aptitude and synaptic also offer search tools. If you use
synaptic, note that you have options on the search menu for searching only package
names or package descriptions as well.
If you still can't find the package, you may be able to find it among the list of
packages on the Debian site (see Resources for a link), or elsewhere on the
Internet.
Most of the package tools can tell you a lot more about an installed package than
about one that you do not yet have installed, such as the list of files within a
package. If you need to find what package contains a program that you do not have
installed, there are a few ways:
• You can guess what package might contain it and download the package
without installing. Once you have the package, you can interrogate it.
• You can search the Internet.
• You can try the command-not-found capability, which is described under
"Command not found" later in this article.
The apt-get command has a -d option to download a package and not install it.
There is also a --print-uris option to show where a package would be
downloaded from and what its checksum would be. Current checksums are likely to
be SHA256 checksums, so you can check the integrity of the downloaded package
using the sha256sum command. Note that the URI and checksum information are
not displayed if you have already downloaded the package, so you should get this
information before downloading the package.
Suppose you want to know whether the gcl command is really contained in the gcl
package. Listing 13 shows you how to use apt-get to just download the gcl
package without installing it.
Once you have the package downloaded, you can use the --info option of dpkg
to display the package information, or the --contents option to show what files the
package installs. The downloaded file will be usually be located in
/var/cache/apt/archives/. Listing 14 shows how to locate the file you downloaded and
find out what binaries it will install (assuming they are installed in a .../bin/...
directory).
If you do find and download a .deb file using something other than apt-get, you
can install it using dpkg -i.
If you decide you do not want to install the package you downloaded into the APT
archives, you can run apt-get clean to clear out any downloaded package files.
If all else fails, there is another possible source for packages. Suppose you find a
program packaged as an RPM rather than a .deb. You may be able to use the
alien program, which can convert between package formats. You should read the
alien documentation carefully as not all features of all package management
systems can be converted to another format by alien.
fi
}
PackageKit
No discussion of package installation would be complete without mentioning
PackageKit, which is a system designed to make installing and updating software
easier. The intent is to unify all the software graphical tools used in different
distributions. PackageKit uses a system activated daemon, which means that the
daemon is activated only when needed. Packagekit has version for Gnome
(gnome-packagekit) and KDE KPackageKit).
There is a lot more to the Debian package management system than covered here.
There is also a lot more to Debian than its package management system. See
Resources for additional details and links to other articles in this series.
Resources
Learn
• Use the developerWorks roadmap for LPIC-1 to find the developerWorks
articles to help you study for LPIC-1 certification based on the April 2009
objectives.
• At the LPIC Program site, find detailed objectives, task lists, and sample
questions for the three levels of the Linux Professional Institute's Linux system
administration certification. In particular, see their April 2009 objectives for LPI
exam 101 and LPI exam 102. Always refer to the LPIC Program site for the
latest objectives.
• Review the entire LPI exam prep series on developerWorks to learn Linux
fundamentals and prepare for system administrator certification based on earlier
LPI exam objectives prior to April 2009.
• The Debian home page is your starting point for information about the Debian
distribution.
• Refer to the "Debian installation guide" for more information on installing
Debian.
• At Debian packages, display and search Debian package lists.
• See the "APT HOWTO" to learn more about the Debian package management
utility, APT.
• Check out the "Debian New Maintainers' Guide."
• Visit Ubuntu, Linux for Human Beings.
• See the PackageKit home page to learn more about PackageKit.
• The Linux Documentation Project has a variety of useful documents, especially
its HOWTOs.
• In the developerWorks Linux zone, find hundreds of how-to articles and
tutorials, as well as downloads, discussion forums, and a wealth other
resources for Linux developers and administrators.
• Stay current with developerWorks technical events and webcasts focused on a
variety of IBM products and IT industry topics.
• Attend a free developerWorks Live! briefing to get up-to-speed quickly on IBM
products and tools as well as IT industry trends.
• Watch developerWorks on-demand demos ranging from product installation and
setup demos for beginners, to advanced functionality for experienced
developers.