Linux Commands · October 22, 2021

nano package info

[Debian] Some Useful Debian Package Manager Commands

Debian Package Manager aka. dpkg, is mainly used for package management in Debian GNU/Linux. As you might guess, most of Debian derivatives like Kali Linux or Ubuntu uses dpkg too among with Debian-based distros like Mint.

We’ll start with getting a deb package. Later on, we’ll try to understand some of dpkg functionalities. I’ll be using “sl” package in this example. To download the package, you can use “apt-get download sl” command. This won’t install the package but instead download it to your current working directory.

Install, upgrade, remove and purge a deb package

To install a deb package with Debian package manager, you can use -i or –install flag, followed by the package file:

root@debian:~# ls
sl_5.02-1+b1_amd64.deb
root@debian:~# dpkg -i sl_5.02-1+b1_amd64.deb 
Selecting previously unselected package sl.
(Reading database ... 30627 files and directories currently installed.)
Preparing to unpack sl_5.02-1+b1_amd64.deb ...
Unpacking sl (5.02-1+b1) ...
Setting up sl (5.02-1+b1) ...
Processing triggers for man-db (2.9.4-2) ...
root@debian:~# echo $?
0

As you can see, there’s no error and “echo $?” returned zero. It’s safe to say that we’ve installed the package successfully.

To remove this package, simply use “-r” or “–remove” flag followed by the package name itself. Not the package file. I’ve tried with package file to show you the error:

root@debian:~# dpkg -r sl_5.02-1+b1_amd64.deb
dpkg: error: you must specify packages by their own names, not by quoting the names of the files they come in

Type dpkg --help for help about installing and deinstalling packages [*];
Use 'apt' or 'aptitude' for user-friendly package management;
Type dpkg -Dhelp for a list of dpkg debug flag values;
Type dpkg --force-help for a list of forcing options;
Type dpkg-deb --help for help about manipulating *.deb files;

Options marked [*] produce a lot of output - pipe it through 'less' or 'more' !
root@debian:~# dpkg -r sl
(Reading database ... 30651 files and directories currently installed.)
Removing sl (5.02-1+b1) ...
Processing triggers for man-db (2.9.4-2) ...
root@debian:~# echo $?
0

So, how to update a Debian package? Well, if you specify a newer version of a currently installed package, “-i” or “–install” will upgrade the package too. Let’s examine below:

root@debian:~# ls
sl_3.03-17+b2_amd64.deb  sl_5.02-1+b1_amd64.deb
root@debian:~# dpkg -i sl_3.03-17+b2_amd64.deb 
Selecting previously unselected package sl.
(Reading database ... 30644 files and directories currently installed.)
Preparing to unpack sl_3.03-17+b2_amd64.deb ...
Unpacking sl (3.03-17+b2) ...
Setting up sl (3.03-17+b2) ...
Processing triggers for man-db (2.9.4-2) ...
root@debian:~# dpkg -l | grep "Correct you if"
ii  sl                             3.03-17+b2                     amd64        Correct you if you type `sl' by mistake
root@debian:~# dpkg -i sl_5.02-1+b1_amd64.deb 
(Reading database ... 30676 files and directories currently installed.)
Preparing to unpack sl_5.02-1+b1_amd64.deb ...
Unpacking sl (5.02-1+b1) over (3.03-17+b2) ...
Setting up sl (5.02-1+b1) ...
Processing triggers for man-db (2.9.4-2) ...
root@debian:~# dpkg -l | grep "Correct you if"
ii  sl                             5.02-1+b1                      amd64        Correct you if you type `sl' by mistake
root@debian:~# 

ls” output shows there are two “sl” packages with different versions. First, I’m installing the older one. Notice it’s version at the output. It shows “3.03”. And then, I specify the newer one. The version changes to “5.02”

Sometimes you may want to remove a package but store it’s configuration files. In this case, you can use “dpkg -r”. But what if you want to completely get rid of a package? This is where “purge” comes into stage. Purge even works for already removed packages. Let’s assume you’ve removed a package but after a while you ran into it’s configuration files. You don’t have to worry about this, purge got your back 🙂 Note that some of the configuration files are not recognized by dpkg, therefore can not be removed by purge.

To purge a Debian package, you can use “-P” or “–purge” flag followed by the package name:

root@debian:~# dpkg -P sl 
(Reading database ... 30668 files and directories currently installed.)
Removing sl (5.02-1+b1) ...
Processing triggers for man-db (2.9.4-2) ...
root@debian:~# 

How to list all installed Debian packages?

If you paid attention to the previous examples, you may have noticed “dpkg -l” command.

You can list installed Debian packages with “dpkg -l” or “dpkg –list“:

root@debian:~# dpkg -l
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                        Architecture Description
+++-==============================-==============================-============-===============================================================================
ii  adduser                        3.118                          all          add and remove users and groups
ii  alsa-topology-conf             1.2.4-1                        all          ALSA topology configuration files
ii  alsa-ucm-conf                  1.2.4-2                        all          ALSA Use Case Manager configuration files
ii  anacron                        2.3-30                         amd64        cron-like program that doesn't go by time
ii  apparmor                       2.13.6-10                      amd64        user-space parser utility for AppArmor
ii  apt                            2.2.4                          amd64        commandline package manager
ii  apt-listchanges                3.24                           all          package change history notification tool
ii  apt-utils                      2.2.4                          amd64        package management related utility programs
ii  avahi-autoipd                  0.8-5                          amd64        Avahi IPv4LL network address configuration daemon
ii  base-files                     11.1+deb11u1                   amd64        Debian base system miscellaneous files
ii  base-passwd                    3.5.51                         amd64        Debian base system master password and group files
ii  bash                           5.1-2+b3                       amd64        GNU Bourne Again SHell
ii  bash-completion                1:2.11-2                       all          programmable completion for the bash shell

The output will come in a pager. You can pipe this output to “grep” too.

What package did this file come from?

Say you’ve found a file in your system and wondering if this file came from a package. You can use “dpkg -S” or “dpkg –search” to find this information:

root@debian:~# dpkg -S /etc/grub.d/00_header 
grub-common: /etc/grub.d/00_header
root@debian:~# dpkg --search /etc/vim/vimrc
vim-common: /etc/vim/vimrc

Find dependencies of a deb package

One of the Debian package manager’s task is to find dependencies. To find dependencies of a Debian package and get more information about it, you can use “dpkg -I” (uppercase i). “Depends” line will show you what you need:

root@debian:~# dpkg -I sl_3.03-17+b2_amd64.deb 
 new Debian package, version 2.0.
 size 26656 bytes: control archive=1164 bytes.
     487 bytes,    14 lines      control              
    1552 bytes,    24 lines      md5sums              
 Package: sl
 Source: sl (3.03-17)
 Version: 3.03-17+b2
 Architecture: amd64
 Maintainer: Hiroyuki Yamamoto <yama1066@gmail.com>
 Installed-Size: 97
 Depends: libc6 (>= 2.2.5), libncurses5 (>= 6), libtinfo5 (>= 6)
 Section: games
 Priority: optional
 Homepage: http://www.tkl.iis.u-tokyo.ac.jp/~toyoda/index_e.html
 Description: Correct you if you type `sl' by mistake
  Sl is a program that can display animations aimed to correct you
  if you type 'sl' by mistake.
  SL stands for Steam Locomotive.

Which files did a deb package bring?

If you want to list all the files that came with a Debian package, you can use “dpkg -L” or “dpkg –listfiles“. With this information, you can simply find configuration files, man pages etc. of a package:

root@debian:~# dpkg -L vim-common
/.
/etc
/etc/vim
/etc/vim/vimrc
/usr
/usr/bin
/usr/bin/helpztags
/usr/lib
/usr/lib/mime
/usr/lib/mime/packages
/usr/lib/mime/packages/vim-common
/usr/share
/usr/share/applications
/usr/share/applications/vim.desktop
/usr/share/doc
/usr/share/doc/vim-common
/usr/share/doc/vim-common/NEWS.Debian.gz
/usr/share/doc/vim-common/README.Debian
/usr/share/doc/vim-common/changelog.Debian.gz
/usr/share/doc/vim-common/changelog.gz
/usr/share/doc/vim-common/copyright
/usr/share/icons
/usr/share/icons/hicolor
/usr/share/icons/hicolor/48x48
/usr/share/icons/hicolor/48x48/apps
/usr/share/icons/hicolor/48x48/apps/gvim.png
/usr/share/icons/hicolor/scalable
/usr/share/icons/hicolor/scalable/apps
/usr/share/icons/hicolor/scalable/apps/gvim.svg
/usr/share/icons/locolor
/usr/share/icons/locolor/16x16
/usr/share/icons/locolor/16x16/apps
/usr/share/icons/locolor/16x16/apps/gvim.png
/usr/share/icons/locolor/32x32
/usr/share/icons/locolor/32x32/apps
/usr/share/icons/locolor/32x32/apps/gvim.png
/usr/share/lintian
/usr/share/lintian/overrides
/usr/share/lintian/overrides/vim-common
/usr/share/man
/usr/share/man/da
/usr/share/man/da/man1
/usr/share/man/da/man1/vim.1.gz
/usr/share/man/da/man1/vimdiff.1.gz
/usr/share/man/de
/usr/share/man/de/man1
/usr/share/man/de/man1/vim.1.gz
/usr/share/man/fr
/usr/share/man/fr/man1
/usr/share/man/fr/man1/vim.1.gz
/usr/share/man/fr/man1/vimdiff.1.gz
/usr/share/man/it
/usr/share/man/it/man1
/usr/share/man/it/man1/vim.1.gz
/usr/share/man/it/man1/vimdiff.1.gz
/usr/share/man/ja
/usr/share/man/ja/man1
/usr/share/man/ja/man1/vim.1.gz
/usr/share/man/ja/man1/vimdiff.1.gz
/usr/share/man/man1
/usr/share/man/man1/helpztags.1.gz
/usr/share/man/man1/vim.1.gz
/usr/share/man/man1/vimdiff.1.gz
/usr/share/man/pl
/usr/share/man/pl/man1
/usr/share/man/pl/man1/vim.1.gz
/usr/share/man/pl/man1/vimdiff.1.gz
/usr/share/man/ru
/usr/share/man/ru/man1
/usr/share/man/ru/man1/vim.1.gz
/usr/share/man/ru/man1/vimdiff.1.gz
/usr/share/vim
/usr/share/vim/vim82
/usr/share/vim/vim82/debian.vim
/var
/var/lib
/var/lib/vim
/var/lib/vim/addons
/usr/share/man/da/man1/rview.1.gz
/usr/share/man/da/man1/rvim.1.gz
/usr/share/man/de/man1/rview.1.gz
/usr/share/man/de/man1/rvim.1.gz
/usr/share/man/fr/man1/rview.1.gz
/usr/share/man/fr/man1/rvim.1.gz
/usr/share/man/it/man1/rview.1.gz
/usr/share/man/it/man1/rvim.1.gz
/usr/share/man/ja/man1/rview.1.gz
/usr/share/man/ja/man1/rvim.1.gz
/usr/share/man/man1/rview.1.gz
/usr/share/man/man1/rvim.1.gz
/usr/share/man/pl/man1/rview.1.gz
/usr/share/man/pl/man1/rvim.1.gz
/usr/share/man/ru/man1/rview.1.gz
/usr/share/man/ru/man1/rvim.1.gz
/usr/share/vim/vimrc

How to verify a deb package?

Sometimes you might want to check if your package’s information is correct. “dpkg -V” compares the installed files with the information on dpkg database. Let’s change one of the “sl” files and see the output:

root@debian:~# dpkg -V sl
root@debian:~# echo "test" >> /usr/share/doc/sl/README
root@debian:~# dpkg -V sl
??5??????   /usr/share/doc/sl/README
root@debian:~# 

At first try, we didn’t get any output. That means our package was verified. But when I change a file that come with the package, verify check fails.

The output is very similar to RPM verify output. From man page of dpkg:

The output format is selectable with the --verify-format option, which by default uses the rpm format, but that might change in the future, and as such, programs parsing this command output should be explicit about the format they expect.

So if you need further information about Debian package verification, you can refer to my RPM post.