Creating a Debian Package (.deb)

Step 1

Create a directory named your package name. Ex: “my_package”

# mkdir my_package
Create DEBIAN subdirectory in “my_package” directory.

# mkdir my_package/DEBIAN

Step 2

Create control and installation files as following. The permission of these files must be 755.

my_package/DEBIAN/control
my_package/DEBIAN/preinst
my_package/DEBIAN/postinst
my_package/DEBIAN/prerm
my_package/DEBIAN/postrm

preinst is responsible for doing requisites before installing.

postinst is responsible for doing requisites after installing.

prerm is responsible for doing requisites before uninstalling. Ex: Stop running services.

postrm is responsible for doing requisites after uninstalling. Ex: Delete config and log files.

Step 3

Write parameters and commands in these files. Control files have a lot of parameters. You can find simple examples below.

Package: batur
Version: 0.0.1
Source: batur
Priority: optional
Architecture: all

If there are some depends, in other words, your package needs some other packages, you must set it in control files. Missing packages will be installed after getting user confirmation.

Package: Leapvox-Mail-Server
Version: 0.0.1
Source: Leapvox
Section: mail
Priority: optional
Maintainer: info@leapvox.com
Homepage: http://www.leapvox.com
Architecture: all
Depends:postfix,postfix-mysql,postfix-doc,dovecot-common,dovecot-imapd,dovecot-pop3d,libsasl2–2,libsasl2-modules,libsasl2-modules-sql,sasl2-bin,libpam-mysql,openssl
Description: Leapvox Mail Server

You can set a private version for depends.

Package: batur-orkun-1.0.0
Source:
Version: 1.0.0
Architecture: amd64
Maintainer: batur@bilkent.edu.tr
Installed-Size: 1000
Depends: python2.6 (>= 2.6.5), python-numpy (>= 1.3.0), python-scipy (>= 0.7.0)
Conflicts:
Replaces:
Section:
Priority: optional
Homepage: http://www.baturorkun.com
Description: Batur Orkun’un ozel debian paketidir.

Example of “preinst” script:

#!/bin/bash
set -e
echo Kurulum basliyor….
service postfix stop
service saslauthd stop
service dovecot stop
rm -rf /etc/default/saslauthd
exit 0;

Example of “postinst” script:

#!/bin/bash
set -e
chmod o= /etc/postfix/mysql-virtual_*.cf
chgrp postfix /etc/postfix/mysql-virtual_*.cf

chmod o= /etc/pam.d/smtp
chmod o= /etc/postfix/sasl/smtpd.conf
chmod o= /etc/postfix/smtpd.key

adduser postfix sasl
groupadd -g 5000 vmail
useradd -g vmail -u 5000 vmail -d /var/vmail -m

postconf -e ‘myhostname = leapvox.voxporta.com’
postconf -e ‘mydestination = leapvox.voxporta.com, localhost, localhost.localdomain’
postconf -e ‘mynetworks = 127.0.0.0/8’
echo “dovecot unix — n n — — pipe” >> /etc/postfix/master.cf
echo ” flags=DRhu user=vmail:vmail argv=/usr/lib/dovecot/deliver -d ${recipient}” >> /etc/postfix/master.cf
mv /etc/default/saslauthd__ /etc/default/saslauthd
newaliases
service postfix restart
service saslauthd restart
service dovecot restart

echo “Installation was completed successfully”
echo Batur Orkun
exit 0;

Step 4

Config files you want to copy are created according to the full directory structure in the “my_package” directory. my_package directory will be the root directory of all files in the package.

Example;

If you want to create “/usr/bin/my_file” file and “/var/www/my_dir” directory, your directory structure should be like that:

my_package/usr/bin/my_file
my_package/var/www/my_dir

Step 5

You are ready to build your package.

$ dpkg-deb –build my_package

After this process, if you have not any error, you will have the file named “my_package.deb”

DevOps & Software & Architect & Linux Geek — http://baturorkun.com