Learn dpkg, the package manager for Debian – Part One

Dear linux geek,

In this tutorial I will try to teach you how to use the the dpkg package manager for Debian, a tool that is used to install, build, remove and manage packages in debian or debian based systems.

This tutorial is for people that like to work from the command line because dpkg itself is controlled entirely via command line parameters.

For people that don’t know, a command line parameter consists of exactly one  action and zero or more options. By learning the command line parameters of dpkg you can control the Debian package manager, tell it what to do and control the behaviour of its actions.

I use the dpkg tool to install .deb files in my Ubuntu machine, but unlike apt it does not automatically download packages and install their dependencies.

For me, dpkg is very useful when I need to install standalone deb files in my machine. But, what command is used to install .deb files with the dpkg Debian package manager?

Use the -i or –install long option to install the package in your Debian based system. You can also specify a directory with the –recursive option.

sudo dpkg -i package-name

The following command does the same thing.

sudo dpkg --install package-name

The dpkg utility can be used to list the installed packages on your system. The -l option can help you with that. Run the following command and watch the output displayed in your terminal.

The following command lists all packages installed on your system.

dpkg -l

Make sure to run every command shown in this article with superuser privilege.  The above command will not work and the output shown below will be printed on your terminal screen.

dpkg: error: requested operation requires superuser privilege

But the following works.

sudo dpkg -l

I have so many packages installed on my Ubuntu 12.04 LTS machine that the amount of output generated by the dpkg -l command is so big it will take one hour to read.

If you want to get information if a specific package installed on your system or not you can use piping  which is used to combine command together in order to create more complex and useful commands.

The following combination is used to send the output of the dpkg -l command to the grep command. The grep utility is used to print lines matching a pattern.

sudo dpkg -l | grep zip

After running the above command I get the output shown below.

ii  bzip2                                                       1.0.6-1                                  high-quality block-sorting file compressor - utilities
ii  gzip                                                        1.4-1ubuntu2                             GNU compression utilities
ii  libzip2                                                     0.10-1ubuntu1                            library for reading, creating, and modifying zip archives (runtime)
ii  p7zip-full                                                  9.20.1~dfsg.1-4                          7z and 7za file archivers with high compression ratio
ii  unzip                                                       6.0-4ubuntu2                             De-archiver for .zip files
ii  zip                                                         3.0-4                                    Archiver for .zip files

How can you uninstall a package by using the dpkg tool? The following command is used to uninstall a package, but it is not recommended.

sudo dpkg -r package-name

If you want to uninstall packages by using the dpkg command line utility you risk to damage your system because dpkg does not handle dependencies.

I have alot of experience with this tool. I have broken many installed packages on my system by using the debian package manager.

For example, if you uninstall python on your machine packages that depend on it will be broken. Use the apt-get command line for uninstalling packages on your own system.

The following command will take care of dependencies.

sudo apt-get remove package-name

The apt-get remove command will create a dependency tree and will warn you about the packages that will be removed. In the next part we will take a deep journey into dpkg and learn more complex stuff.