Tutorial setup

If you have not done the prior sections, you’ll need to set Spack up like this:

git clone https://github.com/spack/spack
. spack/share/spack/setup-env.sh
spack tutorial

See the Basic Installation Tutorial for full details on setup. For more help join us in the #tutorial channel on Slack – get an invitation at spackpm.herokuapp.com

Mirrors and Binary Caches Tutorial

This tutorial will guide you through the process of setting up a source mirror with a binary cache. Source mirrors and binary caches are extremely useful when using spack on a machine without internet access. Source mirrors allow you to fetch source code from a directory on your filesystem instead of accessing the outside internet, and binary caches allow you to install pre-compiled binaries to your spack installation path. Together, these two features can speed up builds when using spack within a larger development team.

We will use the filesystem for the mirrors in this tutorial, but mirrors can also be setup on web servers or s3 buckets – any URL that curl can access can be setup as a Spack mirror.

By default, Spack comes configured with a source mirror in the cloud to increase download reliability. We’ve also already set up a mirror in this tutorial for binary caches. We can see these mirrors are configured

$ spack mirror list
tutorial	file:///mirror (fetch)
tutorial	file:///mirror (push)
spack-public	https://mirror.spack.io

Setting up a source cache mirror

When you run spack install, spack goes out to the internet to grab the source code to build your packages. This works fine on most clusters, but what do you do if the cluster in question doesn’t have access to the outside internet? This could happen for a variety of reasons. Maybe you’re building on a compute node that isn’t connected to the greater internet, or maybe even the whole cluster has been isolated from the internet.

Spack has an easy answer to this – setting up a source mirror. When you use a source mirror, spack checks the mirror for the source code before going to the outside internet.

Building a source mirror is easy. Let’s start with the same simple environment. First, let’s build our software on a computer with external internet access.

$ cd ~
$ mkdir cache-env
mkdir: cannot create directory ‘cache-env’: File exists
$ cd cache-env
$ spack env create -d .
==> Created environment in /home/spack1/cache-env
==> You can activate this environment with:
==>   spack env activate /home/spack1/cache-env
$ spacktivate .
$ # for now, disable fortran support in all packages
$ spack config add "packages:all:variants: ~fortran"
$ spack add macsio+scr
==> Package macsio was already added to /home/spack1/cache-env
$ spack install
==> Concretized macsio+scr
[+]  wkfrisw  macsio@1.1%gcc@7.5.0~exodus~hdf5~ipo+mpi~pdb+scr+silo~szip~typhonio~zfp~zlib build_system=cmake build_type=RelWithDebInfo patches=59479b9 arch=linux-ubuntu18.04-x86_64
[+]  jrlhw77	  ^cmake@3.24.3%gcc@7.5.0~doc+ncurses+ownlibs~qt build_system=generic build_type=Release arch=linux-ubuntu18.04-x86_64
[+]  zcuawli	      ^ncurses@6.3%gcc@7.5.0~symlinks+termlib abi=none build_system=autotools arch=linux-ubuntu18.04-x86_64
[+]  uwlw4wr	      ^openssl@1.1.1s%gcc@7.5.0~docs~shared build_system=generic certs=mozilla arch=linux-ubuntu18.04-x86_64
[+]  aurd37j		  ^ca-certificates-mozilla@2022-10-11%gcc@7.5.0 build_system=generic arch=linux-ubuntu18.04-x86_64
[+]  ncjbxbs	  ^json-cwx@0.12%gcc@7.5.0 build_system=autotools arch=linux-ubuntu18.04-x86_64
[+]  qdpipdf	      ^autoconf@2.69%gcc@7.5.0 build_system=autotools patches=35c4492,7793209,a49dd5b arch=linux-ubuntu18.04-x86_64
[+]  l7j5vdr	      ^automake@1.16.5%gcc@7.5.0 build_system=autotools arch=linux-ubuntu18.04-x86_64
[+]  bdxu4wn	      ^libtool@2.4.7%gcc@7.5.0 build_system=autotools arch=linux-ubuntu18.04-x86_64
[+]  z7d5q4f	      ^m4@1.4.19%gcc@7.5.0+sigsegv build_system=autotools patches=9dc5fbd,bfdffa7 arch=linux-ubuntu18.04-x86_64
[+]  c3uhkcj		  ^diffutils@3.8%gcc@7.5.0 build_system=autotools arch=linux-ubuntu18.04-x86_64
[+]  ltvovvv		      ^libiconv@1.16%gcc@7.5.0 build_system=autotools libs=shared,static arch=linux-ubuntu18.04-x86_64
[+]  e262sf6		  ^libsigsegv@2.13%gcc@7.5.0 build_system=autotools arch=linux-ubuntu18.04-x86_64
[+]  ilnrdw5	  ^openmpi@4.1.4%gcc@7.5.0~atomics~cuda~cxx~cxx_exceptions~gpfs~internal-hwloc~java~legacylaunchers~lustre~memchecker+romio+rsh~singularity+static+vt+wrapper-rpath build_system=autotools fabrics=none schedulers=none arch=linux-ubuntu18.04-x86_64
[+]  5yywrk2	      ^hwloc@2.8.0%gcc@7.5.0~cairo~cuda~gl~libudev+libxml2~netloc~nvml~oneapi-level-zero~opencl+pci~rocm build_system=autotools libs=shared,static arch=linux-ubuntu18.04-x86_64
[+]  p7f5n6q		  ^libpciaccess@0.16%gcc@7.5.0 build_system=autotools arch=linux-ubuntu18.04-x86_64
[+]  ao5w53r		      ^util-macros@1.19.3%gcc@7.5.0 build_system=autotools arch=linux-ubuntu18.04-x86_64
[+]  4aq5aj2		  ^libxml2@2.10.1%gcc@7.5.0~python build_system=autotools arch=linux-ubuntu18.04-x86_64
[+]  nrwxyth		      ^xz@5.2.7%gcc@7.5.0~pic build_system=autotools libs=shared,static arch=linux-ubuntu18.04-x86_64
[+]  l566d6c	      ^numactl@2.0.14%gcc@7.5.0 build_system=autotools patches=4e1d78c,62fc8a8,ff37630 arch=linux-ubuntu18.04-x86_64
[+]  e4esedl	      ^openssh@9.1p1%gcc@7.5.0+gssapi build_system=autotools arch=linux-ubuntu18.04-x86_64
[+]  oc246vi		  ^krb5@1.19.3%gcc@7.5.0+shared build_system=autotools arch=linux-ubuntu18.04-x86_64
[+]  kxbisdg		      ^bison@3.8.2%gcc@7.5.0 build_system=autotools arch=linux-ubuntu18.04-x86_64
[+]  hj6szik		      ^gettext@0.21.1%gcc@7.5.0+bzip2+curses+git~libunistring+libxml2+tar+xz build_system=autotools arch=linux-ubuntu18.04-x86_64
[+]  kawegoy			  ^tar@1.34%gcc@7.5.0 build_system=autotools zip=pigz arch=linux-ubuntu18.04-x86_64
[+]  wod35ny			      ^pigz@2.7%gcc@7.5.0 build_system=makefile arch=linux-ubuntu18.04-x86_64
[+]  fprmzmo			      ^zstd@1.5.2%gcc@7.5.0+programs build_system=makefile compression=none libs=shared,static arch=linux-ubuntu18.04-x86_64
[+]  6pzeykr		  ^libedit@3.1-20210216%gcc@7.5.0 build_system=autotools arch=linux-ubuntu18.04-x86_64
[+]  4ebutg2	      ^perl@5.36.0%gcc@7.5.0+cpanm+shared+threads build_system=generic arch=linux-ubuntu18.04-x86_64
[+]  53fb72w		  ^berkeley-db@18.1.40%gcc@7.5.0+cxx~docs+stl build_system=autotools patches=26090f4,b231fcc arch=linux-ubuntu18.04-x86_64
[+]  cxcreln		  ^bzip2@1.0.8%gcc@7.5.0~debug~pic+shared build_system=generic arch=linux-ubuntu18.04-x86_64
[+]  cpmynty		  ^gdbm@1.23%gcc@7.5.0 build_system=autotools arch=linux-ubuntu18.04-x86_64
[+]  k2lfw6f	      ^pkgconf@1.8.0%gcc@7.5.0 build_system=autotools arch=linux-ubuntu18.04-x86_64
[+]  optdpo7	      ^pmix@4.1.2%gcc@7.5.0~docs+pmi_backwards_compatibility~restful build_system=autotools arch=linux-ubuntu18.04-x86_64
[+]  ule2pil		  ^libevent@2.1.12%gcc@7.5.0+openssl build_system=autotools arch=linux-ubuntu18.04-x86_64
[+]  2hfr5rb	      ^zlib@1.2.13%gcc@7.5.0+optimize+pic+shared build_system=makefile arch=linux-ubuntu18.04-x86_64
[+]  747oqe2	  ^scr@2.0.0%gcc@7.5.0+dtcmp~fortran~ipo+libyogrt async_api=NONE build_system=cmake build_type=RelWithDebInfo cache_base=/dev/shm cntl_base=/dev/shm copy_config=none file_lock=FLOCK resource_manager=SLURM scr_config=scr.conf arch=linux-ubuntu18.04-x86_64
[+]  osciueo	      ^dtcmp@1.1.4%gcc@7.5.0+shared build_system=autotools arch=linux-ubuntu18.04-x86_64
[+]  qo6kgjo		  ^lwgrp@1.0.5%gcc@7.5.0+shared build_system=autotools arch=linux-ubuntu18.04-x86_64
[+]  ty6jvya	      ^libyogrt@1.27%gcc@7.5.0~static build_system=autotools scheduler=slurm arch=linux-ubuntu18.04-x86_64
[+]  kunpxdt		  ^slurm@21-08-8-2%gcc@7.5.0~gtk~hdf5~hwloc~mariadb~pmix+readline~restd build_system=autotools sysconfdir=PREFIX/etc arch=linux-ubuntu18.04-x86_64
[+]  skslu3i		      ^curl@7.85.0%gcc@7.5.0~gssapi~ldap~libidn2~librtmp~libssh~libssh2~nghttp2 build_system=autotools libs=shared,static tls=openssl arch=linux-ubuntu18.04-x86_64
[+]  pqgjrjg		      ^glib@2.74.1%gcc@7.5.0~libmount build_system=generic tracing=none arch=linux-ubuntu18.04-x86_64
[+]  qzq4b7h			  ^libffi@3.4.2%gcc@7.5.0 build_system=autotools arch=linux-ubuntu18.04-x86_64
[+]  2zb5yee			  ^meson@0.63.3%gcc@7.5.0 build_system=python_pip patches=aa6c50d arch=linux-ubuntu18.04-x86_64
[+]  wfr6uxy			      ^py-pip@22.2.2%gcc@7.5.0 build_system=generic arch=linux-ubuntu18.04-x86_64
[+]  opuypru			      ^py-setuptools@65.5.0%gcc@7.5.0 build_system=generic arch=linux-ubuntu18.04-x86_64
[+]  mjnnkeo			      ^py-wheel@0.37.1%gcc@7.5.0 build_system=generic arch=linux-ubuntu18.04-x86_64
[+]  rcjowmo			  ^ninja@1.11.1%gcc@7.5.0 build_system=generic arch=linux-ubuntu18.04-x86_64
[+]  gtbod2d			  ^pcre2@10.39%gcc@7.5.0~jit+multibyte build_system=autotools arch=linux-ubuntu18.04-x86_64
[+]  wvhlgdd			  ^python@3.10.8%gcc@7.5.0+bz2+ctypes+dbm~debug+libxml2+lzma~nis~optimizations+pic+pyexpat+pythoncmd+readline+shared+sqlite3+ssl~tix~tkinter~ucs4+uuid+zlib build_system=generic patches=0d98e93,7d40923,f2fd060 arch=linux-ubuntu18.04-x86_64
[+]  j5k5nzg			      ^expat@2.4.8%gcc@7.5.0+libbsd build_system=autotools arch=linux-ubuntu18.04-x86_64
[+]  qieib3t				  ^libbsd@0.11.5%gcc@7.5.0 build_system=autotools arch=linux-ubuntu18.04-x86_64
[+]  miq3rrs				      ^libmd@1.0.4%gcc@7.5.0 build_system=autotools arch=linux-ubuntu18.04-x86_64
[+]  vmf7qax			      ^sqlite@3.39.4%gcc@7.5.0+column_metadata+dynamic_extensions+fts~functions+rtree build_system=autotools arch=linux-ubuntu18.04-x86_64
[+]  eayyoxj			      ^util-linux-uuid@2.38.1%gcc@7.5.0 build_system=autotools arch=linux-ubuntu18.04-x86_64
[+]  ezuwtot		      ^json-c@0.16%gcc@7.5.0~ipo build_system=cmake build_type=RelWithDebInfo arch=linux-ubuntu18.04-x86_64
[+]  grcwjnc		      ^lz4@1.9.4%gcc@7.5.0 build_system=makefile libs=shared,static arch=linux-ubuntu18.04-x86_64
[+]  qfddwk6		      ^munge@0.5.15%gcc@7.5.0 build_system=autotools localstatedir=PREFIX/var arch=linux-ubuntu18.04-x86_64
[+]  25x4i2l			  ^libgcrypt@1.10.1%gcc@7.5.0 build_system=autotools arch=linux-ubuntu18.04-x86_64
[+]  t3etwuc			      ^libgpg-error@1.46%gcc@7.5.0 build_system=autotools arch=linux-ubuntu18.04-x86_64
[+]  wcuzcqo				  ^gawk@5.1.1%gcc@7.5.0~nls build_system=autotools arch=linux-ubuntu18.04-x86_64
[+]  efr3j4d				      ^gmp@6.2.1%gcc@7.5.0 build_system=autotools libs=shared,static arch=linux-ubuntu18.04-x86_64
[+]  vtov5r3				      ^mpfr@4.1.0%gcc@7.5.0 build_system=autotools libs=shared,static arch=linux-ubuntu18.04-x86_64
[+]  hgpn5sq					  ^texinfo@6.5%gcc@7.5.0 build_system=autotools patches=12f6edb,1732115 arch=linux-ubuntu18.04-x86_64
[+]  2uj3346	      ^pdsh@2.31%gcc@7.5.0+ssh+static_modules build_system=autotools arch=linux-ubuntu18.04-x86_64
[+]  pafegt2	  ^silo@4.10.2%gcc@7.5.0~fortran+fpzip+hdf5+hzip+mpi+pic+shared~silex build_system=autotools patches=3a1e831,7b5a1dc,eb2a3a0,fa050e0 arch=linux-ubuntu18.04-x86_64
[+]  rolnbco	      ^autoconf-archive@2022.02.11%gcc@7.5.0 build_system=autotools patches=139214f arch=linux-ubuntu18.04-x86_64
[+]  ty2m35s	      ^hdf5@1.12.2%gcc@7.5.0~cxx~fortran~hl~ipo~java+mpi+shared~szip~threadsafe+tools api=default build_system=cmake build_type=RelWithDebInfo arch=linux-ubuntu18.04-x86_64
[+]  kgyvtw5	      ^readline@8.1.2%gcc@7.5.0 build_system=autotools arch=linux-ubuntu18.04-x86_64

==> Concretized unzip
[+]  pqjekjj  unzip@6.0%gcc@7.5.0 build_system=makefile arch=linux-ubuntu18.04-x86_64

==> Installing environment /home/spack1/cache-env
==> All of the packages are already installed

Once we’ve created and installed this environment, we can easily upload source code needed to reproduce this build to a mirror. The following command both creates the mirror and uploads the source code for the scr package included in our environment. The -d flag (short for --directory) tells spack where to place the mirrored source code files.

$ spack mirror create -d ~/mirror scr
==> Concretizing input specs
==> Adding package scr@2.0.0 to mirror
==> Summary for mirror in file:///home/spack1/mirror
==> Archive stats:
    1	 already present
    0	 added
    0	 failed to fetch.

We can configure spack to use this source mirror by adding a few lines to your spack.yaml file.

$ spack mirror add mymirror ~/mirror
==> Error: Mirror with name mymirror already exists.

Manually uploading every package in an environment can be tedious. Luckily, when run within an environment, spack mirror create with the --all flag will upload every source used to build the current environment to the specified directory.

$ spack mirror create -d ~/mirror --all
==> Adding package autoconf@2.69 to mirror
==> Adding package autoconf-archive@2022.02.11 to mirror
==> Adding package automake@1.16.5 to mirror
==> Adding package berkeley-db@18.1.40 to mirror
==> Adding package bison@3.8.2 to mirror
==> Adding package bzip2@1.0.8 to mirror
==> Adding package ca-certificates-mozilla@2022-10-11 to mirror
==> Adding package cmake@3.24.3 to mirror
==> Adding package curl@7.85.0 to mirror
==> Adding package diffutils@3.8 to mirror
==> Adding package dtcmp@1.1.4 to mirror
==> Adding package expat@2.4.8 to mirror
==> Adding package gawk@5.1.1 to mirror
==> Adding package gdbm@1.23 to mirror
==> Adding package gettext@0.21.1 to mirror
==> Adding package glib@2.74.1 to mirror
==> Adding package gmp@6.2.1 to mirror
==> Adding package hdf5@1.12.2 to mirror
==> Adding package hwloc@2.8.0 to mirror
==> Adding package json-c@0.16 to mirror
==> Adding package json-cwx@0.12 to mirror
==> Adding package krb5@1.19.3 to mirror
==> Adding package libbsd@0.11.5 to mirror
==> Adding package libedit@3.1-20210216 to mirror
==> Adding package libevent@2.1.12 to mirror
==> Adding package libffi@3.4.2 to mirror
==> Adding package libgcrypt@1.10.1 to mirror
==> Adding package libgpg-error@1.46 to mirror
==> Adding package libiconv@1.16 to mirror
==> Adding package libmd@1.0.4 to mirror
==> Adding package libpciaccess@0.16 to mirror
==> Adding package libsigsegv@2.13 to mirror
==> Adding package libtool@2.4.7 to mirror
==> Adding package libxml2@2.10.1 to mirror
==> Adding package libyogrt@1.27 to mirror
==> Adding package lwgrp@1.0.5 to mirror
==> Adding package lz4@1.9.4 to mirror
==> Adding package m4@1.4.19 to mirror
==> Adding package macsio@1.1 to mirror
==> Adding package meson@0.63.3 to mirror
==> Adding package mpfr@4.1.0 to mirror
==> Adding package munge@0.5.15 to mirror
==> Adding package ncurses@6.3 to mirror
==> Adding package ninja@1.11.1 to mirror
==> Adding package numactl@2.0.14 to mirror
==> Adding package openmpi@4.1.4 to mirror
==> Adding package openssh@9.1p1 to mirror
==> Adding package openssl@1.1.1s to mirror
==> Adding package pcre2@10.39 to mirror
==> Adding package pdsh@2.31 to mirror
==> Adding package perl@5.36.0 to mirror
==> Adding package pigz@2.7 to mirror
==> Adding package pkgconf@1.8.0 to mirror
==> Adding package pmix@4.1.2 to mirror
==> Adding package py-pip@22.2.2 to mirror
==> Adding package py-setuptools@65.5.0 to mirror
==> Adding package py-wheel@0.37.1 to mirror
==> Adding package python@3.10.8 to mirror
==> Adding package readline@8.1.2 to mirror
==> Adding package scr@2.0.0 to mirror
==> Adding package silo@4.10.2 to mirror
==> Adding package slurm@21-08-8-2 to mirror
==> Adding package sqlite@3.39.4 to mirror
==> Adding package tar@1.34 to mirror
==> Adding package texinfo@6.5 to mirror
==> Adding package unzip@6.0 to mirror
==> Adding package util-linux-uuid@2.38.1 to mirror
==> Adding package util-macros@1.19.3 to mirror
==> Adding package xz@5.2.7 to mirror
==> Adding package zlib@1.2.13 to mirror
==> Adding package zstd@1.5.2 to mirror
==> Summary for mirror in file:///home/spack1/mirror
==> Archive stats:
    71	 already present
    0	 added
    0	 failed to fetch.

This directory can be shared between users on a shared filesystem and protected with typical unix file permissions. If you’re making a spack mirror on a shared filesystem, remember to fix the file permissions every time you update the mirror, or update your umask settings so any new files you create have the appropriate permissions. Here you would replace the word spack to the appropriate unix group.

$ umask 750
$ chmod -R g+rs ~/mirror
$ chgrp -R spack ~/mirror
chgrp: invalid group: ‘spack’

As long as spack can read from the mirror directory, spack will attempt to read source packages from the mirror instead of accessing the internet. This can be a huge boon for computers that can’t access the external internet but can access a shared filesystem. If you need to use spack on a system that is isolated from the external internet, you must bundle the whole spack mirror directory and unbundle it on the isolated system. From there, you follow the same steps to use the spack mirror as you would on any computer that can’t access the external internet.

If you need to add more sources to the mirror, you can re-run the command you used to create the mirror. For example, assume we want to add unzip to our environment.

$ spack add unzip
==> Package unzip was already added to /home/spack1/cache-env
$ spack install
==> Installing environment /home/spack1/cache-env
==> All of the packages are already installed

Now that we’ve added unzip, we need to update the mirror.

$ spack mirror create -d ~/mirror --all
==> Adding package autoconf@2.69 to mirror
==> Adding package autoconf-archive@2022.02.11 to mirror
==> Adding package automake@1.16.5 to mirror
==> Adding package berkeley-db@18.1.40 to mirror
==> Adding package bison@3.8.2 to mirror
==> Adding package bzip2@1.0.8 to mirror
==> Adding package ca-certificates-mozilla@2022-10-11 to mirror
==> Adding package cmake@3.24.3 to mirror
==> Adding package curl@7.85.0 to mirror
==> Adding package diffutils@3.8 to mirror
==> Adding package dtcmp@1.1.4 to mirror
==> Adding package expat@2.4.8 to mirror
==> Adding package gawk@5.1.1 to mirror
==> Adding package gdbm@1.23 to mirror
==> Adding package gettext@0.21.1 to mirror
==> Adding package glib@2.74.1 to mirror
==> Adding package gmp@6.2.1 to mirror
==> Adding package hdf5@1.12.2 to mirror
==> Adding package hwloc@2.8.0 to mirror
==> Adding package json-c@0.16 to mirror
==> Adding package json-cwx@0.12 to mirror
==> Adding package krb5@1.19.3 to mirror
==> Adding package libbsd@0.11.5 to mirror
==> Adding package libedit@3.1-20210216 to mirror
==> Adding package libevent@2.1.12 to mirror
==> Adding package libffi@3.4.2 to mirror
==> Adding package libgcrypt@1.10.1 to mirror
==> Adding package libgpg-error@1.46 to mirror
==> Adding package libiconv@1.16 to mirror
==> Adding package libmd@1.0.4 to mirror
==> Adding package libpciaccess@0.16 to mirror
==> Adding package libsigsegv@2.13 to mirror
==> Adding package libtool@2.4.7 to mirror
==> Adding package libxml2@2.10.1 to mirror
==> Adding package libyogrt@1.27 to mirror
==> Adding package lwgrp@1.0.5 to mirror
==> Adding package lz4@1.9.4 to mirror
==> Adding package m4@1.4.19 to mirror
==> Adding package macsio@1.1 to mirror
==> Adding package meson@0.63.3 to mirror
==> Adding package mpfr@4.1.0 to mirror
==> Adding package munge@0.5.15 to mirror
==> Adding package ncurses@6.3 to mirror
==> Adding package ninja@1.11.1 to mirror
==> Adding package numactl@2.0.14 to mirror
==> Adding package openmpi@4.1.4 to mirror
==> Adding package openssh@9.1p1 to mirror
==> Adding package openssl@1.1.1s to mirror
==> Adding package pcre2@10.39 to mirror
==> Adding package pdsh@2.31 to mirror
==> Adding package perl@5.36.0 to mirror
==> Adding package pigz@2.7 to mirror
==> Adding package pkgconf@1.8.0 to mirror
==> Adding package pmix@4.1.2 to mirror
==> Adding package py-pip@22.2.2 to mirror
==> Adding package py-setuptools@65.5.0 to mirror
==> Adding package py-wheel@0.37.1 to mirror
==> Adding package python@3.10.8 to mirror
==> Adding package readline@8.1.2 to mirror
==> Adding package scr@2.0.0 to mirror
==> Adding package silo@4.10.2 to mirror
==> Adding package slurm@21-08-8-2 to mirror
==> Adding package sqlite@3.39.4 to mirror
==> Adding package tar@1.34 to mirror
==> Adding package texinfo@6.5 to mirror
==> Adding package unzip@6.0 to mirror
==> Adding package util-linux-uuid@2.38.1 to mirror
==> Adding package util-macros@1.19.3 to mirror
==> Adding package xz@5.2.7 to mirror
==> Adding package zlib@1.2.13 to mirror
==> Adding package zstd@1.5.2 to mirror
==> Summary for mirror in file:///home/spack1/mirror
==> Archive stats:
    71	 already present
    0	 added
    0	 failed to fetch.

Spack will skip uploading source code packages that are already included in the spack mirror. Mirrors can be shared across different environments, meaning one mirror can house all the source code needed to build your team’s dependencies.

Setting up a binary cache mirror

If you’re going to be setting up a team to use spack as part of their development practice, you’ll run up against the biggest disadvantage to using spack: building all your packages from scratch is slow. Recompiling the software dependencies for a large project can take hours to complete. If every developer is rebuilding their own software stack, that leads to a massive waste of computational resources and a loss of developer productivity.

Spack has two ways to help alleviate this problem: chained spack instances and spack binary caches. For now, we’re going to discuss spack binary caches as a way of solving this issue.

A spack binary cache is made up of spack binary packages. Each spack binary package, ending with a *.spack extension, is a tarball of an installed spack package signed with a gpg signature. When you install a package from a mirror with a binary cache, spack

  • Checks to see if there is a spack binary package that exactly matches the hash of the spec you want to build.
  • If a binary package is found, spack checks to see if the signature on the spack binary package is trusted. If the signature isn’t trusted or no package was found, spack builds the package from source.
  • If the signature is trusted, then spack unzips and relocates the spack package.

For the user, spack binary caches are transparent to use. This is a clear departure from systems like conda, where you need to chose separate workflows for binary and source packages. We’ve already demonstrated using spack binary caches earlier in the tutorial when we set up spack to use a binary mirror. As a reminder, we ran:

$ spack mirror add tutorial /mirror
$ spack buildcache keys --install --trust
==> Fetching file:///mirror/build_cache/_pgp/2C8DD3224EF3573A42BD221FA8E0CA3C1C2ADA2F.pub
gpg: key A8E0CA3C1C2ADA2F: 7 signatures not checked due to missing keys
gpg: key A8E0CA3C1C2ADA2F: public key "Spack Project Official Binaries <maintainers@spack.io>" imported
gpg: Total number processed: 1
gpg:		   imported: 1
gpg: no ultimately trusted keys found
gpg: inserting ownertrust of 6

Building a spack binary cache has some gotchas, but is almost as easy as building a source mirror. We’ll start by making a new environment for ourselves. Since we’re intending to publish to a binary cache, we’ll need to compile all these packages ourselves. This can take some time, so we’ll make a new environment with some packages that compile quickly.

$ cd ~
$ mkdir cache-binary
mkdir: cannot create directory ‘cache-binary’: File exists
$ cd cache-binary
$ spack env create -d .
==> Updating view at /home/spack1/cache-binary/.spack-env/view
==> Created environment in /home/spack1/cache-binary
==> You can activate this environment with:
==>   spack env activate /home/spack1/cache-binary
$ spacktivate .
$ spack add bzip2
==> Package bzip2 was already added to /home/spack1/cache-binary
$ spack add zlib
==> Package zlib was already added to /home/spack1/cache-binary

Before we build anything, we need to modify the following line in our spack configuration file. Then, just to be sure, we’ll initiate a build of this environment and force ourselves to not use any cache.

$ spack config add "config:install_tree:padded_length:128"
$ spack install --no-cache
==> Installing environment /home/spack1/cache-binary
==> Waiting for libiconv-1.16-ltvovvvjgmlvy4m2pn365cy6p5dysajq
==> Installing libiconv-1.16-ltvovvvjgmlvy4m2pn365cy6p5dysajq
==> Using cached archive: /home/spack1/spack/var/spack/cache/_source-cache/archive/e6/e6a1b1b589654277ee790cce3734f07876ac4ccfaecbee8afa0b649cf529cc04.tar.gz
==> No patches needed for libiconv
==> libiconv: Executing phase: 'autoreconf'
==> libiconv: Executing phase: 'configure'
==> libiconv: Executing phase: 'build'
==> libiconv: Executing phase: 'install'
==> libiconv: Successfully installed libiconv-1.16-ltvovvvjgmlvy4m2pn365cy6p5dysajq
  Fetch: 0.02s.  Build: 18.03s.  Total: 18.05s.
[+] /home/spack1/spack/opt/spack/[padded-to-128-chars]/linux-ubuntu18.04-x86_64/gcc-7.5.0/libiconv-1.16-ltvovvvjgmlvy4m2pn365cy6p5dysajq
==> Waiting for zlib-1.2.13-2hfr5rbzefrxf6t6kv4trbsl63puaza6
==> Installing zlib-1.2.13-2hfr5rbzefrxf6t6kv4trbsl63puaza6
==> Using cached archive: /home/spack1/spack/var/spack/cache/_source-cache/archive/b3/b3a24de97a8fdbc835b9833169501030b8977031bcb54b3b3ac13740f846ab30.tar.gz
==> No patches needed for zlib
==> zlib: Executing phase: 'edit'
==> zlib: Executing phase: 'build'
==> zlib: Executing phase: 'install'
==> zlib: Successfully installed zlib-1.2.13-2hfr5rbzefrxf6t6kv4trbsl63puaza6
  Fetch: 0.01s.  Build: 1.89s.	Total: 1.90s.
[+] /home/spack1/spack/opt/spack/[padded-to-128-chars]/linux-ubuntu18.04-x86_64/gcc-7.5.0/zlib-1.2.13-2hfr5rbzefrxf6t6kv4trbsl63puaza6
==> Waiting for diffutils-3.8-c3uhkcjt7tagz2djrd6r2b6b3c2tia3s
==> Installing diffutils-3.8-c3uhkcjt7tagz2djrd6r2b6b3c2tia3s
==> Using cached archive: /home/spack1/spack/var/spack/cache/_source-cache/archive/a6/a6bdd7d1b31266d11c4f4de6c1b748d4607ab0231af5188fc2533d0ae2438fec.tar.xz
==> No patches needed for diffutils
==> diffutils: Executing phase: 'autoreconf'
==> diffutils: Executing phase: 'configure'
==> diffutils: Executing phase: 'build'
==> diffutils: Executing phase: 'install'
==> diffutils: Successfully installed diffutils-3.8-c3uhkcjt7tagz2djrd6r2b6b3c2tia3s
  Fetch: 0.01s.  Build: 25.94s.  Total: 25.95s.
[+] /home/spack1/spack/opt/spack/[padded-to-128-chars]/linux-ubuntu18.04-x86_64/gcc-7.5.0/diffutils-3.8-c3uhkcjt7tagz2djrd6r2b6b3c2tia3s
==> Waiting for bzip2-1.0.8-cxcrelnw64jh6lvhzb6pfar7nijv6us6
==> Installing bzip2-1.0.8-cxcrelnw64jh6lvhzb6pfar7nijv6us6
==> Using cached archive: /home/spack1/spack/var/spack/cache/_source-cache/archive/ab/ab5a03176ee106d3f0fa90e381da478ddae405918153cca248e682cd0c4a2269.tar.gz
==> Ran patch() for bzip2
==> bzip2: Executing phase: 'install'
==> bzip2: Successfully installed bzip2-1.0.8-cxcrelnw64jh6lvhzb6pfar7nijv6us6
  Fetch: 0.01s.  Build: 1.82s.	Total: 1.83s.
[+] /home/spack1/spack/opt/spack/[padded-to-128-chars]/linux-ubuntu18.04-x86_64/gcc-7.5.0/bzip2-1.0.8-cxcrelnw64jh6lvhzb6pfar7nijv6us6
==> Updating view at /home/spack1/cache-binary/.spack-env/view

This configuration change ensures that spack installs all our packages to a path that is at least 128 characters. We need this change because of how spack relocates packages. Relocation consists of the following steps:

  • Search all text files to replace the package builder’s path with your specific local installation path.
  • Modify all the RPATHs in your binaries to point to your specific local installation path. This uses patchelf if the new path is longer than the old path, but otherwise uses a faster implementation in python.
  • Search all binaries to replace hard-coded C strings of the package builder’s path with your specific local installation path.

Adding padding ensures that any paths hard-coded as C strings in our binaries will be large enough to hold our user’s eventual install path. We advise picking 128 because longer strings occasionally cause compilation problems with some software packages. If the user’s install path is too long, spack will give you a warning. All scripts and and RPATHs will still be properly relocated, but C strings within any binaries will not be modified. Depending on the package, this may cause problems when you try and use the software.

We also need to create a gpg key to sign all our packages. You should back up the secret and public keys to a secure place so they can be re-used in the future.

$ spack gpg create "My Name" "<my.email@my.domain.com>"
gpg: key 275CEB7579A7C93D marked as ultimately trusted
gpg: directory '/home/spack1/spack/opt/spack/gpg/openpgp-revocs.d' created
gpg: revocation certificate stored as '/home/spack1/spack/opt/spack/gpg/openpgp-revocs.d/9573D38E4D51534C2663AAAB275CEB7579A7C93D.rev'
$ mkdir ~/private_gpg_backup
mkdir: cannot create directory ‘/home/spack1/private_gpg_backup’: File exists
$ cp ~/spack/opt/spack/gpg/*.gpg ~/private_gpg_backup
$ cp ~/spack/opt/spack/gpg/pubring.* ~/mirror

With this setup done, we’re ready to fill a binary cache with binary packages. Binary packages are attached to an existing source mirror. We follow the same steps we used for the source mirror – making an environment, creating the source mirror, and building the packages to a spack installation with our padded path. To upload a spec to a binary cache, simply use the command spack buildcache create --only=package spec. Here we use the spack find --format command that we will discuss in more detail in the Scripting section of the tutorial to operate on all specs in the environment.

$ spack buildcache create --allow-root --force -d ~/mirror --only=package $(spack find --format /{hash})
==> Pushing binary packages to file:///home/spack1/mirror/build_cache
gpg: checking the trustdb
gpg: marginals needed: 3  completes needed: 1  trust model: pgp
gpg: depth: 0  valid:	2  signed:   0	trust: 0-, 0q, 0n, 0m, 0f, 2u

Voila, done! Our spack mirror has now been augmented with a binary cache. This cache can be used on systems without external internet access, just like with a spack source mirror. As always, remember to update the file permissions after updating the mirror.

$ umask 750
$ chmod -R g+rs ~/mirror
$ chgrp -R spack ~/mirror
chgrp: invalid group: ‘spack’

Though it’s outside the scope of this tutorial, spack mirrors and build caches can also be hosted over https:// and s3:// as well. Consult the spack documentation for more information on how to do this.

Before a user can use this binary cache, they will need to make sure that they trust all the packages listed in the binary cache. If you’re sharing files between trusted users on a filesystem, you can do this with the following command:

$ spack buildcache keys --install --trust --force
==> Fetching file:///mirror/build_cache/_pgp/2C8DD3224EF3573A42BD221FA8E0CA3C1C2ADA2F.pub
gpg: key A8E0CA3C1C2ADA2F: 7 signatures not checked due to missing keys
gpg: key A8E0CA3C1C2ADA2F: "Spack Project Official Binaries <maintainers@spack.io>" not changed
gpg: Total number processed: 1
gpg:		  unchanged: 1

Together, this means download all the keys on the binary cache and trust them. Have your users run the above command on a new spack instance before they initiate a build.

Bootstrapping Mirrors

In order to run Spack on an airgapped system or aywhere else without internet access, you also need to install clingo as a dependency of the concretizer. While you can always install clingo through your favorite method, we like to think Spack is your favorite install mechanism for everything, and we have convenient ways to bootstrap clingo on airgapped systems.

Spack can prepare a bootstrap mirror for either source or binary mirror for bootstrapping via the spack bootstrap mirror command. We will focus on binary bootstrapping for this tutorial.

$ spack bootstrap mirror --binary-packages ~/mirror
==> Adding "clingo-bootstrap@spack+python %gcc target=x86_64" and dependencies to the mirror at /home/spack1/mirror/bootstrap_cache
==> Adding "gnupg@2.3: %gcc target=x86_64" and dependencies to the mirror at /home/spack1/mirror/bootstrap_cache
==> Adding "patchelf@0.13.1: %gcc target=x86_64" and dependencies to the mirror at /home/spack1/mirror/bootstrap_cache
==> Adding "gnuconfig" and dependencies to the mirror at /home/spack1/mirror/bootstrap_cache
==> Adding binary packages from "https://github.com/spack/spack-bootstrap-mirrors/releases/download/v0.4/bootstrap-buildcache.tar.gz" to the mirror at /home/spack1/mirror/bootstrap_cache

To register the mirror on the platform where it's supposed to be used, move "/home/spack1/mirror" to its final location and run the following command(s):

  % spack bootstrap add --trust local-sources <final-path>/metadata/sources
  % spack bootstrap add --trust local-binaries <final-path>/metadata/binaries

Using the instructions printed from Spack, you can register the new bootstrap sources on the airgapped system.

Cache Summary

If you’re using spack within a development team, consider setting up source mirrors with binary caches. Source mirrors will let you replicate a spack environment on a machine without external internet access, and binary mirrors free you from the burden of recompiling everything from scratch and save you development time. Spack mirrors also provide the mechanism to bring Spack’s internal dependencies over to any machine without external internet access.