Skip to content

Creating a spack.yaml file

Environment files should be called spack.yaml

You should save your environment files using the exact name spack.yaml, otherwise Spack may throw an error.

The simplest use of a Spack environment would be to collate a selection of Spack packages we need simultaneously into one simple environment we can activate (as opposed to running module load commands for each individual application). This could be useful for reproducibility and consistency or for sharing your environment with others.

Let's use a selection of some of the Spack package variants we installed during the Spack Custom Scopes tutorial as a starting point:

  • Gromacs with Open MPI and PLUMED support
  • LAMMPS with KOKKOS and Open MPI
  • Nano 6.3

Below is a spack.yaml file which you can also find in the spack-config-templates repository inside the environment-templates sub-directory for the version of Spack you are using - called spack-only.yaml. Either copy and paste the below or grab the example from the repository, make sure it is named spack.yaml and save it into a directory of your choice for your environment. For the purposes of this tutorial we will save it as:

/data/scratch/${USER}/spack-environments/spack-only/spack.yaml

If this directory does not exist, create it first:

mkdir -p /data/scratch/${USER}/spack-environments/spack-only

The Spack environment itself will be stored in the same location as the spack.yaml file (/data/scratch/${USER}/spack-environments/spack-only). You may wish to store your own environments in an alternative location if you need to keep them for longer. Please refer back to the Configuration overview page for further information.

Contents of spack.yaml (click to expand)
spack:
  compilers:
  - compiler:
      spec: gcc@=11.4.1
      paths:
        cc: /bin/gcc
        cxx: /bin/g++
        f77: /bin/gfortran
        fc: /bin/gfortran
      flags: {}
      operating_system: rocky9
      target: x86_64
      modules: []
      environment: {}
      extra_rpaths: []
  - compiler:
      spec: gcc@=12.2.0
      paths:
        cc: /share/apps/rocky9/spack/apps/linux-rocky9-x86_64_v4/gcc-11.4.1/gcc/12.2.0-6frskzg/bin/gcc
        cxx: /share/apps/rocky9/spack/apps/linux-rocky9-x86_64_v4/gcc-11.4.1/gcc/12.2.0-6frskzg/bin/g++
        f77: /share/apps/rocky9/spack/apps/linux-rocky9-x86_64_v4/gcc-11.4.1/gcc/12.2.0-6frskzg/bin/gfortran
        fc: /share/apps/rocky9/spack/apps/linux-rocky9-x86_64_v4/gcc-11.4.1/gcc/12.2.0-6frskzg/bin/gfortran
      flags: {}
      operating_system: rocky9
      target: x86_64
      modules: []
      environment: {}
      extra_rpaths: []
  concretizer:
    unify: true
  config:
    install_tree:
      root: /data/scratch/${USER}/spack/apps
      projections:
        ^mpi: '{architecture}/{compiler.name}-{compiler.version}/{name}/{version}-{^mpi.name}-{^mpi.version}-{hash:7}'
        all: '{architecture}/{compiler.name}-{compiler.version}/{name}/{version}-{hash:7}'
    license_dir: /data/scratch/${USER}/spack/licenses
    source_cache: /data/scratch/${USER}/spack/cache
  packages:
    all:
      target: [x86_64_v4]
    openmpi:
      buildable: false
      prefer:
      - '+gpfs'
      externals:
      - spec: "openmpi@5.0.3%gcc@12.2.0"
        prefix: /share/apps/rocky9/general/libs/openmpi/gcc/12.2.0/5.0.3
    ucx:
      buildable: false
      externals:
      - spec: "ucx@1.16.0%gcc@12.2.0"
        prefix: /share/apps/rocky9/general/libs/ucx/gcc/12.2.0/1.16.0
  specs:
    - gromacs +plumed ^openmpi ^fftw+openmp
    - lammps +kokkos ^openmpi
    - nano@6.3
  upstreams:
    apocrita:
      install_tree: /share/apps/rocky9/spack/apps
  view:
    default:
      root: .spack-env/view

It might look like a lot, but let's first list all the main sub-sections:

  • compilers - this section contains the same thing you might find in a compilers.yaml file for a custom configuration scope. However, we have only defined the GCC compilers in the spack.yaml file above as that is all we are going to use for this environment
  • concretizer - this is explained in more detail in the official Spack documentation. Most users will want to leave this as unify: true
  • config - this section contains the same thing you might find in a config.yaml file for a custom configuration scope
  • packages - this section contains the same thing you might find in a packages.yaml file for a custom configuration scope
  • specs - this is a list of Spack packages that you would like to be present in your environment. Essentially it is a list of what you might put after a spack install command when running an installation command manually as we did for our personal variants in the Spack config scopes tutorial. You should add any specific versions, variants etc. for each package as required
  • upstreams - this section contains the same thing you might find in an upstreams.yaml file for a custom configuration scope
  • view - this defines how and where your environment view will be created. For now, you will probably want to stick to just the default as written. Environment views are explained in more detail a little later on in this tutorial.

You may have noticed that there is no section that is equivalent to a modules.yaml file. Often, you don't actually need to create and load private modules when using Spack environments. Instead, activating the environment will perform the same function as loading modules, which will become clearer as this tutorial progresses.