Guide
Installing OpenFOAM with Spack Using Multiple Compilers and MPI Variants
Learn how to build OpenFOAM with Spack across gcc/clang and OpenMPI/MPICH, verify each install, and recover from common build failures.
Published by Tasadduq Burney
23 Oct 2025, 00:27 UTC
3 min25.4K views0

Desired Outcome
Build and install OpenFOAM twice with Spack, once using gcc and OpenMPI, and once using clang and MPICH. Each build must be isolated in its own prefix, and the runtime environment must load the correct compiler and MPI libraries.
Prerequisites
- Spack 0.20 or newer installed in the user’s home directory (
~/.spack). - At least two compilers available on the system and discoverable by Spack. Typical commands to install them on a Linux system:
Spack will auto‑detect these after a# Example for Ubuntu sudo apt-get install gcc-12 g++-12 clang-15spack compiler find. - At least two MPI implementations installed and discoverable by Spack. For example:
# Example for Ubuntu sudo apt-get install openmpi-bin libopenmpi-dev mpich libmpich-dev - Administrative access to install packages if the system requires it, otherwise use user‑space install.
- Sufficient disk space in
~/.spack/opt/spackfor two separate OpenFOAM prefixes.
Procedure
- Verify compiler catalog
Check thatspack compiler findgcc@12andclang@15appear. If missing, add them manually withspack compiler add. - Verify MPI catalog
Ensure thatspack spec mpichmpich@3.4.2andopenmpi@4.1.6are resolvable. - Install OpenFOAM with gcc and OpenMPI
This creates a prefix underspack install openfoam@develop%gcc ^openmpi@4.1.6~/.spack/opt/spack/.../openfoam@develop%gcc^openmpi@4.1.6. - Install OpenFOAM with clang and MPICH
spack install openfoam@develop%clang ^mpich@3.4.2 - Confirm both installs exist
You should see two entries with distinct prefixes.spack find openfoam - Load the first spec and check environment
The output should bespack load openfoam@develop%gcc ^openmpi@4.1.6 echo $SPACK_CCgcc. - Run a simple OpenFOAM test case
Verify that the MPI implementation matches the spec by inspectingcd $HOME/OpenFOAM-$(spack find -l openfoam | grep -m1 '^openfoam' | awk '{print $2}')/tutorials/liggghts/... # Example command to launch with mpirun mpirun -np 1 simpleFoamldd $(which simpleFoam).
Expected Checks
- Installation prefixes:
~/.spack/opt/spack/.../openfoam@develop%gcc^openmpi@4.1.6and.../openfoam@develop%clang^mpich@3.4.2should exist. - Environment modules: After
spack load,module listshould show the correct compiler and MPI paths. - Runtime linking:
ldd $(which simpleFoam)should listlibmpi.sofrom the chosen MPI implementation. - Test case execution: The OpenFOAM case should finish without errors and the output logs should reference the expected MPI implementation.
Recovery Options
- Build failure: Inspect the build log at
~/.spack/build-env//build.logto locate compile or link errors. - Conflicting specs: If Spack picks the wrong compiler, explicitly name the compiler in the spec string:
gcc@12orclang@15. - Clean and retry: Remove the problematic build with
spack clean -aand re‑run thespack installcommand. - Isolate build directories: Adjust
build_stage:pathsin~/.spack/config.yamlto avoid clashes when building many specs concurrently. - Reduce parallelism: Use
spack install --jobs 1if the system runs out of memory or CPU during the build.
By following these steps, you achieve reproducible, isolated builds of OpenFOAM with different compiler and MPI combinations, and you have clear diagnostics and recovery paths for common build issues.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.