This site is now 100% read-only, and retired.

Rolling your own Debian packages (part 1)

Posted by bdf on Fri 20 Jan 2006 at 09:14

This two-part article explains how to make a Debian package of simple piece of software, presumably something you have written yourself. Although building a new package is more complex than rebuilding one or having one generated, the idea is that it is actually surprisingly simple to create basic Debian packages. In fact, if you can make software install into a temporary installation tree, you're already 90% done! This text provides a quick alternative to the more comprehensive Debian New Maintainers' Guide. Only knowledge of Makefiles and the basic Debian package tools is assumed.

The first part of this article will continue with some preliminary information about Debian packages. In the second part we walk through a concrete packaging example.

Why would you want to make a Debian package?

Perhaps you already know how to install software into /usr/local/bin using make install and wonder why you should bother to make a package? Contrary to what you might think, the main goal of packaging software does not have to be that you want to upload it to the official Debian distribution. A Debian package offers a uniform and integrated way to manage and install software on a Debian system. By packaging software, you can install, upgrade or remove the software using standard tools such as apt-get and dpkg. It also offers an easy way to distribute software, even to systems that don't have development tools installed. If you have to manage a number of machines, I'm sure you'll appreciate this.

What's inside a binary package?

To show how simple it is to create a package, let's first vulgarize the concept a bit: a .deb-file is nothing more than an archive with a bunch of files to be installed 'as such' in the filesystem tree, and a bit of control information. You can check this for yourself by looking at the internals of a binary package. The first part, the data files, usually consists of compiled programs, manpages and documentation (for the /usr/share/doc/packagename/ directory). The control part on the other hand, contains package management data (package name, version, dependencies,...) as well as MD5 checksums to verify the integrity of the first part. Additionally, it can hold some provisions for more advanced packages, such as helper scripts with additional remove and update actions. These two parts are sufficient for dpkg to provide its core features, such as installing packages, maintaining a list of installed packages and their files, verifying package dependencies, etc.

How is a Debian package created?

The command that initiates the creation of a .deb-file is dpkg-buildpackage. From the source tree of the software, it creates a clean installation tree in a temporary directory, and archives this tree to a .deb-file. It also packages the source tree as a source package. For these tasks, dpkg-buildpackage is heavily guided by the files in the special debian subdir of the source tree. In fact, almost all of the work is done by calling the debian/rules script, so the building process is largely controlled by the package source. This script should accept commands such as clean, build and binary and is normally implemented as a set of rules in a Makefile (hence its name). It will in turn invoke the original software build script (i.e. the original Makefile) as well as a number of tools from the debhelper suite. These tools, which all have a prefix "dh_" in their name, handle common tasks in the preparation of the installation tree and the consecutive .deb-archive. To name a few, these tasks include: installing files in the right place (dh_install*), generating md5 checksums (dh_md5sums), fixing file permissions (dh_fixperms) and creating the resulting archive file (dh_builddeb).

Apart from the rules script, dpkg-buildpackage extracts basic information from the control and changelog files as well. We will cover these files in the second part of the text. Also, it is common to invoke dpkg-buildpackage from a wrapper called debuild. It will conveniently call a package checker such as linda or lintian after the build process. Since all the different commands in this layered design can be confusing, a schematic overview is presented below:
  -> dpkg-buildpackage
       -> debian/control
       -> debian/changelog
       -> debian/rules
            -> original build scripts (./configure, Makefile,...)
            -> debhelper suite (dh_*)
  -> lintian or linda
  -> debsign
Below is the command to set up a fairly decent package building environment. The package dpkg-dev houses dpkg-buildpackage; debuild and debsign are provided by devscripts. fakeroot is used to mimic root status when installing into the installation tree.
apt-get install dpkg-dev debhelper devscripts fakeroot linda
In the second part of this text, we will use these commands to build a concrete package for a simple command line program.