How to create a .deb file (tutorial)

Wojciech
Deplink
Published in
2 min readMar 5, 2018

In this article we’ll create simple .deb package which prints the “Hello, World” text. We won’t use theecho bash command, it’d be too simple. Instead our package will depends on the php-cli package and execute the php -r "echo 'Hello, World!';" command. This simple program is complicated to demonstrate how to create package which depends on the other package available in the official Ubuntu repository.

Directory structure

Create empty directory, for example foobar. Everything you create inside this directory will be mapped to the Linux root directory. If you create the foobar/path/to/the/run.sh file then after installation of the .deb package (built from the foobar directory) the run.sh will be available inside the /path/to/the/ directory. Of course it’s recommended to follow the Linux conventions and use location like /<usr_or_opt>/local/<package_name> (read about differences between usr and opt).

Ok, now it’s time for one exception from above rule. Inside the package directory we can have one special directory. It’s the DEBIAN directory (case sensitive) containing package metadata. The most important is the DEBIAN/control file. Let’s see this on the example.

Example package

Package description (foobar/DEBIAN/control):

Package: foobar
Version: 0.1.0
Section: development
Priority: optional
Architecture: all
Depends: php-cli (>= 5.6)
Maintainer: mail@example.org
Description: Foobar CLI
Description indented using one space

Postinst file (foobar/DEBIAN/postinst) called after package installation, read more about hooks in section “what is a Debian preinst, postinst, prerm, and postrm script?”:

#!/bin/shchmod +x /usr/bin/foobarexit 0

And your package script (foobar/usr/bin/foobar):

php -r "echo 'Hello, World!';"

To build .deb package we’ve to execute below commands:

chmod 775 foobar/DEBIAN/postinst
dpkg-deb --build foobar

And that’s it! Now you should have the .deb file, install it using below command:

sudo apt-get install ./foobar.deb

If any of the above steps aren’t clear then feel free to post questions, I’ll try to answer for all of them and fulfill the tutorial according to them.

--

--