Build Packages From Source In Debian [Easy Step-By-Step Guide]

The idea to build packages from source might not be something we often hear about when speaking in the context of Debian. It is more of an Arch-y thing but in this module, we are going to learn how can we do the same on any of the Debian based distros.

Steps to Build Packages From Source in Debian

Let’s get right into the steps to build packages from source in Debian.

Step 1: Setting up the Prerequisites

First things first we’ll need some packages which would help us throughout the process of building. All of them can be installed with the following apt command:

$ sudo apt install dpkg-dev devscripts

Step 2: Fetch Packages From Source

To fetch our source packages, first, we need to make the necessary changes in our /etc/apt/sources.list to enable them. So open the said file to your favorite text editor and comment all the lines begging with deb-src as such :

Before

# Generated by distrobuilder
deb http://deb.debian.org/debian bullseye main

# Additional line for source packages
# deb-src http://deb.debian.org/debian bullseye main

After

# Generated by distrobuilder
deb http://deb.debian.org/debian bullseye main

# Additional line for source packages
deb-src http://deb.debian.org/debian bullseye main

This allows us to fetch source packages from apt repositories directly via the CLI. Finalize the changes and update the repositories with :

$ sudo apt update

But why make this change ?

The lines starting with “deb” are binary packages and can be directly installed with the apt package manager. On the other hand, the lines starting with “deb-src” will help us to fetch the source packages which will come in handy in a minute.

Step 3: Fetch The Source Packages

Now that we have our arsenal ready, we can now get to the real part. Start by fetching a source package from the repository with:

$ sudo apt source <package-name>

For our example, we’ll fetch a simple “Hello World” program with :

$ sudo apt source hello

This should fetch us the following 3 files and one folder in the current working directory :

  • hello-2.10 (folder)
  • hello_2.10-2.dsc
  • hello_2.10-2.debian.tar.xz
  • hello_2.10.orig.tar.gz

Taking a look at the source code inside the folder using the ls command:

$ ls
ABOUT-NLS    config.in     doc          maint.mk     README          TODO
aclocal.m4   configure     GNUmakefile  Makefile.am  README-dev
AUTHORS      configure.ac  hello.1      Makefile.in  README-release
build-aux    contrib       INSTALL      man          src
ChangeLog    COPYING       lib          NEWS         tests
ChangeLog.O  debian        m4           po           THANKS

These are the main source code contents which will help us get our application up and working.

Step 4: Install Build Dependencies

It may happen that the application we are trying to build needs some additional packages to function properly. More than often applications need some shared libraries which are not natively available on most systems.

To make sure that we have all the various dependencies needed by the application we are trying to build, we can type:

$ sudo apt build-dep <package-name>

Following our example, for us this should look like this:

$ sudo apt build-dep hello

If we are missing any library, this should install it for us. With that, we can move onto the next step.

Step 5: Tweaking Our Source File (Optional)

Next up, we can tweak our code to make necessary changes as per our requirements which is one the major advantages of building packages from source.

We can tweak the source code located under the src subfolder of the main source directory. Like in our example, we find the following we have the following :

$ cd hello-2.10/src/
$ ls
hello.c  system.h

Looking into our hello.c file, we make a slight modification in line 60 just for our PoC purposes :

Before :

greeting_msg = _("Hello, world");

After :

 greeting_msg = _("Compiled From Source !");

Note that if you alter the source code in any way, you have to deal with the tests as well accordingly. Therefore be sure to make any changes under the “tests” directory.

Step 6: Build The Package

Now it’s time to build our .deb package using the source code. We can do this in the either of two ways :

Method 1: Using debuild

To build our package just go to the source’s parent directory and type :

$ sudo debuild -b -uc -us

Lets’s break down our command a bit :

  • sudo: To run the command with root privileges
  • debuild: It is the name of the Perl Script which would help us to build a package
  • -b: Build binary packages only
  • -uc: Prevent signing of chainlog
  • -us: Prevent signing of Source Code

This should give us a .deb file which can be found in the same folder where the source folder is located.

Note: You may find a “dbgsym” package as well, which basically packs some debugging information that might come in handy to developers.

Method 2: Using dpkg-buildpackage

In the main directory of source tree, type the following to build your package :

$ sudo dpkg-buildpackage -b -uc -us

Here too, the flags used are same as the previous one and this too shall give us our .deb package !

Note- If you don’t have any changes to make to the source package, you can directly generate your .deb file with:

$ sudo apt-get source --compile <package-name>

Step 7: Install The Package

Now that we have a .deb file, we can simply install it with :

$ sudo dpkg -i <package-name>

And that’s it! With this, we should be able to install the package we just built! In case of any missing dependencies, we can fix them by:

$ sudo apt install -f 

Going back to our example, we install it using :

$ sudo dpkg -i hello_2.10-2_amd64.deb

With this we can check our whether the package was successfully built or not by :

$ hello
Compiled From Source!

It reflects our modified source code !

Conclusion

Thus, we successfully built our packages from source in Debian ! This gives us full control over our applications allowing for tweaks as desired and building from source is indeed very easy !