Wednesday, April 29, 2009

A comprehensive command guide to Debian’s APT-GET and DPKG

Purpose: Debian has a very powerful package management system called APT. Learning some useful commands can really unleash the true power and usefulness of this package management system. From time to time I will add commands and other tips and tricks that will be helpful to solve some issues and get work done faster. The idea is to make this post a COMPREHENSIVE command guide for APT package management.

Note: For most of the examples, I have used “traceroute” as an example package wherever possible. In some scenarios I have used other packages for the example since traceroute was not suitable for those.

APT-GET Commands

  • To install a package. For example, let say you would like to install traceroute package:

#apt-get install traceroute

  • To install a package’s source files. For example, let say you would like to download “traceroute” package’s source:

# apt-get source traceroute

  • To install dependencies of a package for building the package from it’s source. For example, before you start building a binary package (traceroute) from it’s source, you need to install the dependencies that are required to build the package from it’s source:

# apt-get build-dep traceroute

  • To build a package from it’s source:

# apt-get source traceroute
# cd traceroute-VERSION
# debuild -uc -us
# cd ..

  • To fix a system with incorrect/broken dependencies. Also useful if the apt-get was stopped unexpectedly due to crash or power failure:

# apt-get -f install

DPKG Commands

  • To reconfigure any package that is unpacked but not yet configured or half-configured state. This can be used along with “apt-get -f install”. Also useful in case of unexpected shutdown while upgrading the system.

# apt-get -f install
# dpkg --configure -a

  • To remove a package (this does not remove the configuration files the package):

# dpkg --remove traceroute

  • To remove a package (and its configuration files):

# dpkg --purge traceroute

  • To reconfigure a package. For example suppose you want to select a different settings for your X server:

# dpkg-reconfigure xserver-xorg

  • To identify the package name that produced a particular file. For example, “I would like to know which Debian package produced the file ‘lft.db’:

# dpkg -S lft.db
or
# dpkg --search lft.db

Output:

traceroute: /usr/bin/lft.db
traceroute: /usr/share/man/man8/lft.db.8.gz

  • To list all the files installed a particular package:

# dpkg --listfiles traceroute

  • To list all the packages installed on the system along with their state, name, version and a description:

# dpkg --list

  • To list all the packages installed on the system (only names):

# dpkg --get-selections

  • To troubleshoot error messages like the following:

Unpacking dictionaries-common (from …/dictionaries-common_0.98.12_all.deb) …
dpkg-divert: cannot open diversions: No such file or directory
dpkg: error processing /var/cache/apt/archives/dictionaries-common_0.98.12_all.deb (–unpack):
subprocess pre-installation script returned error exit status 2
Selecting previously deselected package aspell.
Unpacking aspell (from …/aspell_0.60.6-1_i386.deb) …
Processing triggers for man-db …
Errors were encountered while processing:
/var/cache/apt/archives/dictionaries-common_0.98.12_all.deb
E: Sub-process /usr/bin/dpkg returned an error code (1)
debian-486:/var/lib/dpkg# ls

# touch /var/lib/dpkg/diversions

  • To troubleshoot error messages like following:

Errors were encountered while processing:
/var/cache/apt/archives/acpid_ 1.0.8-7.deb
E: Sub-process /usr/bin/dpkg returned an error code (1)

Try following commands one by one:

# apt-get -f install
# apt-get upgrade
# apt-get dist-upgrade

# dpkg --configure -a
# apt-get -f install

# cd /var/lib/dpkg/info
# rm -rf acpid*
# apt-get install acpid

# cd /var/lib/apt/lists
# rm *
# apt-get update
# apt-get install acpid

# cd /var/cache/apt/archives
# rm acpid_ 1.0.8-7.deb
# apt-get install acpid

APT-CACHE Commands

  • To perform a full-text search on a package’s name, description, etc:

# apt-cache search traceroute

  • To print detailed information of a package:

# apt-cache show traceroute

  • To print a list of packages a given package (traceroute) depends on. For example, show me all the packages on which traceroute depends:

# apt-cache depends traceroute

Output:

traceroute
Depends: libc6
Conflicts: tcptraceroute
Conflicts:
Conflicts: traceroute-nanog

  • To print a list of packages that are dependent on a particular package. For example, show me all the packages that are dependent on “traceroute” package:

# apt-cache rdepends traceroute

Output:

Reverse Depends:
xorp
traceroute-nanog
traceroute-nanog
licq
traceroute-nanog
ksniffer
traceroute-nanog
iputils-tracepath
traceroute-nanog
gnome-nettool
traceroute-nanog
education-common
traceroute-nanog

  • To print detailed information of the versions available for a package and the packages that reverse-depends on it. For example, show me all the packages which depends on traceroute:

# apt-cache showpkg traceroute

Happy Debians! :)

No comments: