Product SiteDocumentation Site

5.4. Manipulating Packages with dpkg

dpkg is the base command for handling Debian packages on the system. If you have .deb packages, it is dpkg that allows installation or analysis of their contents. But this program only has a partial view of the Debian universe: it knows what is installed on the system, and whatever it is given on the command line, but knows nothing of the other available packages. As such, it will fail if a dependency is not met. Tools such as apt-get, on the contrary, will create a list of dependencies to be installed as automatically as possible.

5.4.1. Installing Packages

dpkg is, above all, a tool for installing an already available Debian package (because it does not download anything). To do this, we use its -i or --install option.

Example 5.2. Installation of a package with dpkg

# dpkg -i man-db_2.5.7-4_i386.deb
(Reading database... 284247 files and directories currently installed.)
Preparing replacement man-db 2.5.7-3 (using .../man-db_2.5.7-4_i386.deb) ...
Unpacking the man-db update...
Configuring man-db (2.5.7-4) ...
Updating database of manual pages ...
Processing triggers for “doc-base”...
Processing 1 modified documentation file(s) 
Recording documents with scrollkeeper ... 

We can see the different steps performed by dpkg; we know, thus, at what point any error may have occurred. The installation can also be effected in two stages: first unpacking, then configuration. apt-get takes advantage of this, limiting the number of calls to dpkg (since each call is costly, due to loading of the database in memory, especially the list of already installed files).

Example 5.3. Separate unpacking and configuration

# dpkg --unpack man-db_2.5.7-4_i386.deb
(Reading database... 284247 files already installed.)
Preparing replacement of man-db 2.5.7-3 (using .../man-db_2.5.7-4_i386.deb) ...
Unpacking the man-db update...
Processing triggers for “doc-base”...
Treatment of a modified documentation file(s) 
Recording documents with scrollkeeper ... 
#  dpkg --configure man-db
Configuring man-db (2.5.7-4) ...
Updating database of manual pages ...

Sometimes dpkg will fail to install a package and return an error; if the user orders it to ignore this, it will only issue a warning; it is for this reason that we have the different --force-* options. The dpkg --force-help command, or documentation of this command, will give a complete list of these options. The most frequent error, which you are bound to encounter sooner or later, is a file collision. When a package contains a file that is already installed by another package, dpkg will refuse to install it. The following messages will then appear:
Unpacking libisc52 (from .../libisc52_1%3a9.6.ESV.R1+dfsg-0+lenny2_amd64.deb) ...
dpkg : error processing /var/cache/apt/archives/libisc52_1%3a9.6.ESV.R1+dfsg-0+lenny2_amd64.deb (--unpack) :
 trying to overwrite "/usr/lib/libisc.so.50", which is also in package libisc50 1:9.6.1.dfsg.P1-3
In this case, if you think that replacing this file is not a significant risk to the stability of your system (which is usually the case), you can use the option --force-overwrite, which tells dpkg to ignore this error and overwrite the file.
While there are many available --force-* options, only --force-overwrite is likely to be used regularly. These options only exist for exceptional situations, and it is better to leave them alone as much as possible in order to respect the rules imposed by the packaging mechanism. Do not forget, these rules ensure the consistency and stability of your system.

5.4.2. Package Removal

Invoking dpkg with the -r or --remove option, followed by the name of a package, removes that package. This removal is, however, not complete: all of the configuration files and scripts, log files (system logs) and other user data handled by the package remain. The reason for keeping them is to disable the program by uninstalling it, while preserving the option to reinstall it quickly and with the same configurations. To completely remove everything associated with a package, use the -P or --purge option, followed by the package name.

Example 5.4. Removal and purge of the debian-cd package

# dpkg -r debian-cd
(Reading database... 14170 files and directories already installed.)
Removing debian-cd ...
# dpkg -P debian-cd
(Reading database... 13794 files and directories already installed.)
Removing debian-cd ...
Removing debian-cd configuration files...

5.4.3. Other dpkg Features

Before concluding this section, we note that some dpkg options may query the internal database in order to obtain information. Giving first the long options and then corresponding short options (that will evidently take the same possible arguments) we cite --listfiles package (or -L), which lists the files installed by this package; --search file (or -S), which finds the package from which the file comes; --status package (or -s), which displays the headers of an installed package; --list (or -l), which displays the list of packages known to the system and their installation status; --contents file.deb (or -c), which lists the files in the Debian package specified; --info file.deb (or -I), which displays the headers of the Debian package.

Example 5.5. Various queries with dpkg

$ dpkg -L base-passwd
/.
/usr
/usr/sbin
/usr/sbin/update-passwd
/usr/share
/usr/share/man
/usr/share/man/ru
/usr/share/man/ru/man8
/usr/share/man/ru/man8/update-passwd.8.gz
/usr/share/man/pl
/usr/share/man/pl/man8
/usr/share/man/pl/man8/update-passwd.8.gz
/usr/share/man/man8
/usr/share/man/man8/update-passwd.8.gz
/usr/share/man/fr
/usr/share/man/fr/man8
/usr/share/man/fr/man8/update-passwd.8.gz
/usr/share/doc-base
/usr/share/doc-base/users-and-groups
/usr/share/base-passwd
/usr/share/base-passwd/passwd.master
/usr/share/base-passwd/group.master
/usr/share/lintian
/usr/share/lintian/overrides
/usr/share/lintian/overrides/base-passwd
/usr/share/doc
/usr/share/doc/base-passwd
/usr/share/doc/base-passwd/copyright
/usr/share/doc/base-passwd/users-and-groups.html
/usr/share/doc/base-passwd/changelog.gz
/usr/share/doc/base-passwd/users-and-groups.txt.gz
/usr/share/doc/base-passwd/README
$ dpkg -S /bin/date
coreutils: /bin/date
$ dpkg -s coreutils
Package: coreutils
Essential: yes
Status: install ok installed
Priority: required
Section: utils
Installed-Size: 12188
Maintainer: Michael Stone <mstone@debian.org>
Architecture: i386
Version: 8.5-1
Replaces: mktemp, timeout
Pre-Depends: libacl1 (>= 2.2.11-1), libattr1 (>= 2.4.41-1), libc6 (>= 2.6), libselinux1 (>= 1.32)
Conflicts: timeout
Description: GNU core utilities
 This package contains the basic file, shell and text manipulation
 utilities which are expected to exist on every operating system.
 .
 Specifically, this package includes:
 arch base64 basename cat chcon chgrp chmod chown chroot cksum comm cp
 csplit cut date dd df dir dircolors dirname du echo env expand expr
 factor false flock fmt fold groups head hostid id install join link ln
 logname ls md5sum mkdir mkfifo mknod mktemp mv nice nl nohup nproc od
 paste pathchk pinky pr printenv printf ptx pwd readlink rm rmdir runcon
 sha*sum seq shred sleep sort split stat stty sum sync tac tail tee test
 timeout touch tr true truncate tsort tty uname unexpand uniq unlink
 users vdir wc who whoami yes
Homepage: http://gnu.org/software/coreutils
$ dpkg -l 'b*' | head
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name                   Version    Description
+++-===================-==========-============================================
un  backupninja         <none>    (no description available)
un  base                <none>    (no description available)
un  base-config         <none>    (no description available)
ii  base-files          4          Debian base system miscellaneous files
ii  base-passwd         3.5.11     Debian base system master password and group
$ dpkg -c /var/cache/apt/archives/iceweasel_2.0.0.18-0etch1_i386.deb

drwxr-xr-x root/root         0 2008-11-23 23:18 ./
drwxr-xr-x root/root         0 2008-11-23 23:16 ./etc/
drwxr-xr-x root/root         0 2008-11-23 23:18 ./etc/iceweasel/
drwxr-xr-x root/root         0 2008-11-23 23:18 ./etc/iceweasel/profile/
-rw-r--r-- root/root      7138 2006-08-19 08:04 ./etc/iceweasel/profile/bookmarks.html
-rw-r--r-- root/root       153 2006-09-14 19:13 ./etc/iceweasel/profile/localstore.rdf
-rw-r--r-- root/root      3287 2005-02-01 18:36 ./etc/iceweasel/profile/search.rdf
-rw-r--r-- root/root       287 2004-11-30 22:26 ./etc/iceweasel/profile/mimeTypes.rdf
drwxr-xr-x root/root         0 2008-11-23 23:18 ./etc/iceweasel/profile/chrome/
-rw-r--r-- root/root      1078 2004-11-30 22:26 ./etc/iceweasel/profile/chrome/userChrome-example.css
-rw-r--r-- root/root       663 2004-11-30 22:26 ./etc/iceweasel/profile/chrome/userContent-example.css
-rw-r--r-- root/root       347 2004-07-28 23:20 ./etc/iceweasel/profile/prefs.js
-rw-r--r-- root/root        53 2008-11-23 22:43 ./etc/iceweasel/iceweaselrc
[...]
$ dpkg -I /var/cache/apt/archives/iceweasel_2.0.0.18-0etch1_i386.deb
 new Debian package, version 2.0.
 size 9126828 bytes : control file = 37297 bytes.
     346 bytes,     9 lines      conffiles            
    1255 bytes,    20 lines      control              
  122920 bytes,  1249 lines      md5sums              
    1267 bytes,    37 lines   *  postinst             #!/bin/sh
     293 bytes,     8 lines   *  postrm               #!/bin/sh
    2763 bytes,    70 lines   *  preinst              #!/bin/sh
     274 bytes,    12 lines   *  prerm                #!/bin/sh
 Package: iceweasel
 Version: 2.0.0.18-0etch1
 Section: web
 Priority: optional
 Architecture: i386
 Depends: fontconfig, psmisc, debianutils (>= 1.16), libatk1.0-0 (>= 1.12.2), libc6 (>= 2.3.6-6)...
 Suggests: iceweasel-gnome-support (= 2.0.0.18-0etch1), latex-xft-fonts, xprint, mozplugger, libkrb53
 Conflicts: mozilla-firefox (<< 1.5.dfsg-1), firefox (<< 2.0+dfsg-1)
 Replaces: mozilla-firefox, firefox (<< 2.0+dfsg-1)
 Provides: www-browser
 Installed-Size: 26364
 Maintainer: Eric Dorland <eric@debian.org>
 Description: lightweight web browser based on Mozilla
  Iceweasel is a redesign of the Mozilla browser component, similar to
  Galeon, K-Meleon and Camino, but written using the XUL user interface
  language and designed to be lightweight and cross-platform.
  .
  This browser is based on the Firefox source-code, with minor
  modifications. Historically, this browser was previously known as
  Firebird and Phoenix.

5.4.4. dpkg's Log File

A recently introduced feature in dpkg is that it keeps a log of all of its actions in /var/log/dpkg.log. This log is extremely verbose, since it details every one of the stages through which packages handled by dpkg go. In addition to offering a way to track dpkg's behavior, it helps, above all, to keep a history of the development of the system: one can find the exact moment when each package has been installed or updated, and this information can be extremely useful in understanding a recent change in behavior. Additionally, all versions being recorded, it is easy to cross-check the information with the changelog.Debian.gz for packages in question, or even with online bug reports.