How to build Debian packages from source

Debian and Debian-based systems like Ubuntu use the Advanced Package Tool, abbreviated as APT, to install, update, update, and uninstall software from the command line. Typically, the APT package manager stores a list of repositories in a file called /etc/apt/sources.list and in any file with a .list suffix in the directory. /etc/apt/sources.list.d/. When we install a package, the apt command pulls the binary or precompiled version of the given package from these repositories. Apart from installing binaries, APT also allows you to download the source code of the package. So you can then add some functionality to the source, build the package from the source, and finally install the modified version of the package. In this article, let’s see how to build debian packages from source on Debian, Ubuntu and other apt based systems such as Linux Mint.

Why should we build a package from source?

There can be many reasons for building a package from source. Here are some reasons I could think of now:

  • View the source code to find the error.
  • Add new features to packages that are no longer actively developed.
  • Install the most recent version of the package from sources. In general, packages in the official repositories may be out of date.
  • More importantly, learning how to compile Debian packages from source.

Install the required build tools

To build a binary package from source, we first need to install the required build tools. Depending on the command line tool to build the binary package, you may need to install any of the following packages:

  1. dpkg-dev
  2. devscripts

These packages provide many tools for building binary packages from source.

To install “dpkg-dev”, run:

$ sudo apt install dpkg-dev

To install the “devscripts” package, use the following command:

$ sudo apt install devscripts

Include source repositories

Make sure you enable the source repositories.

To do this, open the file /etc/apt/sources.list:

$ sudo vim /etc/apt/sources.list

You will see several lines in it as shown below:

# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://in.archive.ubuntu.com/ubuntu focal main restricted
# deb-src http://in.archive.ubuntu.com/ubuntu focal main restricted
[...]

In the above file, all lines starting with one or two hashes (#) are just comments, for information only. And strings without hashes are suitable repositories.

The apt repository lines beginning with “deb” refer to binary packages that can be installed by users using the apt package manager. Lines starting with “deb-src” refer to original packages. This is useful for developers. By default “deb-src” is commented out in the source list.

To download the source packages, you need to uncomment the “deb-src” line. Remove the # character to uncomment the line. After uncommenting, the repository lines should look like this:

# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://in.archive.ubuntu.com/ubuntu focal main restricted
deb-src http://in.archive.ubuntu.com/ubuntu focal main restricted
[...]

Save and close the file. Update the list of repositories using the command:

$ sudo apt update

Download source packages

Download the source code of the package using the command “apt source ”. For example, the following command will download the source code for “grayex World!” program:

$ apt source andreyex

Output example:

Reading package lists... Done
Need to get 734 kB of source archives.
Get:1 http://in.archive.ubuntu.com/ubuntu focal/main andreyex 1.10-2ubuntu2 (dsc) [1847 B]
Get:2 http://in.archive.ubuntu.com/ubuntu focal/main andreyex 1.10-2ubuntu2 (tar) [726 kB]
Get:3 http://in.archive.ubuntu.com/ubuntu focal/main andreyex 1.10-2ubuntu2 (diff) [6560 B]
Fetched 734 kB in 2s (336 kB/s)
dpkg-source: info: extracting andreyex in andreyex-1.10
dpkg-source: info: unpacking andreyex_1.10.orig.tar.gz
dpkg-source: info: unpacking andreyex_1.10-2ubuntu2.debian.tar.xz

As you can see, we have downloaded the source code of the andreyex World program.

$ ls 
andreyex-1.10 andreyex_1.10-2ubuntu2.debian.tar.xz andreyex_1.10-2ubuntu2.dsc andreyex_1.10.orig.tar.gz

View the actual content of the source code:

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

Install build dependencies for a package

Then we need to install the dependencies for this package. To do this, run:

$ sudo apt build-dep andreyex

We have now downloaded the source code of the package and installed the dependencies required to build this package. Then make changes to the source code.

Modify debian package source

Cd to the directory containing the package source code. In our case, it is andreyex-1.10.

$ cd andreyex-1.10

Modify the code as you wish. The code is located in the file “andreyex.c” in the “src” directory.

$ vi src/andreyex.c

Change the code and / or make any changes. After editing the code, save and close the file.

It’s time to create the actual .deb package.

Building Debian packages from source

We downloaded the source code and changed the code. Let’s build the package from source. We can do this in three ways:

  1. Using the command “dpkg-buildpackage”
  2. Using the “debuild” command
  3. Download the source code and build directly

Method 1 – Using dpkg-buildpackage:

Make sure you have installed the “dpkg-dev” package.

And then run the following command to build the package from source:

$ dpkg-buildpackage -rfakeroot -b -uc -us

Here:

  • dpkg-buildpackage is a command for building binary or source packages from sources.
  • -rfakeroot – Create a fakeroot environment to simulate superuser privileges (to avoid ownership and permission issues).
  • -b – build only binary package.
  • -uc Do not cryptographically sign the change log. those. do not sign .buildinfo and .changes files.
  • -us Don’t sign the original package.

Make sure you run the above command in the main directory of the source tree. Otherwise, nothing will work.

If the build was successful, you will see a message similar to the one below:

[...]
dpkg-deb: building package 'andreyex' in '../andreyex_1.10-2ubuntu2_amd64.deb'.
dpkg-deb: building package 'andreyex-dbgsym' in 'debian/.debhelper/scratch-space/build-andreyex/andreyex-dbgsym_1.10-2ubuntu2_amd64.deb'.
        Renaming andreyex-dbgsym_1.10-2ubuntu2_amd64.deb to andreyex-dbgsym_1.10-2ubuntu2_amd64.ddeb
 dpkg-genbuildinfo --build=binary
 dpkg-genchanges --build=binary >../andreyex_1.10-2ubuntu2_amd64.changes
dpkg-genchanges: info: binary-only upload (no source code included)
 dpkg-source --after-build .
dpkg-buildpackage: info: binary-only upload (no source included)

Congratulations! We have successfully created a .deb package from sources. Now go back to the parent directory and you will see the newly created .deb file:

$ cd ..

$ ls

Output example:

andreyex-1.10                             andreyex_1.10-2ubuntu2.dsc              andreyex_1.10-2ubuntu2_amd64.deb
andreyex-dbgsym_1.10-2ubuntu2_amd64.ddeb  andreyex_1.10-2ubuntu2_amd64.buildinfo  andreyex_1.10.orig.tar.gz
andreyex_1.10-2ubuntu2.debian.tar.xz      andreyex_1.10-2ubuntu2_amd64.changes

Method 2 – Using debuild

Make sure you have installed the “devscripts” package.

Then build the binary package by running the following command from the source tree:

$ debuild -b -uc -us

After a successful build, the resulting .deb package will be saved in the parent directory.

Method 3 – download and build directly

This is the simplest and most straightforward method than the above two methods. In this method, we download the package source code and pass the “–compile” parameter to build the binary on one command line below:

$ sudo apt-get source --compile andreyex

The above command will download the source code of the “andreyex” program, build the .deb package and save it in the current directory.

Install the .deb file

Go to the location where you saved the .deb file and install it as shown below:

$ sudo dpkg -i andreyex_1.10-2ubuntu2_amd64.deb

If you see a missing dependencies error, run:

$ sudo apt install -f

As you can see, building a debian package from source is very easy!

Sidebar