Using Spack to Build Packages

The Spack Package Manager

Spack is a tool for building and installing software used by researchers in science, math, engineering, and related fields.  At this point Spack can be used to install well over 7000 packages.  The HPC support team at ISU often uses Spack to install packages requested by users on our HPC clusters.  The Spack system has matured to the point where really anyone can install Spack packages themselves. While the HPC support team will gladly install packages when requested, knowing how to use Spack to install packages yourself will give you much greater control over the software you use.

This guide will give a quick introduction to Spack, show how you to install and configure it, then show how you can easily build and deploy packages for you or your team.

Why Use Spack?

Anyone who has had to build software on Linux knows that the variety of compilers, build systems, and package dependencies make building software very complicated to do correctly.  Todd Gamblin and his team at Lawrence Livermore Labs created Spack to give package installers a consistent framework that could be used for all packages, despite the differences in compilers, build environments, and customizations involved. In Spack, each package has a set of detailed instructions called a “recipe” that specifies where to download source code, the build tools to use, required software dependencies, and possible build variations.  Also, Spack recipes are updated frequently by experts that incorporate the best practices for building the application. As you will see, Spack can be used to precisely define how to build a package along with all of its dependencies, so you can easily communicate to others how the package was built.

In the following sections we will show two scenarios for building packages with Spack:

For the first scenario, we'll show how you can get up to speed quickly with Spack for personal use on your local Linux system or the HPC cluster.  Except for some module settings, most of the Spack configuration settings will use defaults.

In the second scenario we will go deeper into customizing your Spack configuration by adding a custom compiler (Intel OneAPI), and expanding what packages can be used to satisfy requirements for MPI and BLAS/LAPACK.   

Installing Spack for Individual Use with Default Settings

Step 1: System Prerequisites:  Install Base Development Tools

There are several software development prerequisites you must have installed before you can start building packages.  At a minimum you will need: 

  • A compiler such as GCC.  The default GCC and related development utilities available on most Linux distros is sufficient.
  • Gnu Autotools (the make utility is essential).
  • The Git source code manager client, git.
  • A module environment such as Lmod. 
     

See the Spack System Prequisites for more details. 

Step 2:  Clone the Spack Git Repository from Github

On a personal system you can just install spack under your home directory:

git clone https://github.com/spack/spack.git

On the Nova cluster, you will want to install Spack into a work directory location. For example:

cd /work/research-group     (Use your preferred work directory location)
 
git clone https://github.com/spack/spack.git

By default the software will be installed in a directory called “spack”.

Step 3:  Source the Spack setup-env Script 

You will need to source the Spack setup-env.sh script to your environment correctly.   The path to the setup script is <install_root>/spack/share/spack/setup-env.sh.  For example:

source /work/research-group/spack/share/spack/setup-env.sh

This will add the Spack program to your PATH variable and set the SPACK_ROOT environment variable.  To see the value of $SPACK_ROOT, do:

echo $SPACK_ROOT

At this point, you can begin using the Spack command to install packages.

The spack Command

Nearly everything you need to do with Spack is done with the spack command. To see a summary of the most common commands, do this:

spack --help

Or, to see all possible command options, do this:

spack help --all

Listing Available Packages

The first thing we usually need to do with Spack is figure out what packages Spack knows how to build. If we want to see the entire list of packages, we can do this:

$ spack list

This will return a list (not shown here) of over 7000 applications so it may take a few moments to return the list. But if you examine the list, you will be see a remarkably diverse set of packages available. (Note that Python module name begin with “py-” and R modules begin with “r-”.)

You can also search for packages. Let's say you want to build the PETSc package. To see if that's available in Spack, do:

$ spack list petsc
 
petsc py-petsc4py
 
==> 2 packages
 

In this case, there are two packages available with "petsc" in their name: petsc itself, and the Python module for PETSc called py-petsc4py.

Getting Info About a Package

To see more details about a package, you can do:

spack info <package-name>

The info for a package is very important. Along with describing what the package does and who is reponsible for it, Spack shows the available versions, packages that this package depend on, and any build variations. You should always examine the package info to make sure you're including the build right options for your needs. We'll see how to make use of these options later under Choosing Build Variants.

Installing a Package

Installing a package is usually very straightforward. To install PETSc, all we really need to do is:

spack install petsc

Even though the above command is very simple, it is sufficient to build and install PETSc and any prerequsite packages. There additional options you can provide, but we will cover them in the section on Package Specifications.

Loading the Module for the Package

Once you've built a package, you will need to load the environment module for the package to make it active. We will explain how to set up the module environment on your Linux system a bit later, but once it is set up, all you need to load the module is:

module load petsc

Configuring Spack for Your Environment

The amount of configuration you need to give to Spack depends a lot on where you are planning to build the software.  Building packages on your own computer is usually much simpler that building packages to run on a large compute cluster.  We'll discuss the configuration you need based on a couple of different scenarios where you plan to build and run.   The configuration setup usually consists of a few different details:

  1. Specifying a particular compiler and version
  2. Setting the preferred target build architecture.
  3. Setting a preferred MPI implementation.
  4. Specifying any pre-installed applications.

 

First Scenario: Installing Applications on Your Local Computer

Installing applications on your local computer is usually very straightforward because we can assume that:

  • You have a fairly recent version of Linux installed, such as RHEL 8 or 9, or Ubuntu 18 or 20.
  • You have a modern compiler version such as GCC 8.3 or later, or the Intel OneAPI compilers.
  • You won’t be using a job scheduler such as Slurm.
  • You’ll be installing applications under the path where Spack is installed, usually under the spack folder in your home directory.

Installing a Module Environment

In this first scenari the only thing you will usually need to add is a "module" environemt.  A module envronment is a tool that allows you add the necessary paths and environment variables for running the package to your working session.   We recommend using the Lmod module environment.

WIth Ubuntu Linux, you can install Lmod like so:

apt install lmod

Red Hat based Linux such as Centos or AlmaLinux, the Lmod environment is obtained from the EPEL packages.  See EPEL for more information on how to set up this repository.  Once, set up, you can add the Lmod packages like so:

yum install Lmod