Debian Packaging: From Source to Repository

Isolated Build Environments

Learn why clean build environments matter and how to use sbuild to build Debian packages in an isolated environment.

Isolated Build Environments

A package that builds successfully on your computer may fail to build on another machine.

Why?

Because your development system may contain libraries, tools, or dependencies that your package has not properly declared.

This is one of the problems that isolated build environments are designed to solve.

In this lesson, we will explore the concept of clean builds and introduce sbuild, a tool widely used in Debian packaging to build packages inside controlled environments.

Learning Objectives

By the end of this lesson, you will be able to:

  • Explain why isolated build environments are important.
  • Understand the relationship between build dependencies and runtime dependencies.
  • Install and configure sbuild.
  • Create a clean Debian build environment.
  • Understand how packages are built inside a clean environment.

1. The Problem with Building on Your Own Machine

Imagine that you are developing a Go application that uses a system library.

You install that library on your computer and everything works.

You then create a Debian package and test it locally.

The package builds successfully.

However, another developer tries to build the same package on a clean Debian installation and the build fails.

The reason may be simple: your system already had a dependency installed, but your package did not declare it correctly.

This is an example of an undeclared build dependency.

A developer’s workstation is rarely a reliable representation of a minimal target system.

It may contain:

  • Libraries installed for unrelated projects.
  • Development headers that are not declared as build dependencies.
  • Additional compiler versions.
  • Locally installed tools.
  • Environment variables and configuration files.

A clean build environment helps expose these hidden dependencies.

2. What Is an Isolated Build Environment?

An isolated build environment is a separate, controlled filesystem in which a package is built.

Instead of using all the software installed on your host system, the build process runs in an environment containing only the required tools and dependencies.

A typical process looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Developer workstation
        |
        v
Debian source package
        |
        v
Isolated build environment
        |
        v
Install declared build dependencies
        |
        v
Build the package
        |
        v
Generate Debian package artifacts

The goal is to make the build process predictable and to detect missing dependencies.

Isolation does not automatically guarantee reproducible builds, but it is an important step toward reliable packaging.

3. Introducing sbuild

sbuild is a Debian package-building tool that uses clean environments to build source packages.

It is particularly useful because it separates the build environment from the developer’s ordinary operating system.

Some important characteristics:

  • Builds packages using declared build dependencies.
  • Supports multiple Debian releases.
  • Produces build logs.
  • Can detect missing build dependencies.
  • Is used in Debian’s package-building infrastructure.

The Debian project documents sbuild as one of the standard approaches for clean package builds.

4. Install sbuild

On a Debian system, install the required tools:

1
2
3
4
5
6
sudo apt update

sudo apt install \
    sbuild \
    schroot \
    debootstrap

These packages provide:

Package Purpose
sbuild Builds Debian source packages in isolated environments
schroot Manages chroot environments
debootstrap Creates a minimal Debian filesystem

Check the installation:

1
sbuild --version

5. Configure Your User

The sbuild group allows a regular user to access configured build environments.

Add your account:

1
sudo sbuild-adduser "$USER"

You must start a new login session for the group membership to take effect.

Alternatively, you can start a new shell with the updated group:

1
newgrp sbuild

Verify the group membership:

1
id

You should see sbuild in the list of groups.

Do not run package builds as root. The build tool manages the privileged operations it needs.

6. Create a Clean Debian Environment

We will create a clean Debian environment using sbuild-createchroot.

For this example, we will use Debian 13 (Trixie) on the amd64 architecture.

First, create the destination directory:

1
sudo mkdir -p /srv/chroot

Now create the build environment:

1
2
3
4
5
sudo sbuild-createchroot \
    --arch=amd64 \
    trixie \
    /srv/chroot/trixie-amd64-sbuild \
    http://deb.debian.org/debian

This operation downloads and prepares a minimal Debian filesystem.

It may take several minutes, depending on your internet connection.

The resulting environment contains the basic tools required for package builds.

If you are targeting a different Debian release or architecture, adjust the suite and architecture accordingly.

7. Inspect the Build Environment

After creation, list the configured chroots:

1
schroot -l

You should find an entry corresponding to the environment you created.

You can also inspect the directory:

1
sudo ls -la /srv/chroot/trixie-amd64-sbuild

To open a shell inside the environment, use its configured schroot name:

1
schroot -c trixie-amd64-sbuild

Inside the environment, inspect the operating system:

1
cat /etc/os-release

You should see information identifying the Debian release used by the chroot.

Exit the environment:

1
exit

8. How sbuild Uses Build Dependencies

One of the most important features of sbuild is its handling of build dependencies.

Debian source packages declare build requirements in:

1
debian/control

For example:

1
2
3
4
5
6
7
8
9
Source: syspeek
Section: utils
Priority: optional
Maintainer: Example Maintainer <[email protected]>
Build-Depends:
 debhelper-compat (= 13),
 dh-golang,
 golang-any
Standards-Version: 4.7.0

When sbuild builds a source package, it reads the build dependency information and installs the required packages in the isolated environment.

This means that the build does not depend on development packages accidentally installed on your workstation.

We will examine the debian/control file in detail later in the course.

9. Build Isolation Is Not the Same as Reproducibility

It is important to distinguish two related concepts.

Isolated builds attempt to ensure that the build only uses explicitly declared dependencies in a controlled environment.

Reproducible builds aim to ensure that independent builds from the same source produce bit-for-bit identical results.

An isolated environment helps with reproducibility, but does not guarantee it.

Other factors can affect the final output, including:

  • Timestamps.
  • File ordering.
  • Build paths.
  • Compiler versions.
  • Environment variables.
  • Non-deterministic build scripts.

We will focus on clean builds first. Reproducibility is a deeper topic that can be explored after we have a working package.

10. What We Learned

In this lesson, we explored why building packages directly on a development workstation can be unreliable.

We introduced sbuild, installed its supporting tools, configured our user, and created a clean Debian build environment.

The most important principle is:

A package should declare everything it needs to build, rather than relying on whatever happens to be installed on the developer’s machine.

Exercises

Try these exercises before moving on:

  1. Run sbuild --version and inspect the installed version.
  2. Run id and verify that your account belongs to the sbuild group.
  3. List the available environments with schroot -l.
  4. Enter your Debian environment and inspect /etc/os-release.
  5. Compare the packages installed on your host with those available inside the clean environment.

What’s Next?

We now have a development environment and a clean Debian build environment.

It is time to introduce the software that we will package throughout this course.

In the next lesson, we will create Syspeek, a small Go command-line application that displays useful Linux system information.

We will write the application, organize its source code, and prepare it for Debian packaging.

Next: Introducing Syspeek: Our Go Project