How to build a Debian package

The following are some notes on how to build a Debian package. It is not really a complete tutorial.

I will assume that you are using subversion, and I will assume that you will add your package to an apt repository.

*) Install dependencies
apt-get install svn-buildpackage gcc debhelper dh-make epm fakeroot

*) Make a directory for your source code
mkdir -p packageName/branches
mkdir -p packageName/tags
mkdir -p packageName/packageName-0.1

*) Enter packageName-0.1 directory and touch a fake tarball
cd packageName/packageName-0.1
touch ../packageName-0.1.tar.gz

*) Use dh_make to create your default debian directory.
dh_make -n -s -e yourEmail@yourDomain.com -f ../packageName-0.1.tar.gz
“-n” is the flag for creating a native Debian package.
“-s” is the flag for creating a single binary.

*) Move packageName-0.1 to trunk
cd ../;mv packageName-0.1 trunk
Now, you may remove the tar balls.
rm packageName_0.1.orig.tar.gz; rm packageName-0.1.tar.gz

*) Step into trunk/debian. Since I do not want to create any cron jobs or emacs or init scripts or man pages in this package, I am going to delete these example scripts.
cd trunk/debian
rm cron.d.ex emacsen-install.ex emacsen-remove.ex emacsen-startup.ex init.d.ex manpage.1.ex menu.ex manpage.xml.ex manpage.sgml.ex watch.ex

*) Rename the example install scripts that you want to keep.
mv postinst.ex postinst; mv preinst.ex preinst; mv postrm.ex postrm; mv prerm.ex prerm

*) Remove any other example scripts that you do not want.
rm *.EX; rm *.ex

*) Edit the control file to update the “Description:” and to add any package dependencies to “Depends:” (if any exist).
vi control

*) If you want postinst commands to run, edit postinst and add commands on a new line after:
case "$1" in
configure)
#echo "My command"

*) Step into trunk, and create a Makefile.
cd ../; vi Makefile
Below are a few examples of what could be in your Makefile.
SHELL=/bin/sh
INSTALL_TMP = install -d -m 1777 -o root -g root
INSTALL_DIR = install -d -m 755 -o root -g root
INSTALL_DIR_APACHE = install -d -m 755 -o www-data -g www-data
INSTALL_PROGRAM = install -c -m 755 -o root -g root
INSTALL_MAN = install -c -m 444 -o bin -g bin
INSTALL_APACHE = install -c -m 644 -o www-data -g www-data


PEAR=$(DESTDIR)/usr/share/php
TMP=$(DESTDIR)/tmp


install:
^t $(INSTALL_DIR) $(PEAR)
^t $(INSTALL_TMP) $(TMP)
^t $(INSTALL_PROGRAM) myProgram.sh $(TMP)

You will need to use tabs (^t) after “install:” The commands above will create the /usr/share/php and the /tmp directory in your fakeroot environment. Then, you can copy files into these two directories. The last command will install the “myProgram.sh” file (which is located in trunk) into the /tmp directory with the permissions of 755 and with the ownership of root:root. If you want to execute this program as a post installation script, you will need to enter it in debian/postinst with the path, e.g., “/tmp/myProgram.sh”.

*) Before you build your package, add your source code to subversion. Step into the directory above the package directory.
cd ../../ ; svn add packageName; svn ci -m "Adding packageName source code to subversion"

*) Now, you are ready to build the Debain package. Step into trunk (where your Makefile is located) and execute the following command.
cd packageName/trunk; dpkg-buildpackage -us -uc -rfakeroot

*) You package will be built and copied one level up in the directory tree. So, cd up one level, and add your package to your apt repository. The following command assumes that your apt repository is on the local machine in /var/www/yourRepo/. And, it assumes that your username is a sudoer.
cd ../ ; sudo reprepro -Vb /var/www/yourRepo/ includedeb yourRepo-unstable packageName_0.1-1_i386.deb

Creating a Debian package is really not that complicated. But, do not forget to test your package to make sure that it installs without errors. If you have errors, fix them, and then rebuild the package. You can delete the package from the apt repository and re-add it after rebuilding.
sudo reprepro -Vb /var/www/yourRepo/ remove yourRepo-unstable packageName_0.1-1_i386.deb
sudo reprepro -Vb /var/www/yourRepo/ includedeb yourRepo-unstable packageName_0.1-1_i386.deb

Leave a Reply