Pinning HPC Software Stacks with Spack Environments: From Abstract Specs to Reproducible Builds
Pin HPC software stacks in Spack by writing abstract specs to spack.yaml, running spack concretize -f to generate a hashed concrete.yaml, then installing. Covers compiler registration, variant unification, build caches, and common pitfalls like stale concretization and missing externals.
26 Nov 2025, 03:20 UTC

The Problem: Reproducible HPC Stacks Across Machines and Time
High-performance computing teams need to rebuild identical software stacks—compilers, MPI libraries, math kernels, and domain packages—on different clusters, in containers, or months later after a system upgrade. Spack environments solve this by capturing an abstract specification in spack.yaml, expanding it through the concretizer into a fully resolved dependency graph with cryptographic hashes, and recording that graph in concrete.yaml. The result: spack install in a fresh environment reproduces the exact same build, provided the compiler, architecture, and variant choices remain available.
Mechanism: Abstract Specs → Concretized DAG → Install
An abstract spec like hdf5@1.14.3%gcc@11.2.0+mpi ^openmpi@4.1.5 declares intent: version, compiler, variants, and a dependency preference. The concretizer resolves every transitive dependency, selects variants (e.g., +shared vs ~shared), picks a target microarchitecture, and assigns each node a unique SHA-256 hash that becomes part of the install path (/opt/spack/opt/spack/linux-ubuntu22.04-skylake/gcc-11.2.0/hdf5-1.14.3-hash). concrete.yaml stores this complete directed acyclic graph (DAG) so subsequent installs skip re-resolution.
Worked Configuration: Minimal Environment for HDF5 with OpenMPI
Create a scratch environment and populate the three configuration files that drive concretization.
1. Initialize the environment
spack env create hdf5-mpi
cd $(spack env location hdf5-mpi)
# or: spack env activate hdf5-mpi
Run these commands on any machine with Spack in PATH. No elevated privileges needed; the environment lives under ~/.spack/envs/ or a project directory.
2. Write spack.yaml with abstract specs
# spack.yaml
spack:
specs:
- hdf5@1.14.3 %gcc@11.2.0 +mpi ^openmpi@4.1.5
concretizer:
unify: true
config:
install_tree:
root: /opt/spack/opt/spack
projections:
all: '{architecture}/{compiler.name}-{compiler.version}/{name}-{version}-{hash:7}'
unify: true forces a single variant value across the DAG (e.g., all +shared or all ~shared), preventing conflicts. The projection keeps install paths readable and predictable.
3. Register the compiler in compilers.yaml
# ~/.spack/linux/compilers.yaml (or env-specific compilers.yaml)
- compiler:
spec: gcc@11.2.0
paths:
cc: /usr/bin/gcc-11
cxx: /usr/bin/g++-11
f77: /usr/bin/gfortran-11
fc: /usr/bin/gfortran-11
flags: {}
operating_system: ubuntu22.04
target: x86_64_v3
modules: []
environment: {}
Run spack compiler find first to auto-detect; then copy the generated entry into the environment's compilers.yaml for portability.
4. Map external MPI in packages.yaml (optional but common)
# packages.yaml
packages:
openmpi:
externals:
- spec: openmpi@4.1.5 %gcc@11.2.0
prefix: /usr/lib/x86_64-linux-gnu/openmpi
buildable: False
hdf5:
variants: +mpi +shared ~hl ~fortran ~cxx
Setting buildable: False tells the concretizer to use the system OpenMPI instead of building it. Adjust prefix to match ompi_info --prefix output.
5. Concretize and inspect
spack concretize -f
cat concrete.yaml | head -80
The -f flag forces regeneration; without it, Spack reuses a stale concrete.yaml. The output shows every package with its full hash, e.g., hdf5@1.14.3%gcc@11.2.0+mpi+shared~hl~fortran~cxx arch=linux-ubuntu22.04-skylake ^openmpi@4.1.5%gcc@11.2.0 ....
6. Install
spack install
spack find --show-full-spec hdf5
spack find --show-full-spec confirms the installed hash matches concrete.yaml.
Binary Reuse: Build Caches
Once built, push concrete specs to a build cache so other machines skip compilation:
# On build machine
spack buildcache create -a -f -d /shared/buildcache hdf5
# On target machine (same arch/compiler)
spack buildcache keys -it /shared/buildcache
spack buildcache install -f /shared/buildcache hdf5
Caches are keyed by spec hash, compiler, and microarchitecture. A cache built for %gcc@11.2.0 on skylake will not be used for %gcc@12.1.0 or zen3.
Activation and Runtime Use
An environment must be activated for spack install and spack load to see its specs:
spack env activate hdf5-mpi
spack load hdf5
# or, for module-aware shells:
eval $(spack module lmod load --sh hdf5)
spack load alone only manipulates PATH, LD_LIBRARY_PATH, etc., if shell module integration (Lmod, Environment Modules) is configured. Without eval $(spack module lmod load --sh ...) or a spack view symlink tree, the load is a no-op.
Limits and Common Mistakes
Variant Conflicts
If one package requires +shared and another ~shared, concretization fails with ConcretizationError: conflicting variants. Fix by pinning variants in spack.yaml:
specs:
- hdf5@1.14.3 %gcc@11.2.0 +mpi +shared
- openmpi@4.1.5 %gcc@11.2.0 +shared
Or relax unify: false (not recommended for production stacks).
Stale concrete.yaml
Editing spack.yaml without spack concretize -f leaves the old DAG. Always run -f after spec changes.
Missing Compiler or MPI Registration
The concretizer cannot match %gcc@11.2.0 or ^openmpi@4.1.5 unless they appear in spack compilers or packages.yaml externals. Verify with:
spack compilers
spack spec --deps hdf5@1.14.3 %gcc@11.2.0 +mpi ^openmpi@4.1.5
Build Cache Portability
A cache entry is tied to the exact compiler version and target microarchitecture. Moving a cache across clusters with different CPU instruction sets (e.g., Skylake → Zen 3) requires rebuilding or a compatible target fallback.
Environment Isolation
spack env activate only affects the current shell. CI/CD scripts must activate in each step or use spack -e hdf5-mpi install.
Verification Checklist
- Create a test environment:
spack env create testenv && spack env activate testenv - Add a single spec to
spack.yaml, runspack concretize -f, inspectconcrete.yamlfor full hashes. - Run
spack installand confirmspack find --show-full-speclists the expected hash. - Verify compiler registration:
spack compilersshows the expectedgcc@11.2.0. - Check Spack version:
spack --version(CLI behavior for environments changed significantly after v0.19).
Version Assumptions
This article reflects Spack 0.21+ behavior (environments, concretizer.unify, buildcache CLI). Older releases may require spack concretize without -f to overwrite, or use spack -e flags differently. Consult spack help --all for your installed version.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.