How to Achieve Reproducible Builds
Make a build produce byte-identical output from the same source, so you can verify an artifact was built from the code you reviewed.
On this page
Introduction
A reproducible build produces byte-identical output from the same source, inputs and environment. It matters because it lets you verify that a published artifact was actually built from the source you reviewed, rather than from modified source that happens to work. Without reproducibility, you must trust the build machine, which is exactly the machine an attacker would target. Reproducibility is also a prerequisite for caching and for comparing two builds to detect tampering. This article covers the sources of non-determinism, how to eliminate them, and how to verify a build is reproducible.
What Reproducibility Buys
If two builds of the same source produce different bytes, you cannot verify an artifact by rebuilding it, and you cannot cache outputs across machines safely. Reproducibility closes that gap: rebuild the source yourself and compare the bytes to the published artifact, and a mismatch means the artifact was not built from that source. This is the verification that lets you trust a binary you did not build.
It also underpins build caching: a cache key assumes the same inputs produce the same output, which is exactly the reproducibility property. A non-deterministic build poisons a shared cache with variants that differ for no reason.
Sources of Non-Determinism
The usual suspects are timestamps embedded in the output, absolute paths recorded in debug information, random or unordered iteration over a file system or a map, and the build environment itself such as the locale or a version string. A build that embeds the current time changes every second; one that records the build machine’s home directory changes per machine; one that iterates a directory in the order the OS returns changes per file system.
Each is fixable. Set a fixed timestamp for archive entries, or normalize timestamps in the artifact. Use a relative path or a reproducible prefix instead of an absolute path. Sort any collection before iterating it. Fix the locale and the environment so they are not inputs. The principle is to identify every input that is not the source and make it fixed or excluded.
Pinning the Toolchain
The compiler, the package manager and the base image are inputs. Pin them by an exact version or, better, by a digest so a floating tag cannot change what runs. A lockfile pins dependencies, and the package manager must install exactly what it says, with a frozen lockfile in CI. A base image pinned by digest means the same layers are pulled every time.
# Pin the base image by digest, not by a mutable tag like node:22
FROM node@sha256:8b1f3a... AS build
WORKDIR /app
ENV LC_ALL=C.UTF-8 TZ=UTC SOURCE_DATE_EPOCH=1700000000
COPY package.json pnpm-lock.yaml ./
RUN corepack enable && pnpm install --frozen-lockfile
COPY . .
# Normalise file mtimes so tar and archive output is stable.
RUN find . -exec touch -d @$SOURCE_DATE_EPOCH {} + && pnpm build
Deterministic Archives and Metadata
The artifact itself must be assembled deterministically. A tar or zip records file order, permissions and timestamps, all of which vary by machine, so set them explicitly or use a tool with a reproducibility mode. Sort the entries, set a fixed modification time, and normalise permissions. The SOURCE_DATE_EPOCH convention, widely supported, tells build tools to use a fixed timestamp instead of the current one.
Metadata such as a version string or a build id must be derived from the source or fixed, not from the build environment. If you need a build id, derive it from the content hash so it is a function of the inputs, which also makes the id verifiable.
Verifying Reproducibility
Build the same source twice on the same machine and compare the outputs; if they differ, find the changing field by comparing the artifacts at a byte level. Then build on two different machines, or in two different containers, and compare again, because a single-machine build can be deterministic while still depending on machine state. Continuous reproducibility checks build twice and flag differences in CI.
When the outputs differ, diff them and look for the volatile part: a timestamp in a header, a path in debug info, a file order. Fix one source at a time and re-verify, because multiple sources of non-determinism can mask each other. Record which sources you fixed so the knowledge is not lost.
# Build twice and compare the outputs byte for byte.
pnpm build && sha256sum dist/*.tar.gz > first.sha
rm -rf dist && pnpm build && sha256sum dist/*.tar.gz > second.sha
diff first.sha second.sha && echo "reproducible" || echo "DIFFERS"
# Locate the differing bytes when it is not reproducible.
cmp -l first.tar.gz second.tar.gz | head # offsets and differing bytes
# Often the offset points into a header field: a timestamp or a path.
Reproducibility as Supply-Chain Verification
Reproducible builds are the mechanism behind independent verification: multiple parties build the same source and compare artifacts, and agreement means the published artifact matches the source. This is stronger than signing, which proves who built it but not from what. Combined with signing, reproducibility lets you verify both the builder’s identity and the source.
The practical adoption path is incremental: make your own build reproducible first so you can cache and compare, then publish the instructions so others can verify the artifact. Each fixed source of non-determinism brings you closer to a verifiable build. This is the same discipline that makes a build cache trustworthy rather than a source of wrong artifacts.
Conclusion
A reproducible build produces the same bytes from the same source, which lets you verify an artifact by rebuilding it and underpins safe caching. Find the sources of non-determinism: timestamps, absolute paths, unordered iteration and environment. Pin the toolchain by digest, normalise archive metadata with SOURCE_DATE_EPOCH, derive build ids from content, and build twice on two machines to verify. Adopt it incrementally, and the build becomes something you can prove rather than trust.
Sources: Reproducible Builds, SOURCE_DATE_EPOCH, SLSA: build provenance.
FAQ
- What is the most common source of non-reproducibility?
- Embedded timestamps. An artifact that records the build time changes every second, so two builds never match. Fix the timestamp with SOURCE_DATE_EPOCH or normalise it in the archive, then look for the next source.
- How is reproducibility related to build caching?
- A build cache assumes the same inputs produce the same output, which is exactly reproducibility. A non-deterministic build stores different variants under the same key and can serve an artifact that does not match the source, poisoning the cache.
Report a problem with this articleOpens a pre-filled issue on GitHub.