Pinning Spack Builds with spack.yaml and spack.lock
Use spack.yaml and spack.lock to pin concrete Spack dependency graphs, then verify concretization on the target host before sharing builds.
18 May 2026, 16:38 UTC

The problem: abstract specs do not guarantee identical builds
You share a project that lists HDF5 and NetCDF as Spack dependencies. A teammate clones the repository on a different cluster and runs `spack install`. Spack resolves a newer compiler or a different MPI implementation on that machine. The package names match, but the underlying dependency graph differs, and numerical output can drift. On shared HPC systems, an abstract package list is usually not enough to reproduce the same software.
What spack.lock records
A Spack environment is a named scope that keeps installed packages separate from Spack's default root environment. You declare intent in `spack.yaml` using package names, versions, variants, and compiler constraints. Running `spack concretize` resolves those abstract constraints into exact versions, compiler paths, external dependencies, and dependency hashes. Spack writes that resolved graph to `spack.lock`. Committing both files lets teammates rebuild the same concrete graph, but only on hosts where concretization can produce the same result.
Worked example: create, lock, and verify an environment
Run these commands from a project directory that contains `spack.yaml`. You need a working Spack installation and write permission to the directory. No root access is required unless you are installing system-level compilers.
# spack.yaml
spack:
specs:
- hdf5@1.14.2 % gcc@11.2.0
- netcdf-c@4.9.2 % gcc@11.2.0
Create the environment in the current directory and activate it:
$ spack env create hdf5-netcdf
$ spack env activate hdf5-netcdf
Resolve the specs and generate the lock file:
$ spack concretize
Confirm that `spack.lock` was written, then inspect the concrete hash for one package. Use single quotes around the format string so the shell does not interpret the braces:
$ spack spec --format '{hash}' hdf5
Reinstall from the lock on the same host and compare the hash output. If the hash changes, the environment did not reproduce exactly. Before copying the lock to another host, confirm the required toolchain exists there:
$ spack compiler list
$ spack external find
Leave the scoped environment when finished:
$ spack env deactivate
Limitations: a lock is not portable by default
A `spack.lock` created on one host may not concretize identically on another. Compiler availability, external packages, package versions, and the Spack version itself can change the resolved graph. Binary build caches also assume compatible microarchitectures and operating systems, so the same lock does not guarantee a prebuilt binary will run on a different cluster.
Practical verification: run `spack compiler list` and `spack external find` on the target host before installing from the lock. After installation, compare `spack spec --format '{hash}'` output against the original lock on the same host. Commit `spack.yaml` and `spack.lock` to version control, but leave generated module directories and build output out of the repository.
Closing
Use `spack.yaml` to express what you need and `spack.lock` to record exactly what Spack built. Treat the lock as host-specific until you have verified concretization on the target machine, and regenerate it whenever compilers or externals change.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.