apt-dpkg-command

dpkg vs apt

dpkg only installs a package from local which apt-get installs it from local cache which is got from remote repo configure by /etc/apt/source.list, so when you run apt related commands better to run $ apt-get update first to sync local cache with remote repo.

dpkg only installs a package provided it does not solve dependency for the package, let’s say if a package depends on A, while A is not installed, install this package will fail, that’s the big difference for these two tools, so always use apt-get for published package while use dpkg for your own package.

dpkg commands

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# install the package
$ sudo dpkg -i package.deb

# if same file(path) in two debs, the later package can't be installed successfully if no overwrite
$ sudo dpkg -i --force-overwrite package.deb

# show all installed packages
$ dpkg --get-selections | grep green
$ dpkg -l | grep green

Output:
root@dev:~/build# dpkg -l | grep dhcp
rc dhcp 2.5.1 amd64 dhcp backend

ii: any package is installed without any error
rc: residual package(removed but config kept)
iU: broken


# show then content of it
$ dpkg -c package.deb

# show control file of it
$ dpkg -I package.deb

# show where the package is installed
$ dpkg -L ibus-gtk

# remove deb package
$ sudo dpkg -r ibus-gtk
# Remove/Delete an installed package except configuration files

$ sudo dpkg -P ibus-gtk
# Remove/Delete everything including configuration files

# repack---create debian package from installed files
$ sudo apt-get install dpkg-repack
$ dpkg-repack ibus-gtk

# extract files from deb to outputdir
$ dpkg-deb -x xxx.deb outputdir

# extract control files like control/preinst/postinst
$ dpkg -e xx.deb

# show tar files in deb
$ ar tv hello.deb
rw-r--r-- 0/0 4 Mar 28 23:17 2017 debian-binary
rw-r--r-- 0/0 270 Mar 28 23:17 2017 control.tar.gz
rw-r--r-- 0/0 2176 Mar 28 23:17 2017 data.tar.xz

# extract a debian
$ ar xv hello.deb
debian-binary control.tar.gz data.tar.xz

# only extract data file and extract data.tar.xz as well
$ dpkg-deb -x hello.deb

# check if deb package missed something
$ lintian xx.deb

# if a file is installed by apt/dpkg, check which package it belongs
$ dpkg -S /bin/cat

what happens when you install a deb package from apt/dpkg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
-i, --install package-file...
Install the package. If --recursive or -R option is specified, package-file must refer to a directory instead.

Installation consists of the following steps:

1. Extract the control files of the new package.

2. If another version of the same package was installed before the new installation, execute prerm script of the old package.

3. Run preinst script, if provided by the package.

4. Unpack the new files, and at the same time back up the old files, so that if something goes wrong, they can be restored.

5. If another version of the same package was installed before the new installation, execute the postrm script of the old package. Note that this script is executed after the preinst script of the new package, because new files are written at the same time old files are removed.

6. Configure the package. See --configure for detailed information about how this is done.

--unpack package-file...
Unpack the package, but don not configure it. If --recursive or -R option is specified, package-file must refer to a directory instead.

--configure package...|-a|--pending
Configure a package which has been unpacked but not yet configured. If -a or --pending is given instead of package, all unpacked but unconfigured packages are configured.

To reconfigure a package which has already been configured, try the dpkg-reconfigure(8) command instead.

Configuring consists of the following steps:

1. Unpack the conf files, and at the same time back up the old conf files, so that they can be restored if something goes wrong.

2. Run postinst script, if provided by the package.

The control files(post install, preinstall etc) are at /var/lib/dpkg/info

debug dpkg when install fails

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Setting up gconf2 (2.28.1-6) ...
dpkg: error processing gconf2 (--configure):
subprocess installed post-installation script returned error exit status 247

check why
$ dpkg -i --debug=7337 xx.deb

$ dpkg --configure -D 777 gconf2
# OR run the post script with shell debug
$ sh -x /var/lib/dpkg/info/gconf2.postinstpostinst configure 2.28.1-6

# if there is no script at /var/lib/dpkg/info, extract it
$ dpkg -e xx.deb
$ sh -x DEBIAN/postinst configure 2.28.1-6

apt commands

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# update local cache for apt
$ sudo apt-get update

$ sudo apt-get remove/install packageName
# remove not delete cfg file, but purge did
$ sudo apt-get purge packageName

# remove files with pattern matches
$ sudo apt-get remove "green-*"

# only download package to /var/cache/apt/archives
$ sudo apt-get install -d packageName

# search package from local cache
$ sudo apt-cache search *

# show runtime depends directly
$ sudo apt-cache depends common-dev

# show runtime depends recursively
$ sudo apt install apt-rdepends
$ sudo apt-rdepends common-dev

# show who depends on me
$ sudo apt-rdepends -r xxx

# fix pending issue that apt-get knows with -f option
$ sudo apt-get install -f