Creating and Verifying a Reproducible Spack Environment
Step‑by‑step guide to create a named Spack environment, install packages, generate module files, and confirm that the exact concretization is captured in spack.lock.
21 Dec 2025, 13:45 UTC

Desired outcome
Create a named Spack environment that contains a specific set of packages, activate it in the current shell, and record the exact concretization in spack.lock so the same environment can be reproduced later.
Prerequisites
- Spack version 0.19 or newer (check with
spack --version). - A working compiler configuration (
spack compilersshows at least one usable compiler). - Write access to the Spack var directory (
$SPACK_ROOT/var/spack/environments). - Basic familiarity with Spack spec syntax (e.g.,
hdf5@1.12.1%gcc@11.2.0).
Procedure
- Create the environment
Choose a name (e.g.,
myenv) and optionally provide aspack.yamlfile that lists the desired packages. If you do not have a yaml file, you can create the environment empty and add specs later.spack env create myenvThis creates a directory
$SPACK_ROOT/var/spack/environments/myenvcontaining an initialspack.yaml. - Activate the environment
Activation modifies the shell’s
SPACK_ENVvariable so subsequent Spack commands operate inside the environment.spack env activate myenvAfter activation, the prompt may show the environment name depending on your shell configuration.
- Add packages to the environment
Specify the packages you need. For example, to add HDF5 and OpenMPI with specific versions and compilers:
spack add hdf5@1.12.1%gcc@11.2.0 spack add openmpi@4.1.5%gcc@11.2.0Each
spack addupdates the environment’sspack.yaml. - Concretize and install
Run the installation. The
--fail-fastflag stops at the first error, making it easier to detect problems.spack install --fail-fastSpack will resolve dependencies, write the concrete spec tree to
spack.lock, and build/install the packages into the environment’s install path. - Generate module files
Create a shell‑sourceable file that sets up the environment’s
PATH,LD_LIBRARY_PATH, etc., for the installed packages.spack loads --to-file env_modules.shThis writes a script
env_modules.shin the current directory. - Load the module file
Source the generated script to make the packages available in the shell.
source env_modules.sh
Expected checks
- Verify loaded packages:
spack find --loadedshould list only the packages you added and their dependencies. - Environment status:
spack env statusreports a clean environment with no mismatches betweenspack.yamlandspack.lock. - Load a specific package:
spack load hdf5(or any added package) should succeed without error. - Check the lock file: After installation, compare the current concretization with the lock using
spack env status --diff. No output indicates a successful, reproducible environment. - Validate a binary or library: Run a simple check, e.g.,
which h5ccorpkg-config --modversion openmpi, to confirm the expected version is present.
Recovery options
- Deactivate: Return to the default Spack context with
spack env deactivate. - Remove the environment: If the environment is no longer needed, delete it (including its
spack.lockand install tree) withspack env rm -y myenv. - Clean stage directories: Free space used during builds with
spack clean -a(use cautiously; this removes all staged source tarballs and build directories). - Revert to a previous lock: If you have a backup of
spack.lock(e.g.,spack.lock.backup), copy it back over the current lock and reinstall:cp spack.lock.backup spack.lockfollowed byspack install --fail-fast.
Limitations and practical verification
Environment isolation relies on activation; shells started outside the activation will not automatically see the environment’s packages unless you manually source the module file or use spack load. Changing the compiler or target architecture after environment creation may trigger reconcretization, potentially altering installed versions. To avoid surprises, keep the same spack.yaml (or regenerate the lock) when you change compilers or architectures.
Before relying on lock‑file reproducibility, confirm your Spack version supports it:
spack --versionEnsure the output is 0.19 or newer. Older versions lack full lockfile support, and reproducibility guarantees are weaker.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.