Build a Debian Package

From GLUG-BOM

You are here : Main Page > Howtos > Build a Debian Package

Building a Debian package

I hope that one is familiar with some of the basic GNU/Linux commands. Mainly one will be using the tar, ar commands etc.. If one does not know how to run this commands one can go through their respective manual pages.

The very first thing that one has to have while creating a debian package is the control file. This file contains various values which dpkg, dselect and other package management tools will use to manage the package.

One can create a debian control file in the similar lines, by extracting the control file from a debian .deb archive. Let us consider that one has a somepackage.deb file. One will find a lot of .deb files in /var/cache/apt/archives/. Take any one of it. In this case we will take the package somepackage.deb file. Unpack the .deb file using the ar command with option x.

For listing the files that are in somepackage.deb

 $ ar tv somepackage.deb 
 rw-r--r-- 0/0      4 Mar 28 13:46 2002 debian-binary
 rw-r--r-- 0/0   1386 Mar 28 13:46 2002 control.tar.gz
 rw-r--r-- 0/0  39772 Mar 28 13:46 2002 data.tar.gz

For extracting use -x option,

 $ ar xv somepackage.deb

One will land with having the three files. 1. The contents of debain-binary will consits of a line "2.0\n". This states the version of the deb file format. For 2.0 all other lines get ignored. 2. Untar the control.tar.gz file using the the tar command.

 $ tar -zxvf control.tar.gz

It will list the following files.

 -rw-r--r--    1 root     root         1336 Mar 28  2002 control
 -rw-r--r--    1 root     root          388 Mar 28  2002 md5sums
 -rwxr-xr-x    1 root     root          253 Mar 28  2002 postinst
 -rwxr-xr-x    1 root     root          194 Mar 28  2002 prerm
 ...

Don't bother about the other files right now. Open the control file. It will look similar to the one below.

Sample control file.

 Package: somepackage		## Package name
 Version: 1.1-1			## Version Number	
 Section: base			## In which Debian CD should the package be merged
 Priority: optional		## Importance
 Architecture: all		## Architecture support
 Depends: bash (>= 2.05a-11), textutils (>= 2.0-12), awk, procps (>= 1:2.0.7-8),
 sed (>= 3.02-8), grep (>= 2.4.2-3), coreutils (>= 5.0-5)
 Maintainer: Mr. Prasanta Baruah <prasanta@metawire.org>	## Maintainer or Creator
 Description: Linux system information			## Description
 This package provides a broad overview of different
 system aspects.

Edit the file according to your package.

Note: When you are building the control file please remove the ## along with the things after ##. This was intended what things are required to be specified in the control file.

The other files i.e is the md5sums, preinst, postinst, prerm, postrm etc. are required when you have to run some scripts before or after the package gets installed. If you have some scripts to run before the installation of the package takes place put them in preinst, after the installation postinst. If you have to stop any daemons before installation put those scripts in prerm and if have to modify links etc. put them in postrm. For more details regarding the above files have a look at maintainer lists of debian.

3. The 'data.tar.gz' file contains all the files that will be installed with their destination paths.

If you happen to untar the data.tar.gz, you will find the path where the given package will get installed along with the files that are to be installed.

4. Lot's of theory. Let's do some practicals. :-)

Suppose you have package named as 'first' which you want to install as /usr/local/first. So first let's create a directory named 'debian' next to the file 'first'.

 $ mkdir -p ./debian/usr/local
 $ cp first ./debian/usr/local/

Create another directory DEBIAN inside debian for copying the control file.

 $ mkdir -p debian/DEBIAN
 $ find ./debian -type d | xargs chmod 755   # this is necessary on Debian Woody, don't ask me why
 $ cp control debian/DEBIAN  # the control file you have created.

You don't have to think of the md5sum. It will be created on its own. You will have to copy the other pre and post files in the same directory.

That's it. You are almost done in building the package. Just give the last command.

 $ dpkg-deb --build debian
 dpkg-deb: building package `first' in `debian.deb'.
 $ mv debian.deb first.deb

Whow your first debian package is created. Try installing it using,

 $ dpkg -i first.deb 

to see whether it works or not.

For more information regarding building a debian package, go to the package maintainer lists of debian.


Author(s)

Prasanta Baruah <prasanta at hbcse dot tifr dot res dot in>.