---
title: How to create a debian package from scratch
date: 2019-05-02
tags: [code, linux]
description: The process of creating a debian package is, admittedly, tedious. However, that statement only applies if your goal is to create a real debian package.
---

The process of creating a debian package is, admittedly, tedious. However, that
statement only applies if your goal is to create a *real* debian package that
passes all lintian checks and could be included in the official repositories.
If you just want to use the package for yourself, the process is not actually
that hard.

## Why should I create a debian package?

If you are using debian or any of its derivatives such as ubuntu or mint, you
typically install software on your system as debian packages using the `apt`
command line tool.

This is great, as it allows you to install, remove, and update all software
from a central place. Compare that to your classical windows desktop where at
least 5 tools will prompt you to update them individually directly after
startup. But this also means that you cannot easily install software that is
not packaged for your distribution.

Of course, you can install most software using the classic `make && make
install`, but that leaves you with unmanaged files scattered across your file
system as well as manually installed dependencies. You will have to clean all
that up manually if you ever want to get rid of the software again.

A much cleaner approach is to create a debian package. That way your system
knows about both the files and dependencies and can remove them automatically.
As an added benefit, you get an automatic update if your distribution starts
packaging the software.

## The joy of meta-packages

A special kind of package you may want to create are meta-packages. A
meta-package is one that only defines dependencies and contains no files of its
own.

I use this extensively to install groups of packages. For example, I have a
package called `xi-desktop` which depends on all the packages I usually use for
my desktop. On a new system, I can simply install this single package and
everything else gets installed automatically. That way, the list of manually
installed packages stays short and concise. Meta-packages are also great to
manage build-dependencies for other packages.

## How to create a package

So here is the tutorial:

-	Create a new folder.
-	Copy any files you want to have installed into that folder. For example, if
	you want to install `/usr/bin/foo`, copy the file to `{folder}/usr/bin/foo`.
-	Create the file `{folder}/DEBIAN/control`. Here is a minimal example:

		Package: mypackage
		Maintainer: name <name@example.com>
		Architecture: all
		Version: 1.0.0
		Depends: bash,git
		Description: this is a great package!

	The documentation includes a [full list of possible
	fields](https://www.debian.org/doc/debian-policy/ch-controlfields.html).
-	Run `dpkg-deb --build {folder}` to create the actual package.

Simple as that.

You can install the package using `sudo apt install {filename}`.

## Taking the next step

Now that you have a simple package, there are some things you could look into
to improve it:

-	Set `Installed-Size` so apt can inform you about disc usage.
-	Set `Section` to a custom value so you can easily find the installed packages
	you created yourself using `aptitude search '~i ?section(mysection)'`.
-	Use `deb-name` to fix the package filename.
-	Use `lintian` to get further hints on how to improve the package.
-	Use a wrapper like [debpack](https://github.com/hoffa/debpack) to automate
	some of the steps.
-	Instead of copying the files manually into the package folder, instruct `make
	install` to do it automatically.

## Conclusion

Creating debian packages is not so hard after all. It may not be as refined as
the [`PKGBUILD` system in
arch](https://wiki.archlinux.org/index.php/Creating_packages), but it is not as
bad by far as people may tell you.
