02. Debian (Ubuntu) Package Management dpkg

The Debian package management system works on packages with the .deb extension. The main commands are dpkg, apt-get, apt-cache and aptitude. aptitude is a text-user interface for apt-get which we won't cover here.

Let's begin by going over dpkg, which is mainly used to install, dissect and extract information about packages with the .deb extension.

Dissecting your packages with dpkg

To see a complete listing of all dpkg commands and options, use the --help option. Here are the actions (and some options) that go along with dpkg:

-i|--install
Install a debian package file.
Use -E or --skip-same-version to avoid installing the package if same version is already present.
Use -G to avoid installing if newer version of package already exists.
Use --recursive to install all packages that match the package name wildcard.
Use --no-act to check for dependencies, conflicts and other problems without actually installing.
-c|--contents
List contents of a debian package file.
--configure
Reconfigure an installed package that has been unpacked.
-P|--purge
Purges an installed package (removing config files as well).
-r|--remove
Uninstall a package (does not remove config files).
Use --no-act to check for dependencies, conflicts and other problems without actually installing.
Use -B or --auto-deconfigure to disable packages that depend on the one being removed.
-s|--status
Shows information and status on the installed package.
-p|--print-avail
Print available status details
-I|--info
Extract package information.
-l|--list
List all packages on your system. Can add a pattern after the action to list matching installed packages.
-L|--listfiles
Lists all files installed (or "owned") by the package.
-C|--audit
Searches for partially installed packages and suggests what to do.
-S|--search
Searches which packages installed a file. Place the name or pattern of the package as an argument after this option.

Additionally, here are some options you may use with one of the above actions.

--ignore-depend
Ignores the dependency package.
--force-depends
Force installation without dependency.
--force-conflicts
Overcome conflict problems.

6 examples with dpkg

For practice with the dpkg command, let's try installing the Vim package and testing out or dpkg command. Here are just select 6 things you can do with with dpkg on packages.

1) List information

To print out information about our package, use the -I or --info option.

$ dpkg --info vim_7.4.488-7_amd64.deb
new debian package, version 2.0. size 952572 bytes: control archive=2080 bytes. 935 bytes, 22 lines control 237 bytes, 4 lines md5sums 2471 bytes, 78 lines * postinst #!/bin/sh 1218 bytes, 57 lines * prerm #!/bin/sh Package: vim Version: 2:7.4.488-7 Architecture: amd64 Maintainer: Debian Vim Maintainers <pkg-vim-maintainers@lists.alioth.debian.org> Installed-Size: 2233 Depends: vim-common (= 2:7.4.488-7), vim-runtime (= 2:7.4.488-7), libacl1 (>= 2.2.51-8), libc6 (>= 2.15), libgpm2 (>= 1.20.4), libselinux1 (>= 1.32), libtinfo5 Suggests: ctags, vim-doc, vim-scripts Provides: editor Section: editors Priority: optional Homepage: http://www.vim.org/ Description: Vi IMproved - enhanced vi editor Vim is an almost compatible version of the UNIX editor Vi. . Many new features have been added: multi level undo, syntax highlighting, command line history, on-line help, filename completion, block operations, folding, Unicode support, etc. . This package contains a version of vim compiled with a rather standard set of features. This package does not provide a GUI version of Vim. See the other vim-* packages if you need more (or less).

2) List file contents

To check the package file contents, use the -c option.

$ dpkg -c vim_7.4.488-7_amd64.deb
drwxr-xr-x root/root         0 2015-03-31 16:37 ./
drwxr-xr-x root/root         0 2015-03-31 16:37 ./usr/
drwxr-xr-x root/root         0 2015-03-31 16:37 ./usr/bin/
-rwxr-xr-x root/root   2240936 2015-03-31 16:37 ./usr/bin/vim.basic
drwxr-xr-x root/root         0 2015-03-31 16:37 ./usr/share/
drwxr-xr-x root/root         0 2015-03-31 16:37 ./usr/share/bug/
drwxr-xr-x root/root         0 2015-03-31 16:37 ./usr/share/bug/vim/
-rw-r--r-- root/root       516 2014-10-22 18:01 ./usr/share/bug/vim/presubj
-rwxr-xr-x root/root       204 2014-10-22 18:01 ./usr/share/bug/vim/script
drwxr-xr-x root/root         0 2015-03-31 16:37 ./usr/share/doc/
drwxr-xr-x root/root         0 2015-03-31 16:37 ./usr/share/lintian/
drwxr-xr-x root/root         0 2015-03-31 16:37 ./usr/share/lintian/overrides/
-rw-r--r-- root/root       138 2015-03-31 16:37 ./usr/share/lintian/overrides/vim
lrwxrwxrwx root/root         0 2015-03-31 16:37 ./usr/share/doc/vim -> vim-common

3) Unpacking and Configuring

To unpack the package, use the --unpack option. This won't configure any settings, so we must apply the --configure option along with it.

$ dpkg --unpack vim_7.4.488-7_amd64.deb
(Reading database ... 166563 files and directories currently installed.) Preparing to unpack vim_7.4.488-7_amd64 (1).deb ... Unpacking vim (2:7.4.488-7) over (2:7.4.052-1ubuntu3) ...

When you run with the --configure option, you may get some dependency issues, with the terminal erroring out and saying that the versions don't match up. We can handle these dependency installations easily by using apt-get command. We'll study these in the next lesson, but for now, use the following command to automatically pull in any necessary dependencies and install vim.

$ sudo apt-get install vim

4) Checking the location of package

To check where the folders that your package or software is located, use the -L option.

$ dpkg -L vim
/. /usr /usr/bin /usr/bin/vim.basic /usr/share /usr/share/lintian /usr/share/lintian/overrides /usr/share/lintian/overrides/vim /usr/share/bug /usr/share/bug/vim /usr/share/bug/vim/presubj /usr/share/bug/vim/script /usr/share/doc /usr/share/doc/vim

5) Removing and Uninstalling

Use the -r or --remove option to remove Vim from our system, but keep the configuration files. To check if a package is installed or not, use the -s option.

$ dpkg -r vim
# Vim is now removed
$ dpkg -s vim
dpkg-query: package 'vim' is not installed and no information is available Use dpkg --info (= dpkg-deb --info) to examine archive files, and dpkg --contents (= dpkg-deb --contents) to list their contents.

To remove configuration files as well, we must use the -P or --purge option.

6) Listing out all packages

To list out all your packages on your system with the -l option. There's going to be a good amount of output, so consider piping to grep.

$ dpkg -l | grep vim
ii vim 2:7.4. | 052-1ubuntu3 amd64 Vi IMproved - enhanced vi editor ii vim-common 2:7.4. | 052-1ubuntu3 amd64 Vi IMproved - Common files ii vim-runtime 2:7.4. | 052-1ubuntu3 all Vi IMproved - Runtime files ii vim-tiny 2:7.4.052-1ubuntu3 amd64 Vi IMproved - enhanced vi editor - compact version r

View log files

To see a list of the previously run dpkg commands, look at the /var/log/dpkg.log file. The tail command shows a select few lines at the bottom of the file.

$ tail /var/log/dpkg.log
2015-09-14 13:49:12 startup archives unpack 2015-09-14 13:49:14 install vim:amd64 <none> 2:7.4.052-1ubuntu3 2015-09-14 13:49:14 status half-installed vim:amd64 2:7.4.052-1ubuntu3 2015-09-14 13:49:14 status unpacked vim:amd64 2:7.4.052-1ubuntu3 2015-09-14 13:49:14 status unpacked vim:amd64 2:7.4.052-1ubuntu3 2015-09-14 13:49:14 startup packages configure 2015-09-14 13:49:14 configure vim:amd64 2:7.4.052-1ubuntu3 <none> 2015-09-14 13:49:14 status unpacked vim:amd64 2:7.4.052-1ubuntu3 2015-09-14 13:49:14 status half-configured vim:amd64 2:7.4.052-1ubuntu3 2015-09-14 13:49:14 status installed vim:amd64 2:7.4.052-1ubuntu3

Reconfiguring a package back to default

In case you altered the default configurations of a package and want to revert back, simply use the dpkg-reconfigure command, followed by the package name.

Limitations of dpkg

The main limitation of the dpkg command is that it does not automatically pull in any dependencies. The commands that are able to link and pull the relevant dependencies are apt-get and apt-cache, which we'll see next.

Want to avoid becoming a code monkey?

Code Complete 2

Want to avoid becoming a code monkey? Try Good Practice

This book synthesizes the most effective techniques and must-know principles into clear, pragmatic guidance. No matter what your experience level, development environment, or project size, this book will inform and stimulate your thinking - and help you build the highest quality code.

$ Check price
54.9954.99Amazon 4.5 logo(291+ reviews)

More Good Practice resources

Take your Linux skills to the next level!

Command Line Kung Fu

Take your Linux skills to the next level! Try Linux & UNIX

Command Line Kung Fu is packed with dozens of tips and practical real-world examples. You won't find theoretical examples in this book. The examples demonstrate how to solve actual problems. The tactics are easy to find, too. Each chapter covers a specific topic and groups related tips and examples together.

$ Check price
14.9914.99Amazon 4.5 logo(27+ reviews)

More Linux & UNIX resources

Ad