<?xml version='1.0' encoding='utf-8'?>
<!-- -*- DocBook -*- -->
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
    "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [
    <!ENTITY % shareddata SYSTEM "../debian-faq.ent" > %shareddata;
]>

<chapter id="pkg-basics"><title>Basics of the Debian package management system</title>
<para>
This chapter touches on some lower level internals of Debian package
management.  If you're interested mainly in <emphasis>usage</emphasis> of the
relevant tools, skip to chapters <xref linkend="pkgtools"/> and/or <xref
linkend="uptodate"/>.
</para>
<section id="package"><title>What is a Debian package?</title>
<para>
Packages generally contain all of the files necessary to implement a set of
related commands or features.  There are two types of Debian packages:
</para>
<itemizedlist>
<listitem>
<para>
<emphasis>Binary packages</emphasis>, which contain executables, configuration
files, man/info pages, copyright information, and other documentation.  These
packages are distributed in a Debian-specific archive format (see <xref
linkend="deb-format"/>); they are usually characterized by having a '.deb' file
extension.  Binary packages can be unpacked using the Debian utility
<literal>dpkg</literal> (possibly via a frontend like
<command>apt</command>); details are given in its manual page.
</para>
</listitem>
<listitem>
<para>
<emphasis>Source packages</emphasis>, which consist of a
<literal>.dsc</literal> file describing the source package (including the names
of the following files), a <literal>.orig.tar.gz</literal> file that contains
the original unmodified source in gzip-compressed tar format and usually a
<literal>.debian.tar.xz</literal> file that contains the Debian-specific changes to
the original source.  The utility <literal>dpkg-source</literal> packs and
unpacks Debian source archives; details are provided in its manual page.  (The
program <command>apt-get</command> can be used as a frontend for
<literal>dpkg-source</literal>.)
</para>
</listitem>
</itemizedlist>
<para>
Installation of software by the package system uses "dependencies" which are
carefully designed by the package maintainers.  These dependencies are
documented in the <literal>control</literal> file associated with each package.
For example, the package containing the GNU C compiler (<systemitem
role="package">gcc</systemitem>) "depends" on the package <systemitem
role="package">binutils</systemitem> which includes the linker and assembler.
If a user attempts to install <systemitem role="package">gcc</systemitem>
without having first installed <systemitem
role="package">binutils</systemitem>, the package management system (dpkg) will
send an error message that it also needs <systemitem
role="package">binutils</systemitem>, and stop installing <systemitem
role="package">gcc</systemitem>.  (However, this facility can be overridden by
the insistent user, see
<citerefentry><refentrytitle>dpkg</refentrytitle><manvolnum>8</manvolnum></citerefentry>.)
See more in <xref linkend="depends"/> below.
</para>
<para>
Debian's packaging tools can be used to:
</para>
<itemizedlist>
<listitem>
<para>
manipulate and manage packages or parts of packages,
</para>
</listitem>
<listitem>
<para>
administer local overrides of files in a package,
</para>
</listitem>
<listitem>
<para>
aid developers in the construction of package archives, and
</para>
</listitem>
<listitem>
<para>
aid users in the installation of packages which reside on a remote archive site.
</para>
</listitem>
</itemizedlist>
</section>

<section id="deb-format"><title>What is the format of a Debian binary package?</title>
<para>
A Debian "package", or a Debian archive file, contains the executable files,
libraries, and documentation associated with a particular program or set of
related programs. Normally, a Debian archive file has a filename that ends in
<literal>.deb</literal>.
</para>
<para>
The internals of this Debian binary packages format are described in the
<citerefentry><refentrytitle>deb</refentrytitle><manvolnum>5</manvolnum></citerefentry>
manual page.  This internal format is subject to change (between major releases
of &debian;), therefore please always use
<citerefentry><refentrytitle>dpkg-deb</refentrytitle><manvolnum>1</manvolnum></citerefentry>
if you need to do lowlevel manipulations on <literal>.deb</literal> files.
</para>
</section>

<section id="pkgname"><title>Why are Debian package file names so long?</title>
<para>
The Debian binary package file names conform to the following convention:
&lt;DebianPackageName&gt;_&lt;VersionNumber&gt;-&lt;DebianRevisionNumber&gt;_&lt;DebianArchitecture&gt;.deb
</para>
<para>
Checking the package name associated with a particular Debian archive file
(.deb file) can be done in one of these ways:
</para>
<itemizedlist>
<listitem>
<para>
inspect the "Packages" file in the directory where it was stored at a Debian
archive site.  This file contains a stanza describing each package; the
first field in each stanza is the formal package name.
</para>
</listitem>
<listitem>
<para>
use the command <literal>dpkg --info PPP_VVV-RRR_AAA.deb</literal> (where PPP,
VVV, RRR and AAA are the package name, version, revision and architecture of
the package in question, respectively).  This displays, among other things, the
package name corresponding to the archive file being unpacked.
</para>
</listitem>
</itemizedlist>
<para>
The <literal>VVV</literal> component is the version number specified by the
upstream developer.  There are no standards in place here, so the version
number may have formats as different as "19990513" and "1.3.8pre1".
</para>
<para>
The <literal>RRR</literal> component is the Debian revision number, and is
specified by the Debian developer (or a user who chooses to rebuild
the package locally).  This number corresponds to the revision level of the
Debian package, thus, a new revision level usually signifies changes in the
Debian Makefile (<literal>debian/rules</literal>), the Debian control file
(<literal>debian/control</literal>), the installation or removal scripts
(<literal>debian/p*</literal>), or in the configuration files used with the
package.
</para>
<para>
The <literal>AAA</literal> component identifies the processor for which the
package was built.  This is commonly <literal>amd64</literal>, which refers to
AMD64, Intel 64 or VIA Nano chips.  For other possibilities review Debian's archive
directory structure at <xref linkend="dirtree"/>.  For details, see the
description of "Debian architecture" in the manual page
<citerefentry><refentrytitle>dpkg-architecture</refentrytitle><manvolnum>1</manvolnum></citerefentry>.
</para>
</section>

<section id="controlfile"><title>What is a Debian control file?</title>
<para>
Specifics regarding the contents of a Debian control file are provided in the
Debian Policy Manual, section 5, see <xref linkend="debiandocs"/>.
</para>
<para>
Briefly, a sample control file is shown below for the Debian package hello:
</para>
<screen>
Package: hello
Version: 2.9-2+deb8u1
Architecture: amd64
Maintainer: Santiago Vila &lt;sanvila@debian.org&gt;
Installed-Size: 145
Depends: libc6 (>= 2.14)
Conflicts: hello-traditional
Breaks: hello-debhelper (&lt;&lt; 2.9)
Replaces: hello-debhelper (&lt;&lt; 2.9), hello-traditional
Section: devel
Priority: optional
Homepage: https://www.gnu.org/software/hello/
Description: example package based on GNU hello
 The GNU hello program produces a familiar, friendly greeting.  It
 allows non-programmers to use a classic computer science tool which
 would otherwise be unavailable to them.
 .
 Seriously, though: this is an example of how to do a Debian package.
 It is the Debian version of the GNU Project's "hello world" program
 (which is itself an example for the GNU Project).
</screen>
<para>
The Package field gives the package name.  This is the name by which the
package can be manipulated by the package tools, and usually similar to but not
necessarily the same as the first component string in the Debian archive file
name.
</para>
<para>
The Version field gives both the upstream developer's version number and (in
the last component) the revision level of the Debian package of this program as
explained in <xref linkend="pkgname"/>.
</para>
<para>
The Architecture field specifies the chip for which this particular binary was
compiled.
</para>
<para>
The Depends field gives a list of packages that have to be installed in order
to install this package successfully.
</para>
<para>
The Installed-Size indicates how much disk space the installed package will
consume.  This is intended to be used by installation front-ends in order to
show whether there is enough disk space available to install the program.
</para>
<para>
The Section line gives the "section" where this Debian package is stored at the
Debian archive sites.
</para>
<para>
The Priority indicates how important is this package for installation, so that
semi-intelligent software like apt or aptitude can sort the package into a
category of e.g. packages optionally installed.  See <xref
linkend="priority"/>.
</para>
<para>
The Maintainer field gives the e-mail address of the person who is currently
responsible for maintaining this package.
</para>
<para>
The Description field gives a brief summary of the package's features.
</para>
<para>
For more information about all possible fields a package can have, please see
the Debian Policy Manual, section 5, "Control files and their fields", see
<xref linkend="debiandocs"/>.
</para>
</section>

<section id="conffile"><title>What is a Debian conffile?</title>
<para>
Conffiles is a list of configuration files (usually placed in
<literal>/etc</literal>) that the package management system will not overwrite
when the package is upgraded.  This ensures that local values for the contents
of these files will be preserved, and is a critical feature enabling the
in-place upgrade of packages on a running system.
</para>
<para>
To determine exactly which files are preserved during an upgrade, run:
</para>
<screen>
dpkg --status package
</screen>
<para>
And look under "Conffiles:".
</para>
</section>

<section id="maintscripts"><title>What is a Debian preinst, postinst, prerm, and postrm script?</title>
<para>
These files are executable scripts which are automatically run before or after
a package is installed or removed.  Along with a file named
<literal>control</literal>, all of these files are part of the "control"
section of a Debian archive file.
</para>
<para>
The individual files are:
</para>
<variablelist>
<varlistentry>
<term>preinst</term>
<listitem>
<para>
This script is executed before the package it belongs to is unpacked from its
Debian archive (".deb") file.  Many 'preinst' scripts stop services for
packages which are being upgraded until their installation or upgrade is
completed (following the successful execution of the 'postinst' script).
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>postinst</term>
<listitem>
<para>
This script typically completes any required configuration of the package
<literal>foo</literal> once <literal>foo</literal> has been unpacked from its
Debian archive (".deb") file.
Many 'postinst' scripts
execute any commands necessary to start or restart a service once a new package
has been installed or upgraded.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>prerm</term>
<listitem>
<para>
This script typically stops any daemons which are associated with a package.
It is executed before the removal of files associated with the package.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>postrm</term>
<listitem>
<para>
This script typically modifies links or other files associated with
<literal>foo</literal>, and/or removes files created by the package.  (Also see
<xref linkend="virtual"/>.)
</para>
</listitem>
</varlistentry>
</variablelist>
<para>
Currently all of the control files can be found in the directory
<literal>/var/lib/dpkg/info</literal>.  The files relevant to package
<literal>foo</literal> begin with the name "foo" and have file extensions of
"preinst", "postinst", etc., as appropriate.  The file
<literal>foo.list</literal> in that directory lists all of the files that were
installed with the package <literal>foo</literal>.  (Note that the location of
these files is a dpkg internal; you should not rely on it.)
</para>
</section>

<section id="priority"><title>What is an <emphasis>Essential</emphasis>, <emphasis>Required</emphasis>, <emphasis>Important</emphasis>, <emphasis>Standard</emphasis>, <emphasis>Optional</emphasis>, or <emphasis>Extra</emphasis> package?</title>
<para>
Each Debian package is assigned a <emphasis>priority</emphasis> by the
distribution maintainers, as an aid to the package management system.  The
priorities are:
</para>
<itemizedlist>
<listitem>
<para>
<emphasis role="strong">Required</emphasis>: packages that are necessary for
the proper functioning of the system.
</para>
<para>
This includes all tools that are necessary to repair system defects.  You must
not remove these packages or your system may become totally broken and you may
probably not even be able to use dpkg to put things back.  Systems with only
the Required packages are probably unusable, but they do have enough
functionality to allow the sysadmin to boot and install more software.
</para>
</listitem>
<listitem>
<para>
<emphasis role="strong">Important</emphasis> packages should be found on any
Unix-like system.
</para>
<para>
Other packages which the system will not run well or be usable without will be
here.  This does <emphasis>NOT</emphasis> include Emacs or X or TeX or any
other large application.  These packages only constitute the bare
infrastructure.
</para>
</listitem>
<listitem>
<para>
<emphasis role="strong">Standard</emphasis> packages are standard on any Linux
system, including a reasonably small but not too limited character-mode system.
Tools are included to be able to send e-mail (with mutt) and download files
from archive servers.
</para>
<para>
This is what will be installed by default if users do not select anything else.
It does not include many large applications, but it does include the Python
interpreter and some server software like OpenSSH (for remote administration)
and Exim (for mail delivery, although it can be configured for local delivery
only).  It also includes some common generic documentation that most users will
find helpful.
</para>
</listitem>
<listitem>
<para>
<emphasis role="strong">Optional</emphasis> packages include all those that you
might reasonably want to install if you do not know what they are, or that do
not have specialized requirements.
</para>
<para>
This includes X, a full TeX distribution, and lots of applications.
</para>
</listitem>
<listitem>
<para>
<emphasis role="strong">Extra</emphasis>: packages that either conflict with
others with higher priorities, are only likely to be useful if you already know
what they are, or have specialized requirements that make them unsuitable for
"Optional".
</para>
</listitem>
</itemizedlist>
<para>
If you do a default Debian installation all the packages of priority <emphasis
role="strong">Standard</emphasis> or higher will be installed in your system.
If you select pre-defined tasks you will get lower priority packages too.
</para>
<para>
Additionally, some packages are marked as <emphasis
role="strong">Essential</emphasis> since they are absolutely necessary for the
proper functioning of the system.  The package management tools will refuse to
remove these.
</para>
</section>

<section id="virtual"><title>What is a Virtual Package?</title>
<para>
A virtual package is a generic name that applies to any one of a group of
packages, all of which provide similar basic functionality.  For example, both
the <literal>konqueror</literal> and <literal>firefox-esr</literal> programs
are web browsers, and should therefore satisfy any dependency of a program that
requires a web browser on a system, in order to work or to be useful.  They are
therefore both said to provide the "virtual package" called
<literal>www-browser</literal>.
</para>
<para>
Similarly, <literal>exim4</literal> and <literal>sendmail</literal> both
provide the functionality of a mail transport agent.  They are therefore said
to provide the virtual package "mail-transport-agent".  If either one is
installed, then any program depending on the installation of a
<literal>mail-transport-agent</literal> will be satisfied by the presence of
this virtual package.
</para>
<para>
Debian provides a mechanism so that, if more than one package which provide the
same virtual package is installed on a system, then system administrators can
set one as the preferred package.  The relevant command is
<literal>update-alternatives</literal>, and is described further in <xref
linkend="diverse"/>.
</para>
</section>

<section id="depends"><title>What is meant by saying that a package <emphasis>Depends</emphasis>, <emphasis>Recommends</emphasis>, <emphasis>Suggests</emphasis>, <emphasis>Conflicts</emphasis>, <emphasis>Replaces</emphasis>, <emphasis>Breaks</emphasis> or <emphasis>Provides</emphasis> another package?</title>
<para>
The Debian package system has a range of package "dependencies" which are
designed to indicate (in a single flag) the level at which Program A can
operate independently of the existence of Program B on a given system:
</para>
<itemizedlist>
<listitem>
<para>
Package A <emphasis>depends</emphasis> on Package B if B absolutely must be
installed in order to run A.  In some cases, A depends not only on B, but on a
version of B.  In this case, the version dependency is usually a lower limit,
in the sense that A depends on any version of B more recent than some specified
version.
</para>
</listitem>
<listitem>
<para>
Package A <emphasis>recommends</emphasis> Package B, if the package maintainer
judges that most users would not want A without also having the functionality
provided by B.
</para>
</listitem>
<listitem>
<para>
Package A <emphasis>suggests</emphasis> Package B if B contains files that are
related to (and usually enhance) the functionality of A.
</para>
</listitem>
<listitem>
<para>
Package A <emphasis>conflicts</emphasis> with Package B when A will not operate
if B is installed on the system.  Most often, conflicts are cases where A
contains files which are an improvement over those in B.  "Conflicts" are often
combined with "replaces".
</para>
</listitem>
<listitem>
<para>
Package A <emphasis>replaces</emphasis> Package B when files installed by B are
removed and (in some cases) over-written by files in A.
</para>
</listitem>
<listitem>
<para>
Package A <emphasis>breaks</emphasis> Package B when both packages cannot be
simultaneously configured in a system.  The package management system will
refuse to install one if the other one is already installed and configured in
the system.
</para>
</listitem>
<listitem>
<para>
Package A <emphasis>provides</emphasis> Package B when all of the files and
functionality of B are incorporated into A.  This mechanism provides a way for
users with constrained disk space to get only that part of package A which they
really need.
</para>
</listitem>
</itemizedlist>
<para>
More detailed information on the use of each of these terms can be found in the
Debian Policy manual, section 7.2, "Binary Dependencies", see <xref
linkend="debiandocs"/>.
</para>
</section>

<section id="pre-depends"><title>What is meant by Pre-Depends?</title>
<para>
"Pre-Depends" is a special dependency.  In the case of most packages,
<literal>dpkg</literal> will unpack the archive file of a package (i.e., its
<literal>.deb</literal> file) independently of whether or not the files on
which it depends exist on the system.  Simplistically, unpacking means that
<literal>dpkg</literal> will extract the files from the archive file that were
meant to be installed on your file system, and put them in place.  If those
packages <emphasis>depend</emphasis> on the existence of some other packages on
your system, <literal>dpkg</literal> will refuse to complete the installation
(by executing its "configure" action) until the other packages are installed.
</para>
<para>
However, for some packages, <literal>dpkg</literal> will refuse even to unpack
them until certain dependencies are resolved.  Such packages are said to
"Pre-depend" on the presence of some other packages.  The Debian project
provided this mechanism to support the safe upgrading of systems from
<literal>a.out</literal> format to <literal>ELF</literal> format, where the
<emphasis>order</emphasis> in which packages were unpacked was critical.  There
are other large upgrade situations where this method is useful, e.g. the
packages with the required priority and their LibC dependency.
</para>
<para>
As before, more detailed information about this can be found in the Policy
manual.
</para>
</section>

<section id="pkgstatus"><title>What is meant by <emphasis>unknown</emphasis>, <emphasis>install</emphasis>, <emphasis>remove</emphasis>, <emphasis>purge</emphasis> and <emphasis>hold</emphasis> in the package status?</title>
<para>
These "want" flags tell what the user wanted to do with a package (as indicated
by the user's direct invocations of
<literal>dpkg</literal>/<literal>apt</literal>/ <literal>aptitude</literal>).
</para>
<para>
Their meanings are:
</para>
<itemizedlist>
<listitem>
<para>
unknown - the user has never indicated whether the package is wanted.
</para>
</listitem>
<listitem>
<para>
install - the user wants the package installed or upgraded.
</para>
</listitem>
<listitem>
<para>
remove - the user wants the package removed, but does not want to remove any
existing configuration file.
</para>
</listitem>
<listitem>
<para>
purge - the user wants the package to be removed completely, including its
configuration files.
</para>
</listitem>
<listitem>
<para>
hold - the user wants this package not to be processed, i.e. wants to keep the
current version with the current status whatever that is.
</para>
</listitem>
</itemizedlist>
</section>

<section id="puttingonhold"><title>How do I put a package on hold?</title>
<para>
There are three ways of holding back packages, with dpkg, apt or aptitude.
</para>
<para>
With dpkg, you have to export the list of package selections, with:
</para>
<screen>
dpkg --get-selections \* > selections.txt
</screen>
<para>
Then edit the resulting file <filename>selections.txt</filename>, change the
line containing the package you wish to hold, e.g. <systemitem
role="package">libc6</systemitem>, from this:
</para>
<screen>
libc6                                           install
</screen>
<para>
to this:
</para>
<screen>
libc6                                           hold
</screen>
<para>
Save the file, and reload it into dpkg database with:
</para>
<screen>
dpkg --set-selections &lt; selections.txt
</screen>
<para>
With apt, you can set a package to hold using
</para>
<screen>
apt-mark hold package_name
</screen>
<para>
and remove the hold with
</para>
<screen>
apt-mark unhold package_name
</screen>
<para>
With aptitude, you can hold a package using
</para>
<screen>
aptitude hold package_name
</screen>
<para>
and remove the hold with
</para>
<screen>
aptitude unhold package_name
</screen>
</section>

<section id="sourcepkgs"><title>How do I install a source package?</title>
<para>
Debian source packages can't actually be "installed", they are just unpacked in
whatever directory you want to build the binary packages they produce.
</para>
<para>
Source packages are distributed on most of the same mirrors where you can
obtain the binary packages.  If you set up your APT's
<citerefentry><refentrytitle>sources.list</refentrytitle><manvolnum>5</manvolnum></citerefentry>
to include the appropriate "deb-src" lines, you'll be able to easily download
any source package by running
</para>
<screen>
apt-get source foo
</screen>
<para>
To help you in actually building the source package, Debian source packages
provide the so-called build-dependencies mechanism.  This means that the source
package maintainer keeps a list of other packages that are required to build
their package.  To see how this is useful, run
</para>
<screen>
apt-get build-dep foo
</screen>
<para>
before building the source.
</para>
</section>

<section id="sourcebuild"><title>How do I build binary packages from a source package?</title>
<para>
The preferred way to do this is by using various wrapper tools.  We'll show how
it's done using the <literal>devscripts</literal> tools.  Install this package
if you haven't done so already.
</para>
<para>
Now, first get the source package:
</para>
<screen>
apt-get source foo
</screen>
<para>
and change to the source tree:
</para>
<screen>
cd foo-*
</screen>
<para>
Then install needed build-dependencies (if any):
</para>
<screen>
sudo apt-get build-dep foo
</screen>
<para>
Then create a dedicated version of your own build (so that you won't get
confused later when Debian itself releases a new version):
</para>
<screen>
dch -l local 'Blah blah blah'
</screen>
<para>
And finally build your package:
</para>
<screen>
debuild -us -uc
</screen>
<para>
If everything worked out fine, you should now be able to install your package
by running
</para>
<screen>
sudo dpkg -i ../*.deb
</screen>
<para>
If you prefer to do things manually, and don't want to use
<literal>devscripts</literal>, follow this procedure:
</para>
<para>
You will need all of foo_*.dsc, foo_*.tar.gz and foo_*.debian.tar.xz to compile the
source (note: there is no .debian.tar.xz for some packages that are native to
Debian).
</para>
<para>
Once you have them (<xref linkend="sourcepkgs"/>) and if you have the
<systemitem role="package">dpkg-dev</systemitem> package installed, the
following command:
</para>
<screen>
dpkg-source -x foo_version-revision.dsc
</screen>
<para>
will extract the package into a directory called
<literal>foo-version</literal>.
</para>
<para>
If you just want to compile the package, you may cd into the
<literal>foo-version</literal> directory and issue the command
</para>
<screen>
dpkg-buildpackage -rfakeroot -b
</screen>
<para>
to build the package (note that this also requires the <systemitem
role="package">fakeroot</systemitem> package), and then
</para>
<screen>
dpkg -i ../foo_version-revision_arch.deb
</screen>
<para>
to install the newly-built package(s).
</para>
</section>

<section id="creatingdebs"><title>How do I create Debian packages myself?</title>
<para>
For a more detailed description on this, read the New Maintainers' Guide,
available in the <systemitem role="package">maint-guide</systemitem> package or
at <ulink url="https://www.debian.org/doc/devel-manuals#maint-guide"/>,
or the Guide for Debian Maintainers, available in the <systemitem
role="package">debmake-doc</systemitem> package or at <ulink url="https://www.debian.org/doc/devel-manuals#debmake-doc"/>.
</para>
</section>

</chapter>
