Build Debian Packages on Ubuntu: dpkg-buildpackage Guide

Overview

Creating native .deb installers directly from raw source code is a fundamental skill for system administrators and DevOps engineers working in Debian and Ubuntu ecosystems. At the center of this distribution workflow sits dpkg-buildpackage, a versatile build automation utility that coordinates compiling source code, staging binaries, and generating control archives. This end-to-end practical guide covers everything required to produce clean packages: provisioning toolchains, laying out required Debian metadata files, compiling packages, and verifying the final binary locally.

1. Preparing the Debian Packaging Toolchain and Environment

Before packaging software, your operating system requires fundamental compilation utilities, developer scripts, and Debian helper routines. Update local package lists and install the core build suite by executing apt-get update -qq && apt-get install -y build-essential devscripts debhelper dh-make.

Installing build-essential and packaging utilities

The dpkg-buildpackage utility acts as the primary driver responsible for transforming Debian source trees into installable binary packages (.deb) alongside their source metadata archives. Check your installed version to confirm that your packaging environment is operational.

⚙️ [Key Options]

-b : Generates binary packages only, skipping source archive creation.
-B : Compiles only architecture-dependent binary targets.
-A : Produces strictly architecture-independent packages (Architecture: all).
-S : Creates the source package files without compiling binaries.
-d : Disables build-time dependency verification (Build-Depends).
-nc : Prevents clearing the build directory before building (clean stage).
-us : Bypasses cryptographic GPG signing of the source archive.
-uc : Skips generating a GPG signature for the .changes manifest.
-j : Specifies the number of concurrent jobs to allocate for parallel builds.
-rfakeroot : Simulates superuser privileges during package file staging using fakeroot.

Verifying dpkg-buildpackage version

2. Setting Up Sample C Source Code and Makefile Targets

To keep experiments isolated from system files, create a dedicated workspace for version 1.0 of the demo application with mkdir -p ~/deb-build-lab/samplepkg-1.0.

Creating project workspace directory

Switch into the newly scaffolded project tree with cd ~/deb-build-lab/samplepkg-1.0.

Navigating to source directory

Next, create a minimal C source file that outputs a confirmation string to standard output using cat << 'EOF' > main.c.

cat « ‘EOF’ > main.c
#include <stdio.h>
int main(void) {
printf(“Hello from dpkg-buildpackage custom package!\n”);
return 0;
}
EOF

Debian packaging relies on deterministic compilation, staging, and cleanup tasks. Provide standard all, install, and clean instructions that respect the installation prefix variable DESTDIR by generating a Makefile with cat << 'EOF' > Makefile.

cat « ‘EOF’ > Makefile
all:
gcc -O2 main.c -o samplepkg
install:
install -D -m 755 samplepkg $(DESTDIR)/usr/bin/samplepkg
clean:
rm -f samplepkg
EOF

3. Configuring Debian Directory Structure and Control Manifests

Ensure your current shell session resides at the package source root before creating metadata files by executing cd ~/deb-build-lab/samplepkg-1.0.

Confirming package root location

Package builders look for a standardized debian/ directory containing operational blueprints and source declarations. Create this layout with mkdir -p debian debian/source.

Creating debian directory structure

Set the debhelper compatibility level to version 12 inside debian/compat with echo "12" > debian/compat.

Setting debhelper compat version

Declare the source format as 3.0 (quilt)—which cleanly separates pristine upstream code from Debian patches—using echo "3.0 (quilt)" > debian/source/format.

Configuring quilt source format

The debian/control file outlines package identity, maintainer contact details, supported hardware architectures, and runtime prerequisites. Generate this configuration using cat << 'EOF' > debian/control.

![cat « ‘EOF’ > debian/control Source: samplepkg Section: utils Priority: optional Maintainer: Test Developer [email protected] Build-Depends: debhelper (>= 12) Standards-Version: 4.5.0

Package: samplepkg Architecture: any Depends: ${shlibs:Depends}, ${misc:Depends} Description: Sample utility built with dpkg-buildpackage A demonstration binary package created via dpkg-buildpackage. EOF](captures/s03_c05_cat_____EOF____debian_control_Source__samplepkg_Se.png)

Package versioning, target distributions, and revision histories are extracted directly from debian/changelog. Populate the initial release log via cat << 'EOF' > debian/changelog.

![cat « ‘EOF’ > debian/changelog samplepkg (1.0-1) unstable; urgency=medium

  • Initial release.

– Test Developer [email protected] Mon, 02 Nov 2026 00:00:00 +0000 EOF](captures/s03_c06_cat_____EOF____debian_changelog_samplepkg__1_0-1.png)

To delegate compilation and packaging workflows to the modern dh sequencer, write an executable Makefile script using cat << 'EOF' > debian/rules.

cat « ‘EOF’ > debian/rules
#!/usr/bin/make -f
%:
dh $@
EOF

Because the packaging engine runs debian/rules as an autonomous script during build phases, grant execute permissions using chmod +x debian/rules.

Granting execution rights to debian/rules

4. Compiling the Binary .deb Package with dpkg-buildpackage

Confirm that you are located at the root directory of the package tree before initiating compilation using cd ~/deb-build-lab/samplepkg-1.0.

Verifying active build directory

Execute dpkg-buildpackage -us -uc -b to build a binary .deb package while omitting GPG signatures for local development.
The build engine follows the configured sequence, executing Makefile targets and staging binary files accordingly.

Running dpkg-buildpackage

Once compilation finishes, review the parent folder to inspect the newly generated binary package and associated metadata with ls -lh ~/deb-build-lab.

Listing generated build artifacts

5. Validating, Installing, and Executing the Built Package

Navigate into the parent workspace containing the newly generated .deb installer with cd ~/deb-build-lab.

Moving into artifact directory

Examine the embedded control fields, architecture tags, and package metadata using dpkg-deb -I samplepkg_1.0-1_*.deb.
Verify that package parameters, dependencies, and descriptions match the directives defined in debian/control.

Inspecting package control information

Audit the internal file structure and target filesystem locations with dpkg-deb -c samplepkg_1.0-1_*.deb.
Check that the executable binary is assigned to /usr/bin/samplepkg with correct 755 permissions.

Verifying archive file contents

Deploy the generated local package directly onto the operating system via dpkg -i samplepkg_1.0-1_*.deb.
The installer extracts payload files and copies the executable directly into the system hierarchy.

Installing package using dpkg

Check whether the new binary is recognized within your system search path (PATH) using which samplepkg.

Checking binary resolution in PATH

Run the application to verify its execution and observe the output string printed to your terminal with samplepkg.

Running the installed samplepkg binary

This walkthrough covered the practical workflow of compiling custom software into installable Debian packages using dpkg-buildpackage on Ubuntu. Implementing standard Debian packaging conventions allows you to distribute software cleanly, manage system dependencies predictably, and streamline long-term versioning across Linux environments.