Packagecloud logo

HOWTO: Build debian packages for simple shell scripts

TL;DR

This post goes over the creation of a debian package containing shell scripts using dh_make and debuild. Starting from structuring the packaging directory to building the final debian package, this tutorial covers the process of creating a debian package with just a few simple steps.

We’ll start by creating the directory where all the work will be done. This specially named directory is used by the dh_make command to generate the debian boilerplate and control files. We then edit the debian/install file to include the scripts to be installed by the package we’re creating and their install location on the target system. We finish by using the debuild command to build the package containing our shell script(s).

You can skip ahead and clone a git repository containing the code from this tutorial:

https://github.com/computology/debian-package-shell-script-tutorial

 

Setup

Tools necessary for this tutorial: dh_make and debuild. The debuild tool is installed via the devscripts package.

$ sudo apt-get install dh-make devscripts

 

Packaging a shell script

Setting up the package directory

We’re going to create and move into the directory where all the work will be done.

$ mkdir greetings-0.1
$ cd greetings-0.1

dh_make uses the current directory name to populate certain fields in the debian/control file. This means the current directory must be named in the format <package>-<version> (i.e. greetings-0.1). You can alternatively force the pacakage name by passing the -p flag (--packagename) to the dh_make command.

According to the description in the dh_make man page:

dh_make must be invoked within a directory containing the
source code, which must be named <packagename>-<version>.
The <packagename> must be all lowercase, digits and
dashes.  If  the  directory  name  does not conform
to this scheme, you must rename it before using dh_make.
Alternatively, you may be able to use the  --packagename option
to force the package name.

Now, let’s move the shell script into the directory we just created:

$ cp ~/hi.sh .

Our directory should look something like this:

`-- greetings-0.1
    `-- hi.sh

 

Creating the Debian boilerplate using dh_make

We’re going to leverage the dh_make tool to create the original source archive (greetings_0.1.orig.tar.gz) and generate the necessary debian boilerplate files.

Set $DEBEMAIL and $DEBFULLNAME environment variables for dh_make (Optional)

These values will be used to populate the fields in control and changelog files. This step is optional, but will save you some time when editing the generated debian boilerplate.

$ cat >>~/.bashrc <<EOF
DEBEMAIL="person@company.tld"
DEBFULLNAME="Person McTester"
export DEBEMAIL DEBFULLNAME
EOF
$ . ~/.bashrc

 

Using dh_make to create the debian packaging structure

Since we’re just packaging a shell script, we’re going to pass dh_make a couple of flags to get us where we need to be. From inside our greetings-0.1 directory, run the following:

$ dh_make --indep --createorig
The --indep flag for dh_make

Passing the --indep flag will tell dh_make that we intend to create a binary that can be shared across all cpu architectures.

 

The --createorig flag for dh_make

The --createorig flag will take the current directory (./greetings-0.1) and create an original source archive necessary for building the package. The source archive will be placed in the parent directory (../greetings_0.1.orig.tar.gz).

 

Debian boilerplate

After running the dh_make command above, you’ll have an original source archive in the parent directory and a debian directory inside ./greetings-0.1. The greetings-0.1 directory should look something like this:

.
|-- greetings-0.1
|   |-- debian
|   |   |-- changelog
|   |   |-- compat
|   |   |-- control
|   |   |-- copyright
|   |   |-- docs
|   |   |-- rules
|   |   `-- source
|           `-- format
|   `-- hi.sh
`-- greetings_0.1.orig.tar.gz

 

Debian example files with the .ex and .EX extensions and READMEs have been removed from this tutorial

From the dh-make man page: 'dh_make will also generate example files that are also customized for the package generated. You can remove all files with *.ex if you wish. You can also delete the README.Debian file if you don't have anything to put in it. Renaming the example files to their name without the .ex at the end (and editing them if necessary) will activate that feature of debhelper.'

Create the debian/install file

The debian/install file isn’t created with the other boilerplate files via dh_make. We need this file to tell debuild which files are to be included with this package. So, let’s create the debian/install file and edit it:

$ touch debian/install
$ vi debian/install

debian/install

hi.sh usr/bin

This shows hi.sh is placed into usr/bin on the installing system (the computer installing our soon-to-be created package). You can read more about the debian/install file here.

 

Package multiple scripts into one debian package

To package multiple scripts into a debian package, simply move those scripts into the directory (greetings-0.1) and make additional entries to the debian/install file. Once the files are in place, run the dh_make command with the --indep and --createorig flags to create a source archive with the newly added shell scripts.

debian/install
hi.sh usr/bin
hola.sh usr/bin
salut.sh usr/bin

 

You can also glob the shell scripts together in the debian/install file if you prefer **

debian/install
*.sh usr/bin

 

Building the package using debuild

Now that our debian directory is ready to go, let’s use the debuild tool to bring it all together and get an installable package:

$ debuild -us -uc

The -us and -uc flags for debuild

Using these flags tells debuild to proceed without GPG signing any .changes or .dsc files.

Refer to our other post on this topic: GPG signing debian packages with debsign.

Conclusion

Much like creating packages for your applications, creating packages for your shell scripts allow them to be easily distributed and installed. Plus, they can be easily rolled back in case an error is found. Using tools like dh_make and debuild can make packaging your .deb quite easy. Building packages from the ground up can prove to be a tedious task, so using tools like the ones in this tutorial can help take the pain out of creating debian packages.

You might also like other posts...