Tutorial setup

If you have not done the prior sections, you’ll need to start the docker image:

docker run -it ghcr.io/spack/tutorial:cineca23

and then set Spack up like this:

git clone --depth=100 --branch=releases/v0.19 https://github.com/spack/spack
. spack/share/spack/setup-env.sh
spack tutorial -y
spack bootstrap now
spack compiler find

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

Scripting with Spack

This tutorial introduces advanced Spack features related to scripting. Specifically, we will show you how to write scripts using spack find and spack python. Earlier sections of the tutorial demonstrated using spack find to list and search installed packages. The spack python command gives you access to all of Spack’s internal APIs, allowing you to write more complex queries, for example.

Since Spack has an extensive API, we’ll only scratch the surface here. We’ll give you enough information to start writing your own scripts and to find what you need, with a little digging.

Setting up the tutorial

Before proceeding, let’s ensure the outputs for this segment are reasonable. Since you may have a lot of packages installed from earlier sections of the tutorial, we want to perform a little cleanup.

Let’s remove gcc@8.3.0 and re-install hdf5 and zlib@clang using the following commands:

$ spack uninstall -ay
==> Successfully uninstalled gcc@8.4.0%gcc@7.5.0~binutils+bootstrap~graphite~nvptx~piclibs~profiled~strip build_system=autotools build_type=RelWithDebInfo languages=c,c++,fortran patches=98a9c96 arch=linux-ubuntu18.04-x86_64/uobbegc
==> Successfully uninstalled gawk@5.1.1%gcc@7.5.0~nls build_system=autotools arch=linux-ubuntu18.04-x86_64/dco3nzd
==> Successfully uninstalled automake@1.16.5%gcc@7.5.0 build_system=autotools arch=linux-ubuntu18.04-x86_64/rxjqnjw
==> Successfully uninstalled autoconf@2.69%gcc@7.5.0 build_system=autotools patches=35c4492,7793209,a49dd5b arch=linux-ubuntu18.04-x86_64/btoteei
==> Successfully uninstalled texinfo@6.5%gcc@7.5.0 build_system=autotools patches=12f6edb,1732115 arch=linux-ubuntu18.04-x86_64/hdyi7fu
==> Successfully uninstalled libtool@2.4.7%gcc@7.5.0 build_system=autotools arch=linux-ubuntu18.04-x86_64/bdxu4wn
==> Successfully uninstalled diffutils@3.8%gcc@7.5.0 build_system=autotools arch=linux-ubuntu18.04-x86_64/c3uhkcj
==> Successfully uninstalled pkgconf@1.8.0%gcc@7.5.0 build_system=autotools arch=linux-ubuntu18.04-x86_64/k2lfw6f
==> Successfully uninstalled mpc@1.1.0%gcc@7.5.0 build_system=autotools libs=shared,static arch=linux-ubuntu18.04-x86_64/l5bs2ru
==> Successfully uninstalled perl@5.36.0%gcc@7.5.0+cpanm+shared+threads build_system=generic arch=linux-ubuntu18.04-x86_64/eydiwim
==> Successfully uninstalled m4@1.4.19%gcc@7.5.0+sigsegv build_system=autotools patches=9dc5fbd,bfdffa7 arch=linux-ubuntu18.04-x86_64/z7d5q4f
==> Successfully uninstalled libiconv@1.16%gcc@7.5.0 build_system=autotools libs=shared,static arch=linux-ubuntu18.04-x86_64/ltvovvv
==> Successfully uninstalled mpfr@3.1.6%gcc@7.5.0 build_system=autotools libs=shared,static patches=7a6dd71 arch=linux-ubuntu18.04-x86_64/3brr36e
==> Successfully uninstalled gdbm@1.23%gcc@7.5.0 build_system=autotools arch=linux-ubuntu18.04-x86_64/cpmynty
==> Successfully uninstalled bzip2@1.0.8%gcc@7.5.0~debug~pic+shared build_system=generic arch=linux-ubuntu18.04-x86_64/cxcreln
==> Successfully uninstalled libsigsegv@2.13%gcc@7.5.0 build_system=autotools arch=linux-ubuntu18.04-x86_64/e262sf6
==> Successfully uninstalled berkeley-db@18.1.40%gcc@7.5.0+cxx~docs+stl build_system=autotools patches=26090f4,b231fcc arch=linux-ubuntu18.04-x86_64/53fb72w
==> Successfully uninstalled zlib@1.2.13%gcc@7.5.0+optimize+pic+shared build_system=makefile arch=linux-ubuntu18.04-x86_64/2hfr5rb
==> Successfully uninstalled gmp@6.2.1%gcc@7.5.0 build_system=autotools libs=shared,static arch=linux-ubuntu18.04-x86_64/iowhen4
==> Warning: Inconsistent state! Dependent cpmyntybsnm3se7lfnvqosbiqzrcg2kq of kgyvtw5h6mi5nms32x3vzgbw32ymmfwa not in DB
==> Warning: Inconsistent state! Dependent eydiwimku2cctu6dsom3yvfxxjmqyr4w of kgyvtw5h6mi5nms32x3vzgbw32ymmfwa not in DB
==> Warning: Inconsistent state! Dependent btoteeiz74epw4kulm7aecae4cxraxua of kgyvtw5h6mi5nms32x3vzgbw32ymmfwa not in DB
==> Warning: Inconsistent state! Dependent rxjqnjw5jkrbym5x4yshogm3f262crre of kgyvtw5h6mi5nms32x3vzgbw32ymmfwa not in DB
==> Successfully uninstalled readline@8.1.2%gcc@7.5.0 build_system=autotools arch=linux-ubuntu18.04-x86_64/kgyvtw5
==> Successfully uninstalled ncurses@6.3%gcc@7.5.0~symlinks+termlib abi=none build_system=autotools arch=linux-ubuntu18.04-x86_64/zcuawli
$ spack compiler rm gcc@8.4.0
==> Removed compiler gcc@8.4.0
$ spack install hdf5
==> Installing pkgconf-1.8.0-k2lfw6fht6zvsfihvx3avihboi3uv455
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64-gcc-7.5.0-pkgconf-1.8.0-k2lfw6fht6zvsfihvx3avihboi3uv455.spec.json.sig
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64/gcc-7.5.0/pkgconf-1.8.0/linux-ubuntu18.04-x86_64-gcc-7.5.0-pkgconf-1.8.0-k2lfw6fht6zvsfihvx3avihboi3uv455.spack
==> Extracting pkgconf-1.8.0-k2lfw6fht6zvsfihvx3avihboi3uv455 from binary cache
==> pkgconf: Successfully installed pkgconf-1.8.0-k2lfw6fht6zvsfihvx3avihboi3uv455
  Fetch: 0.14s.	 Build: 0.24s.	Total: 0.38s.
[+] /home/spack/spack/opt/spack/linux-ubuntu18.04-x86_64/gcc-7.5.0/pkgconf-1.8.0-k2lfw6fht6zvsfihvx3avihboi3uv455
==> Installing ca-certificates-mozilla-2022-10-11-aurd37jjebxaqzs73saip5v6hgfu3foj
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64-gcc-7.5.0-ca-certificates-mozilla-2022-10-11-aurd37jjebxaqzs73saip5v6hgfu3foj.spec.json.sig
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64/gcc-7.5.0/ca-certificates-mozilla-2022-10-11/linux-ubuntu18.04-x86_64-gcc-7.5.0-ca-certificates-mozilla-2022-10-11-aurd37jjebxaqzs73saip5v6hgfu3foj.spack
==> Extracting ca-certificates-mozilla-2022-10-11-aurd37jjebxaqzs73saip5v6hgfu3foj from binary cache
==> ca-certificates-mozilla: Successfully installed ca-certificates-mozilla-2022-10-11-aurd37jjebxaqzs73saip5v6hgfu3foj
  Fetch: 0.03s.	 Build: 0.22s.	Total: 0.25s.
[+] /home/spack/spack/opt/spack/linux-ubuntu18.04-x86_64/gcc-7.5.0/ca-certificates-mozilla-2022-10-11-aurd37jjebxaqzs73saip5v6hgfu3foj
==> Installing berkeley-db-18.1.40-53fb72wziswrec6tkbjpowshkul2nsk4
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64-gcc-7.5.0-berkeley-db-18.1.40-53fb72wziswrec6tkbjpowshkul2nsk4.spec.json.sig
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64/gcc-7.5.0/berkeley-db-18.1.40/linux-ubuntu18.04-x86_64-gcc-7.5.0-berkeley-db-18.1.40-53fb72wziswrec6tkbjpowshkul2nsk4.spack
==> Extracting berkeley-db-18.1.40-53fb72wziswrec6tkbjpowshkul2nsk4 from binary cache
==> berkeley-db: Successfully installed berkeley-db-18.1.40-53fb72wziswrec6tkbjpowshkul2nsk4
  Fetch: 0.03s.	 Build: 0.29s.	Total: 0.32s.
[+] /home/spack/spack/opt/spack/linux-ubuntu18.04-x86_64/gcc-7.5.0/berkeley-db-18.1.40-53fb72wziswrec6tkbjpowshkul2nsk4
==> Installing libiconv-1.16-ltvovvvjgmlvy4m2pn365cy6p5dysajq
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64-gcc-7.5.0-libiconv-1.16-ltvovvvjgmlvy4m2pn365cy6p5dysajq.spec.json.sig
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64/gcc-7.5.0/libiconv-1.16/linux-ubuntu18.04-x86_64-gcc-7.5.0-libiconv-1.16-ltvovvvjgmlvy4m2pn365cy6p5dysajq.spack
==> Extracting libiconv-1.16-ltvovvvjgmlvy4m2pn365cy6p5dysajq from binary cache
==> libiconv: Successfully installed libiconv-1.16-ltvovvvjgmlvy4m2pn365cy6p5dysajq
  Fetch: 0.03s.	 Build: 0.26s.	Total: 0.28s.
[+] /home/spack/spack/opt/spack/linux-ubuntu18.04-x86_64/gcc-7.5.0/libiconv-1.16-ltvovvvjgmlvy4m2pn365cy6p5dysajq
==> Installing zlib-1.2.13-2hfr5rbzefrxf6t6kv4trbsl63puaza6
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64-gcc-7.5.0-zlib-1.2.13-2hfr5rbzefrxf6t6kv4trbsl63puaza6.spec.json.sig
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64/gcc-7.5.0/zlib-1.2.13/linux-ubuntu18.04-x86_64-gcc-7.5.0-zlib-1.2.13-2hfr5rbzefrxf6t6kv4trbsl63puaza6.spack
==> Extracting zlib-1.2.13-2hfr5rbzefrxf6t6kv4trbsl63puaza6 from binary cache
==> zlib: Successfully installed zlib-1.2.13-2hfr5rbzefrxf6t6kv4trbsl63puaza6
  Fetch: 0.02s.	 Build: 0.22s.	Total: 0.25s.
[+] /home/spack/spack/opt/spack/linux-ubuntu18.04-x86_64/gcc-7.5.0/zlib-1.2.13-2hfr5rbzefrxf6t6kv4trbsl63puaza6
==> Installing libsigsegv-2.13-e262sf6ptgnmyu7zkflzp3ycbdfqz7hf
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64-gcc-7.5.0-libsigsegv-2.13-e262sf6ptgnmyu7zkflzp3ycbdfqz7hf.spec.json.sig
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64/gcc-7.5.0/libsigsegv-2.13/linux-ubuntu18.04-x86_64-gcc-7.5.0-libsigsegv-2.13-e262sf6ptgnmyu7zkflzp3ycbdfqz7hf.spack
==> Extracting libsigsegv-2.13-e262sf6ptgnmyu7zkflzp3ycbdfqz7hf from binary cache
==> libsigsegv: Successfully installed libsigsegv-2.13-e262sf6ptgnmyu7zkflzp3ycbdfqz7hf
  Fetch: 0.03s.	 Build: 0.24s.	Total: 0.26s.
[+] /home/spack/spack/opt/spack/linux-ubuntu18.04-x86_64/gcc-7.5.0/libsigsegv-2.13-e262sf6ptgnmyu7zkflzp3ycbdfqz7hf
==> Installing util-macros-1.19.3-ao5w53rpwhgfruk2zrqcehlov5slestd
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64-gcc-7.5.0-util-macros-1.19.3-ao5w53rpwhgfruk2zrqcehlov5slestd.spec.json.sig
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64/gcc-7.5.0/util-macros-1.19.3/linux-ubuntu18.04-x86_64-gcc-7.5.0-util-macros-1.19.3-ao5w53rpwhgfruk2zrqcehlov5slestd.spack
==> Extracting util-macros-1.19.3-ao5w53rpwhgfruk2zrqcehlov5slestd from binary cache
==> util-macros: Successfully installed util-macros-1.19.3-ao5w53rpwhgfruk2zrqcehlov5slestd
  Fetch: 0.03s.	 Build: 0.23s.	Total: 0.26s.
[+] /home/spack/spack/opt/spack/linux-ubuntu18.04-x86_64/gcc-7.5.0/util-macros-1.19.3-ao5w53rpwhgfruk2zrqcehlov5slestd
==> Installing xz-5.2.7-nrwxythffgfumjv7skmgdjtxaunlmqau
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64-gcc-7.5.0-xz-5.2.7-nrwxythffgfumjv7skmgdjtxaunlmqau.spec.json.sig
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64/gcc-7.5.0/xz-5.2.7/linux-ubuntu18.04-x86_64-gcc-7.5.0-xz-5.2.7-nrwxythffgfumjv7skmgdjtxaunlmqau.spack
==> Extracting xz-5.2.7-nrwxythffgfumjv7skmgdjtxaunlmqau from binary cache
==> xz: Successfully installed xz-5.2.7-nrwxythffgfumjv7skmgdjtxaunlmqau
  Fetch: 0.03s.	 Build: 0.28s.	Total: 0.31s.
[+] /home/spack/spack/opt/spack/linux-ubuntu18.04-x86_64/gcc-7.5.0/xz-5.2.7-nrwxythffgfumjv7skmgdjtxaunlmqau
==> Installing zstd-1.5.2-fprmzmoubdjdr663rprctp2tuqiv5apu
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64-gcc-7.5.0-zstd-1.5.2-fprmzmoubdjdr663rprctp2tuqiv5apu.spec.json.sig
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64/gcc-7.5.0/zstd-1.5.2/linux-ubuntu18.04-x86_64-gcc-7.5.0-zstd-1.5.2-fprmzmoubdjdr663rprctp2tuqiv5apu.spack
==> Extracting zstd-1.5.2-fprmzmoubdjdr663rprctp2tuqiv5apu from binary cache
==> zstd: Successfully installed zstd-1.5.2-fprmzmoubdjdr663rprctp2tuqiv5apu
  Fetch: 0.03s.	 Build: 0.26s.	Total: 0.29s.
[+] /home/spack/spack/opt/spack/linux-ubuntu18.04-x86_64/gcc-7.5.0/zstd-1.5.2-fprmzmoubdjdr663rprctp2tuqiv5apu
==> Installing ncurses-6.3-zcuawlif55qhuknvbk4jez7xjcxhnydf
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64-gcc-7.5.0-ncurses-6.3-zcuawlif55qhuknvbk4jez7xjcxhnydf.spec.json.sig
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64/gcc-7.5.0/ncurses-6.3/linux-ubuntu18.04-x86_64-gcc-7.5.0-ncurses-6.3-zcuawlif55qhuknvbk4jez7xjcxhnydf.spack
==> Extracting ncurses-6.3-zcuawlif55qhuknvbk4jez7xjcxhnydf from binary cache
==> ncurses: Successfully installed ncurses-6.3-zcuawlif55qhuknvbk4jez7xjcxhnydf
  Fetch: 0.04s.	 Build: 0.64s.	Total: 0.68s.
[+] /home/spack/spack/opt/spack/linux-ubuntu18.04-x86_64/gcc-7.5.0/ncurses-6.3-zcuawlif55qhuknvbk4jez7xjcxhnydf
==> Installing diffutils-3.8-c3uhkcjt7tagz2djrd6r2b6b3c2tia3s
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64-gcc-7.5.0-diffutils-3.8-c3uhkcjt7tagz2djrd6r2b6b3c2tia3s.spec.json.sig
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64/gcc-7.5.0/diffutils-3.8/linux-ubuntu18.04-x86_64-gcc-7.5.0-diffutils-3.8-c3uhkcjt7tagz2djrd6r2b6b3c2tia3s.spack
==> Extracting diffutils-3.8-c3uhkcjt7tagz2djrd6r2b6b3c2tia3s from binary cache
==> diffutils: Successfully installed diffutils-3.8-c3uhkcjt7tagz2djrd6r2b6b3c2tia3s
  Fetch: 0.03s.	 Build: 0.26s.	Total: 0.28s.
[+] /home/spack/spack/opt/spack/linux-ubuntu18.04-x86_64/gcc-7.5.0/diffutils-3.8-c3uhkcjt7tagz2djrd6r2b6b3c2tia3s
==> Installing pigz-2.7-wod35nye4qcrkbopr23zoy5qb3ywp3v3
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64-gcc-7.5.0-pigz-2.7-wod35nye4qcrkbopr23zoy5qb3ywp3v3.spec.json.sig
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64/gcc-7.5.0/pigz-2.7/linux-ubuntu18.04-x86_64-gcc-7.5.0-pigz-2.7-wod35nye4qcrkbopr23zoy5qb3ywp3v3.spack
==> Extracting pigz-2.7-wod35nye4qcrkbopr23zoy5qb3ywp3v3 from binary cache
==> pigz: Successfully installed pigz-2.7-wod35nye4qcrkbopr23zoy5qb3ywp3v3
  Fetch: 0.02s.	 Build: 0.23s.	Total: 0.25s.
[+] /home/spack/spack/opt/spack/linux-ubuntu18.04-x86_64/gcc-7.5.0/pigz-2.7-wod35nye4qcrkbopr23zoy5qb3ywp3v3
==> Installing libxml2-2.10.1-4aq5aj2n5u2n3dbrb76pnhoil6tv3e4e
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64-gcc-7.5.0-libxml2-2.10.1-4aq5aj2n5u2n3dbrb76pnhoil6tv3e4e.spec.json.sig
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64/gcc-7.5.0/libxml2-2.10.1/linux-ubuntu18.04-x86_64-gcc-7.5.0-libxml2-2.10.1-4aq5aj2n5u2n3dbrb76pnhoil6tv3e4e.spack
==> Extracting libxml2-2.10.1-4aq5aj2n5u2n3dbrb76pnhoil6tv3e4e from binary cache
==> libxml2: Successfully installed libxml2-2.10.1-4aq5aj2n5u2n3dbrb76pnhoil6tv3e4e
  Fetch: 0.03s.	 Build: 0.30s.	Total: 0.33s.
[+] /home/spack/spack/opt/spack/linux-ubuntu18.04-x86_64/gcc-7.5.0/libxml2-2.10.1-4aq5aj2n5u2n3dbrb76pnhoil6tv3e4e
==> Installing libedit-3.1-20210216-6pzeykrtvrkpbw6u3mq6likd4ktvah76
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64-gcc-7.5.0-libedit-3.1-20210216-6pzeykrtvrkpbw6u3mq6likd4ktvah76.spec.json.sig
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64/gcc-7.5.0/libedit-3.1-20210216/linux-ubuntu18.04-x86_64-gcc-7.5.0-libedit-3.1-20210216-6pzeykrtvrkpbw6u3mq6likd4ktvah76.spack
==> Extracting libedit-3.1-20210216-6pzeykrtvrkpbw6u3mq6likd4ktvah76 from binary cache
==> libedit: Successfully installed libedit-3.1-20210216-6pzeykrtvrkpbw6u3mq6likd4ktvah76
  Fetch: 0.03s.	 Build: 0.24s.	Total: 0.27s.
[+] /home/spack/spack/opt/spack/linux-ubuntu18.04-x86_64/gcc-7.5.0/libedit-3.1-20210216-6pzeykrtvrkpbw6u3mq6likd4ktvah76
==> Installing readline-8.1.2-kgyvtw5h6mi5nms32x3vzgbw32ymmfwa
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64-gcc-7.5.0-readline-8.1.2-kgyvtw5h6mi5nms32x3vzgbw32ymmfwa.spec.json.sig
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64/gcc-7.5.0/readline-8.1.2/linux-ubuntu18.04-x86_64-gcc-7.5.0-readline-8.1.2-kgyvtw5h6mi5nms32x3vzgbw32ymmfwa.spack
==> Extracting readline-8.1.2-kgyvtw5h6mi5nms32x3vzgbw32ymmfwa from binary cache
==> readline: Successfully installed readline-8.1.2-kgyvtw5h6mi5nms32x3vzgbw32ymmfwa
  Fetch: 0.03s.	 Build: 0.25s.	Total: 0.28s.
[+] /home/spack/spack/opt/spack/linux-ubuntu18.04-x86_64/gcc-7.5.0/readline-8.1.2-kgyvtw5h6mi5nms32x3vzgbw32ymmfwa
==> Installing bzip2-1.0.8-cxcrelnw64jh6lvhzb6pfar7nijv6us6
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64-gcc-7.5.0-bzip2-1.0.8-cxcrelnw64jh6lvhzb6pfar7nijv6us6.spec.json.sig
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64/gcc-7.5.0/bzip2-1.0.8/linux-ubuntu18.04-x86_64-gcc-7.5.0-bzip2-1.0.8-cxcrelnw64jh6lvhzb6pfar7nijv6us6.spack
==> Extracting bzip2-1.0.8-cxcrelnw64jh6lvhzb6pfar7nijv6us6 from binary cache
==> bzip2: Successfully installed bzip2-1.0.8-cxcrelnw64jh6lvhzb6pfar7nijv6us6
  Fetch: 0.03s.	 Build: 0.24s.	Total: 0.27s.
[+] /home/spack/spack/opt/spack/linux-ubuntu18.04-x86_64/gcc-7.5.0/bzip2-1.0.8-cxcrelnw64jh6lvhzb6pfar7nijv6us6
==> Installing m4-1.4.19-z7d5q4f2qzxejx4xxdrj45q5gxur6q7b
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64-gcc-7.5.0-m4-1.4.19-z7d5q4f2qzxejx4xxdrj45q5gxur6q7b.spec.json.sig
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64/gcc-7.5.0/m4-1.4.19/linux-ubuntu18.04-x86_64-gcc-7.5.0-m4-1.4.19-z7d5q4f2qzxejx4xxdrj45q5gxur6q7b.spack
==> Extracting m4-1.4.19-z7d5q4f2qzxejx4xxdrj45q5gxur6q7b from binary cache
==> m4: Successfully installed m4-1.4.19-z7d5q4f2qzxejx4xxdrj45q5gxur6q7b
  Fetch: 0.03s.	 Build: 0.26s.	Total: 0.29s.
[+] /home/spack/spack/opt/spack/linux-ubuntu18.04-x86_64/gcc-7.5.0/m4-1.4.19-z7d5q4f2qzxejx4xxdrj45q5gxur6q7b
==> Installing gdbm-1.23-cpmyntybsnm3se7lfnvqosbiqzrcg2kq
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64-gcc-7.5.0-gdbm-1.23-cpmyntybsnm3se7lfnvqosbiqzrcg2kq.spec.json.sig
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64/gcc-7.5.0/gdbm-1.23/linux-ubuntu18.04-x86_64-gcc-7.5.0-gdbm-1.23-cpmyntybsnm3se7lfnvqosbiqzrcg2kq.spack
==> Extracting gdbm-1.23-cpmyntybsnm3se7lfnvqosbiqzrcg2kq from binary cache
==> gdbm: Successfully installed gdbm-1.23-cpmyntybsnm3se7lfnvqosbiqzrcg2kq
  Fetch: 0.03s.	 Build: 0.27s.	Total: 0.29s.
[+] /home/spack/spack/opt/spack/linux-ubuntu18.04-x86_64/gcc-7.5.0/gdbm-1.23-cpmyntybsnm3se7lfnvqosbiqzrcg2kq
==> Installing tar-1.34-kawegoyhusotpo5mlsqnjqf6ngrjyj2w
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64-gcc-7.5.0-tar-1.34-kawegoyhusotpo5mlsqnjqf6ngrjyj2w.spec.json.sig
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64/gcc-7.5.0/tar-1.34/linux-ubuntu18.04-x86_64-gcc-7.5.0-tar-1.34-kawegoyhusotpo5mlsqnjqf6ngrjyj2w.spack
==> Extracting tar-1.34-kawegoyhusotpo5mlsqnjqf6ngrjyj2w from binary cache
==> tar: Successfully installed tar-1.34-kawegoyhusotpo5mlsqnjqf6ngrjyj2w
  Fetch: 0.03s.	 Build: 0.29s.	Total: 0.31s.
[+] /home/spack/spack/opt/spack/linux-ubuntu18.04-x86_64/gcc-7.5.0/tar-1.34-kawegoyhusotpo5mlsqnjqf6ngrjyj2w
==> Installing libtool-2.4.7-bdxu4wnbhlmvj6sh5y7uynrldzepiilz
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64-gcc-7.5.0-libtool-2.4.7-bdxu4wnbhlmvj6sh5y7uynrldzepiilz.spec.json.sig
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64/gcc-7.5.0/libtool-2.4.7/linux-ubuntu18.04-x86_64-gcc-7.5.0-libtool-2.4.7-bdxu4wnbhlmvj6sh5y7uynrldzepiilz.spack
==> Extracting libtool-2.4.7-bdxu4wnbhlmvj6sh5y7uynrldzepiilz from binary cache
==> libtool: Successfully installed libtool-2.4.7-bdxu4wnbhlmvj6sh5y7uynrldzepiilz
  Fetch: 0.03s.	 Build: 0.25s.	Total: 0.28s.
[+] /home/spack/spack/opt/spack/linux-ubuntu18.04-x86_64/gcc-7.5.0/libtool-2.4.7-bdxu4wnbhlmvj6sh5y7uynrldzepiilz
==> Installing perl-5.36.0-eydiwimku2cctu6dsom3yvfxxjmqyr4w
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64-gcc-7.5.0-perl-5.36.0-eydiwimku2cctu6dsom3yvfxxjmqyr4w.spec.json.sig
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64/gcc-7.5.0/perl-5.36.0/linux-ubuntu18.04-x86_64-gcc-7.5.0-perl-5.36.0-eydiwimku2cctu6dsom3yvfxxjmqyr4w.spack
==> Extracting perl-5.36.0-eydiwimku2cctu6dsom3yvfxxjmqyr4w from binary cache
==> perl: Successfully installed perl-5.36.0-eydiwimku2cctu6dsom3yvfxxjmqyr4w
  Fetch: 0.04s.	 Build: 1.35s.	Total: 1.39s.
[+] /home/spack/spack/opt/spack/linux-ubuntu18.04-x86_64/gcc-7.5.0/perl-5.36.0-eydiwimku2cctu6dsom3yvfxxjmqyr4w
==> Installing gettext-0.21.1-hj6szikaqzdpxa36gk4w72m4czhwd6gu
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64-gcc-7.5.0-gettext-0.21.1-hj6szikaqzdpxa36gk4w72m4czhwd6gu.spec.json.sig
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64/gcc-7.5.0/gettext-0.21.1/linux-ubuntu18.04-x86_64-gcc-7.5.0-gettext-0.21.1-hj6szikaqzdpxa36gk4w72m4czhwd6gu.spack
==> Extracting gettext-0.21.1-hj6szikaqzdpxa36gk4w72m4czhwd6gu from binary cache
==> gettext: Successfully installed gettext-0.21.1-hj6szikaqzdpxa36gk4w72m4czhwd6gu
  Fetch: 0.03s.	 Build: 0.66s.	Total: 0.70s.
[+] /home/spack/spack/opt/spack/linux-ubuntu18.04-x86_64/gcc-7.5.0/gettext-0.21.1-hj6szikaqzdpxa36gk4w72m4czhwd6gu
==> Installing libpciaccess-0.16-p7f5n6q6kwnzq7syvqbslnotl72yxs23
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64-gcc-7.5.0-libpciaccess-0.16-p7f5n6q6kwnzq7syvqbslnotl72yxs23.spec.json.sig
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64/gcc-7.5.0/libpciaccess-0.16/linux-ubuntu18.04-x86_64-gcc-7.5.0-libpciaccess-0.16-p7f5n6q6kwnzq7syvqbslnotl72yxs23.spack
==> Extracting libpciaccess-0.16-p7f5n6q6kwnzq7syvqbslnotl72yxs23 from binary cache
==> libpciaccess: Successfully installed libpciaccess-0.16-p7f5n6q6kwnzq7syvqbslnotl72yxs23
  Fetch: 0.03s.	 Build: 0.23s.	Total: 0.26s.
[+] /home/spack/spack/opt/spack/linux-ubuntu18.04-x86_64/gcc-7.5.0/libpciaccess-0.16-p7f5n6q6kwnzq7syvqbslnotl72yxs23
==> Installing autoconf-2.69-btoteeiz74epw4kulm7aecae4cxraxua
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64-gcc-7.5.0-autoconf-2.69-btoteeiz74epw4kulm7aecae4cxraxua.spec.json.sig
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64/gcc-7.5.0/autoconf-2.69/linux-ubuntu18.04-x86_64-gcc-7.5.0-autoconf-2.69-btoteeiz74epw4kulm7aecae4cxraxua.spack
==> Extracting autoconf-2.69-btoteeiz74epw4kulm7aecae4cxraxua from binary cache
==> autoconf: Successfully installed autoconf-2.69-btoteeiz74epw4kulm7aecae4cxraxua
  Fetch: 0.03s.	 Build: 0.27s.	Total: 0.30s.
[+] /home/spack/spack/opt/spack/linux-ubuntu18.04-x86_64/gcc-7.5.0/autoconf-2.69-btoteeiz74epw4kulm7aecae4cxraxua
==> Installing bison-3.8.2-wobmzgvlc235muaaxwehzblkgcnhvwtn
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64-gcc-7.5.0-bison-3.8.2-wobmzgvlc235muaaxwehzblkgcnhvwtn.spec.json.sig
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64/gcc-7.5.0/bison-3.8.2/linux-ubuntu18.04-x86_64-gcc-7.5.0-bison-3.8.2-wobmzgvlc235muaaxwehzblkgcnhvwtn.spack
==> Extracting bison-3.8.2-wobmzgvlc235muaaxwehzblkgcnhvwtn from binary cache
==> bison: Successfully installed bison-3.8.2-wobmzgvlc235muaaxwehzblkgcnhvwtn
  Fetch: 0.03s.	 Build: 0.31s.	Total: 0.33s.
[+] /home/spack/spack/opt/spack/linux-ubuntu18.04-x86_64/gcc-7.5.0/bison-3.8.2-wobmzgvlc235muaaxwehzblkgcnhvwtn
==> Installing openssl-1.1.1s-yr4z2pt4jnp5fltumr2my3badazsarz2
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64-gcc-7.5.0-openssl-1.1.1s-yr4z2pt4jnp5fltumr2my3badazsarz2.spec.json.sig
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64/gcc-7.5.0/openssl-1.1.1s/linux-ubuntu18.04-x86_64-gcc-7.5.0-openssl-1.1.1s-yr4z2pt4jnp5fltumr2my3badazsarz2.spack
==> Extracting openssl-1.1.1s-yr4z2pt4jnp5fltumr2my3badazsarz2 from binary cache
==> openssl: Successfully installed openssl-1.1.1s-yr4z2pt4jnp5fltumr2my3badazsarz2
  Fetch: 0.03s.	 Build: 0.33s.	Total: 0.36s.
[+] /home/spack/spack/opt/spack/linux-ubuntu18.04-x86_64/gcc-7.5.0/openssl-1.1.1s-yr4z2pt4jnp5fltumr2my3badazsarz2
==> Installing hwloc-2.8.0-5yywrk2zoib75deet4eu4ocxim2ldngq
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64-gcc-7.5.0-hwloc-2.8.0-5yywrk2zoib75deet4eu4ocxim2ldngq.spec.json.sig
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64/gcc-7.5.0/hwloc-2.8.0/linux-ubuntu18.04-x86_64-gcc-7.5.0-hwloc-2.8.0-5yywrk2zoib75deet4eu4ocxim2ldngq.spack
==> Extracting hwloc-2.8.0-5yywrk2zoib75deet4eu4ocxim2ldngq from binary cache
==> hwloc: Successfully installed hwloc-2.8.0-5yywrk2zoib75deet4eu4ocxim2ldngq
  Fetch: 0.03s.	 Build: 0.36s.	Total: 0.39s.
[+] /home/spack/spack/opt/spack/linux-ubuntu18.04-x86_64/gcc-7.5.0/hwloc-2.8.0-5yywrk2zoib75deet4eu4ocxim2ldngq
==> Installing automake-1.16.5-rxjqnjw5jkrbym5x4yshogm3f262crre
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64-gcc-7.5.0-automake-1.16.5-rxjqnjw5jkrbym5x4yshogm3f262crre.spec.json.sig
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64/gcc-7.5.0/automake-1.16.5/linux-ubuntu18.04-x86_64-gcc-7.5.0-automake-1.16.5-rxjqnjw5jkrbym5x4yshogm3f262crre.spack
==> Extracting automake-1.16.5-rxjqnjw5jkrbym5x4yshogm3f262crre from binary cache
==> automake: Successfully installed automake-1.16.5-rxjqnjw5jkrbym5x4yshogm3f262crre
  Fetch: 0.02s.	 Build: 0.26s.	Total: 0.28s.
[+] /home/spack/spack/opt/spack/linux-ubuntu18.04-x86_64/gcc-7.5.0/automake-1.16.5-rxjqnjw5jkrbym5x4yshogm3f262crre
==> Installing krb5-1.19.3-y6ek4vz7ntj6qw26jv2trb3ok4m5koxy
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64-gcc-7.5.0-krb5-1.19.3-y6ek4vz7ntj6qw26jv2trb3ok4m5koxy.spec.json.sig
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64/gcc-7.5.0/krb5-1.19.3/linux-ubuntu18.04-x86_64-gcc-7.5.0-krb5-1.19.3-y6ek4vz7ntj6qw26jv2trb3ok4m5koxy.spack
==> Extracting krb5-1.19.3-y6ek4vz7ntj6qw26jv2trb3ok4m5koxy from binary cache
==> krb5: Successfully installed krb5-1.19.3-y6ek4vz7ntj6qw26jv2trb3ok4m5koxy
  Fetch: 0.03s.	 Build: 0.47s.	Total: 0.50s.
[+] /home/spack/spack/opt/spack/linux-ubuntu18.04-x86_64/gcc-7.5.0/krb5-1.19.3-y6ek4vz7ntj6qw26jv2trb3ok4m5koxy
==> Installing cmake-3.24.3-stszbnscxrtgzqrwmx2wfwsuntf47mpy
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64-gcc-7.5.0-cmake-3.24.3-stszbnscxrtgzqrwmx2wfwsuntf47mpy.spec.json.sig
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64/gcc-7.5.0/cmake-3.24.3/linux-ubuntu18.04-x86_64-gcc-7.5.0-cmake-3.24.3-stszbnscxrtgzqrwmx2wfwsuntf47mpy.spack
==> Extracting cmake-3.24.3-stszbnscxrtgzqrwmx2wfwsuntf47mpy from binary cache
==> cmake: Successfully installed cmake-3.24.3-stszbnscxrtgzqrwmx2wfwsuntf47mpy
  Fetch: 0.05s.	 Build: 0.93s.	Total: 0.98s.
[+] /home/spack/spack/opt/spack/linux-ubuntu18.04-x86_64/gcc-7.5.0/cmake-3.24.3-stszbnscxrtgzqrwmx2wfwsuntf47mpy
==> Installing libevent-2.1.12-nblr6w4gwbdyhjz2z5lg5pseztxnb7gh
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64-gcc-7.5.0-libevent-2.1.12-nblr6w4gwbdyhjz2z5lg5pseztxnb7gh.spec.json.sig
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64/gcc-7.5.0/libevent-2.1.12/linux-ubuntu18.04-x86_64-gcc-7.5.0-libevent-2.1.12-nblr6w4gwbdyhjz2z5lg5pseztxnb7gh.spack
==> Extracting libevent-2.1.12-nblr6w4gwbdyhjz2z5lg5pseztxnb7gh from binary cache
==> libevent: Successfully installed libevent-2.1.12-nblr6w4gwbdyhjz2z5lg5pseztxnb7gh
  Fetch: 0.03s.	 Build: 0.29s.	Total: 0.32s.
[+] /home/spack/spack/opt/spack/linux-ubuntu18.04-x86_64/gcc-7.5.0/libevent-2.1.12-nblr6w4gwbdyhjz2z5lg5pseztxnb7gh
==> Installing numactl-2.0.14-qbwqmk6qylskqtyrfuyfp36f465bxrrk
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64-gcc-7.5.0-numactl-2.0.14-qbwqmk6qylskqtyrfuyfp36f465bxrrk.spec.json.sig
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64/gcc-7.5.0/numactl-2.0.14/linux-ubuntu18.04-x86_64-gcc-7.5.0-numactl-2.0.14-qbwqmk6qylskqtyrfuyfp36f465bxrrk.spack
==> Extracting numactl-2.0.14-qbwqmk6qylskqtyrfuyfp36f465bxrrk from binary cache
==> numactl: Successfully installed numactl-2.0.14-qbwqmk6qylskqtyrfuyfp36f465bxrrk
  Fetch: 0.03s.	 Build: 0.25s.	Total: 0.27s.
[+] /home/spack/spack/opt/spack/linux-ubuntu18.04-x86_64/gcc-7.5.0/numactl-2.0.14-qbwqmk6qylskqtyrfuyfp36f465bxrrk
==> Installing openssh-9.1p1-xevso22q5f5fb2bqp5lhuutgvrqtiolo
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64-gcc-7.5.0-openssh-9.1p1-xevso22q5f5fb2bqp5lhuutgvrqtiolo.spec.json.sig
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64/gcc-7.5.0/openssh-9.1p1/linux-ubuntu18.04-x86_64-gcc-7.5.0-openssh-9.1p1-xevso22q5f5fb2bqp5lhuutgvrqtiolo.spack
==> Extracting openssh-9.1p1-xevso22q5f5fb2bqp5lhuutgvrqtiolo from binary cache
==> openssh: Successfully installed openssh-9.1p1-xevso22q5f5fb2bqp5lhuutgvrqtiolo
  Fetch: 0.03s.	 Build: 0.32s.	Total: 0.35s.
[+] /home/spack/spack/opt/spack/linux-ubuntu18.04-x86_64/gcc-7.5.0/openssh-9.1p1-xevso22q5f5fb2bqp5lhuutgvrqtiolo
==> Installing pmix-4.1.2-76ul6unbdppxalfkkxjqzoky7lu4jqpo
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64-gcc-7.5.0-pmix-4.1.2-76ul6unbdppxalfkkxjqzoky7lu4jqpo.spec.json.sig
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64/gcc-7.5.0/pmix-4.1.2/linux-ubuntu18.04-x86_64-gcc-7.5.0-pmix-4.1.2-76ul6unbdppxalfkkxjqzoky7lu4jqpo.spack
==> Extracting pmix-4.1.2-76ul6unbdppxalfkkxjqzoky7lu4jqpo from binary cache
==> pmix: Successfully installed pmix-4.1.2-76ul6unbdppxalfkkxjqzoky7lu4jqpo
  Fetch: 0.03s.	 Build: 0.32s.	Total: 0.34s.
[+] /home/spack/spack/opt/spack/linux-ubuntu18.04-x86_64/gcc-7.5.0/pmix-4.1.2-76ul6unbdppxalfkkxjqzoky7lu4jqpo
==> Installing openmpi-4.1.4-mfxvu5fnaac33f4pw25rjrojafbbupwb
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64-gcc-7.5.0-openmpi-4.1.4-mfxvu5fnaac33f4pw25rjrojafbbupwb.spec.json.sig
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64/gcc-7.5.0/openmpi-4.1.4/linux-ubuntu18.04-x86_64-gcc-7.5.0-openmpi-4.1.4-mfxvu5fnaac33f4pw25rjrojafbbupwb.spack
==> Extracting openmpi-4.1.4-mfxvu5fnaac33f4pw25rjrojafbbupwb from binary cache
==> openmpi: Successfully installed openmpi-4.1.4-mfxvu5fnaac33f4pw25rjrojafbbupwb
  Fetch: 0.03s.	 Build: 0.64s.	Total: 0.67s.
[+] /home/spack/spack/opt/spack/linux-ubuntu18.04-x86_64/gcc-7.5.0/openmpi-4.1.4-mfxvu5fnaac33f4pw25rjrojafbbupwb
==> Installing hdf5-1.12.2-xfftbwlx2fmpxvfzg27vj6hk2pet45f2
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64-gcc-7.5.0-hdf5-1.12.2-xfftbwlx2fmpxvfzg27vj6hk2pet45f2.spec.json.sig
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64/gcc-7.5.0/hdf5-1.12.2/linux-ubuntu18.04-x86_64-gcc-7.5.0-hdf5-1.12.2-xfftbwlx2fmpxvfzg27vj6hk2pet45f2.spack
==> Extracting hdf5-1.12.2-xfftbwlx2fmpxvfzg27vj6hk2pet45f2 from binary cache
==> hdf5: Successfully installed hdf5-1.12.2-xfftbwlx2fmpxvfzg27vj6hk2pet45f2
  Fetch: 0.12s.	 Build: 2.60s.	Total: 2.73s.
[+] /home/spack/spack/opt/spack/linux-ubuntu18.04-x86_64/gcc-7.5.0/hdf5-1.12.2-xfftbwlx2fmpxvfzg27vj6hk2pet45f2
$ spack install zlib%clang
==> Installing zlib-1.2.13-fdbomoa6xopqvfi4a7jvad27yinnw47a
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64-clang-7.0.0-zlib-1.2.13-fdbomoa6xopqvfi4a7jvad27yinnw47a.spec.json.sig
==> Fetching file:///mirror/build_cache/linux-ubuntu18.04-x86_64/clang-7.0.0/zlib-1.2.13/linux-ubuntu18.04-x86_64-clang-7.0.0-zlib-1.2.13-fdbomoa6xopqvfi4a7jvad27yinnw47a.spack
==> Extracting zlib-1.2.13-fdbomoa6xopqvfi4a7jvad27yinnw47a from binary cache
==> zlib: Successfully installed zlib-1.2.13-fdbomoa6xopqvfi4a7jvad27yinnw47a
  Fetch: 0.09s.	 Build: 0.24s.	Total: 0.32s.
[+] /home/spack/spack/opt/spack/linux-ubuntu18.04-x86_64/clang-7.0.0/zlib-1.2.13-fdbomoa6xopqvfi4a7jvad27yinnw47a

Now we are ready to use the Spack’s find and python subcommands to query the installed packages.

Scripting with spack find

So far, the output we’ve seen from spack find has been for human consumption. But you can take advantage of some advanced options of the command to generate machine-readable output suitable for piping to a script.

spack find --format

The main job of spack find is to show the user a bunch of concrete specs that correspond to installed packages. By default, we display them with some default attributes, like the @version suffix you’re used to seeing in the output.

The --format argument allows you to display the specs however you choose, using custom format strings. Format strings let you specify the names of particular parts of the specs you want displayed. Let’s see the first option in action.

Suppose you only want to display the name, version, and first ten (10) characters of the hash for every package installed in your Spack instance. You can generate that output with the following command:

$ spack find --format "{name} {version} {hash:10}"
autoconf 2.69 btoteeiz74	ca-certificates-mozilla 2022-10-11 aurd37jjeb  hdf5 1.12.2 xfftbwlx2f		libiconv 1.16 ltvovvvjgm      m4 1.4.19 z7d5q4f2qz	 openssl 1.1.1s yr4z2pt4jn  readline 8.1.2 kgyvtw5h6m	   zlib 1.2.13 2hfr5rbzef
automake 1.16.5 rxjqnjw5jk	cmake 3.24.3 stszbnscxr			       hwloc 2.8.0 5yywrk2zoi		libpciaccess 0.16 p7f5n6q6kw  ncurses 6.3 zcuawlif55	 perl 5.36.0 eydiwimku2	    tar 1.34 kawegoyhus		   zstd 1.5.2 fprmzmoubd
berkeley-db 18.1.40 53fb72wzis	diffutils 3.8 c3uhkcjt7t		       krb5 1.19.3 y6ek4vz7nt		libsigsegv 2.13 e262sf6ptg    numactl 2.0.14 qbwqmk6qyl	 pigz 2.7 wod35nye4q	    util-macros 1.19.3 ao5w53rpwh
bison 3.8.2 wobmzgvlc2		gdbm 1.23 cpmyntybsn			       libedit 3.1-20210216 6pzeykrtvr	libtool 2.4.7 bdxu4wnbhl      openmpi 4.1.4 mfxvu5fnaa	 pkgconf 1.8.0 k2lfw6fht6   xz 5.2.7 nrwxythffg
bzip2 1.0.8 cxcrelnw64		gettext 0.21.1 hj6szikaqz		       libevent 2.1.12 nblr6w4gwb	libxml2 2.10.1 4aq5aj2n5u     openssh 9.1p1 xevso22q5f	 pmix 4.1.2 76ul6unbdp	    zlib 1.2.13 fdbomoa6xo

Note that name, version, and hash are attributes of Spack’s internal Spec object and enclosing them in braces ensures they are output according to your format string.

Using spack find --format allows you to retrieve just the information you need to do things like pipe the output to typical UNIX command-line tools like sort or uniq.

spack find --json

Alternatively, you can get a serialized version of Spec objects in the JSON format using the --json option. For example, you can get attributes for all installations of zlib by entering:

$ spack find --json zlib
[
  {
    "name": "zlib",
    "version": "1.2.13",
    "arch": {
      "platform": "linux",
      "platform_os": "ubuntu18.04",
      "target": "x86_64"
    },
    "compiler": {
      "name": "clang",
      "version": "7.0.0"
    },
    "namespace": "builtin",
    "parameters": {
      "build_system": "makefile",
      "optimize": true,
      "pic": true,
      "shared": true,
      "cflags": [],
      "cppflags": [],
      "cxxflags": [],
      "fflags": [],
      "ldflags": [],
      "ldlibs": []
    },
    "package_hash": "ghj4qyx3sv3so5vpvnodld6r372mc6oqefofmoer3xbpq5tcwvca====",
    "hash": "fdbomoa6xopqvfi4a7jvad27yinnw47a"
  },
  {
    "name": "zlib",
    "version": "1.2.13",
    "arch": {
      "platform": "linux",
      "platform_os": "ubuntu18.04",
      "target": "x86_64"
    },
    "compiler": {
      "name": "gcc",
      "version": "7.5.0"
    },
    "namespace": "builtin",
    "parameters": {
      "build_system": "makefile",
      "optimize": true,
      "pic": true,
      "shared": true,
      "cflags": [],
      "cppflags": [],
      "cxxflags": [],
      "fflags": [],
      "ldflags": [],
      "ldlibs": []
    },
    "package_hash": "ghj4qyx3sv3so5vpvnodld6r372mc6oqefofmoer3xbpq5tcwvca====",
    "hash": "2hfr5rbzefrxf6t6kv4trbsl63puaza6"
  }

The spack find --json command gives you everything we know about the specs in a structured format. You can pipe its output to JSON filtering tools like jq to extract just the parts you want.

Check out the basic usage docs for more examples.

Introducing the spack python command

What if we need to perform more advanced queries?

Spack provides the spack python command to launch a python interpreter with Spack’s python modules available to import. It uses the underlying python for the rest of its commands. So you can write scripts to:

  • run Spack commands;
  • explore abstract and concretized specs; and
  • directly access other internal components of Spack.

Let’s launch a Spack-aware python interpreter by entering:

$ spack python
exit()
Spack version 0.19.1
Python 3.6.9, Linux x86_64
>>> exit()

Since we are in a python interpreter, use exit() to end the session and return to the terminal.

Accessing the Spec object

Now let’s take a look at the internal representation of the Spack Spec. As you already know, specs can be either abstract or concrete. The specs you’ve seen in package.py files (e.g., in the install() method) have been concrete, or fully specified. The specs you’ve typed on the command line have been abstract. Understanding the differences between the two types is key to using Spack’s internal API.

Let’s open another python interpreter with spack python, instantiate the zlib spec, and check a few properties of an abstract spec:

  >>> from spack.spec import Spec
  >>> s = Spec('zlib target=ivybridge')
  >>> s.concrete
  False
  >>> s.version
  Traceback (most recent call last):
    File "<console>", line 1, in <module>
    File "/home/spack/spack/lib/spack/spack/spec.py", line 3166, in version
      raise SpecError("Spec version is not concrete: " + str(self))
  SpecError: Spec version is not concrete: zlib arch=linux-None-ivybridge
  >>> s.versions
  [:]
  >>> str(s.architecture)
  linux-None-ivybridge

Notice that there are Spec properties and methods that are not accessible to abstract specs; specifically:

  • an exception – SpecError – is raised if we try to access its version;
  • there are no associated versions; and
  • the spec’s operating system is None.

Now, without exiting the interpreter, let’s concretize the spec and try again:

  >>> s.concretize()
  >>> s.concrete
  True
  >>> s.version
  Version('1.2.11')
  >>> s.versions
  [Version('1.2.11')]
  >>> str(s.architecture)
  linux-ubuntu18.04-ivybridge

Notice that the concretized spec now:

  • has a version;
  • has a single entry in its versions list; and
  • the operating system is now ubuntu18.04.

It is not necessary to store the intermediate abstract spec – you can use the .concretized() method as shorthand:

  >>> t = Spec('zlib target=ivybridge').concretized()
  >>> s == t
  True

Querying the Spack database

Even more powerful queries are available when we look at the information stored in the Spack database. The Database object in Spack is in the spack.store.db variable. We’ll interact with it mainly through the query() method. Let’s see the documentation available for query() using python’s built-in help() function:

  >>> import spack.store
  >>> help(spack.store.db.query)
  Help on method query in module spack.database:

  query(*args, **kwargs) method of spack.database.Database instance
      Query the Spack database including all upstream databases.

      Args:
          query_spec: queries iterate through specs in the database and
              return those that satisfy the supplied ``query_spec``. If
              query_spec is `any`, This will match all specs in the
              database.  If it is a spec, we'll evaluate
              ``spec.satisfies(query_spec)``

          known (bool or any, optional): Specs that are "known" are those
              for which Spack can locate a ``package.py`` file -- i.e.,
              Spack "knows" how to install them.  Specs that are unknown may
              represent packages that existed in a previous version of
              Spack, but have since either changed their name or
              been removed

          installed (bool or any, or InstallStatus or iterable of
              InstallStatus, optional): if ``True``, includes only installed
              specs in the search; if ``False`` only missing specs, and if
              ``any``, all specs in database. If an InstallStatus or iterable
              of InstallStatus, returns specs whose install status
              (installed, deprecated, or missing) matches (one of) the
              InstallStatus. (default: True)

          explicit (bool or any, optional): A spec that was installed
              following a specific user request is marked as explicit. If
              instead it was pulled-in as a dependency of a user requested
              spec it's considered implicit.

          start_date (datetime, optional): filters the query discarding
              specs that have been installed before ``start_date``.

          end_date (datetime, optional): filters the query discarding
              specs that have been installed after ``end_date``.

          hashes (container): list or set of hashes that we can use to
              restrict the search

      Returns:
          list of specs that match the query
  (END)

We will primarily make use of the query_spec argument.

Recall that queries using the spack find command are limited to queries of attributes with matching values, not values they do not have. In other words, we cannot use the spack find command for all packages that do not satisfy a certain criterion.

We can use the python interface to write these types of queries. For example, let’s find all packages that were compiled with gcc but do not depend on mpich. We can do this by using custom python code and Spack database queries. We will use the spack.cmd.display_specs for output to achieve the same printing functionality as the spack find command:

  >>> gcc_query_spec = Spec('%gcc')
  >>> gcc_specs = spack.store.db.query(gcc_query_spec)
  >>> result = filter(lambda spec: not spec.satisfies('^mpich'), gcc_specs)
  >>> import spack.cmd
  >>> spack.cmd.display_specs(result)
  -- linux-ubuntu18.04-x86_64 / gcc@7.5.0 -------------------------
  autoconf@2.69    libiconv@1.16        m4@1.4.18       perl@5.30.3         zlib@1.2.11
  automake@1.16.2  libpciaccess@0.13.5  ncurses@6.2     pkgconf@1.7.3
  gdbm@1.18.1      libsigsegv@2.12      numactl@2.0.12  readline@8.0
  hdf5@1.10.6      libtool@2.4.6        openmpi@3.1.6   util-macros@1.19.1
  hwloc@1.11.11    libxml2@2.9.10       patchelf@0.10   xz@5.2.5

Now we have a powerful query not available through spack find.

Let’s exit the interpreter to take us back to the command line:

>>> exit()

before generalizing the functionality for re-use.

Using scripts

Now let’s parameterize our script to accept arguments on the command line. With a few generalizations to use the include and exclude specs as arguments, we can create a powerful, general-purpose query script.

Open a file called find_exclude.py in your preferred editor and add the following code:

from spack.spec import Spec
import spack.store
import spack.cmd
import sys

include_spec = Spec(sys.argv[1])
exclude_spec = Spec(sys.argv[2])

all_included = spack.store.db.query(include_spec)
result = filter(lambda spec: not spec.satisfies(exclude_spec), all_included)

spack.cmd.display_specs(result)

Notice we added importing and using the system package (sys) to access the first and second command line arguments.

Now we can run our new script by entering the following:

$ spack python find_exclude.py %gcc ^mpich
-- linux-ubuntu18.04-x86_64 / gcc@7.5.0 -------------------------
autoconf@2.69	     bison@3.8.2			 cmake@3.24.3	gettext@0.21.1	krb5@1.19.3	      libiconv@1.16	 libtool@2.4.7	 ncurses@6.3	 openssh@9.1p1	 pigz@2.7	readline@8.1.2	    xz@5.2.7
automake@1.16.5	     bzip2@1.0.8			 diffutils@3.8	hdf5@1.12.2	libedit@3.1-20210216  libpciaccess@0.16	 libxml2@2.10.1	 numactl@2.0.14	 openssl@1.1.1s	 pkgconf@1.8.0	tar@1.34	    zlib@1.2.13
berkeley-db@18.1.40  ca-certificates-mozilla@2022-10-11	 gdbm@1.23	hwloc@2.8.0	libevent@2.1.12	      libsigsegv@2.13	 m4@1.4.19	 openmpi@4.1.4	 perl@5.36.0	 pmix@4.1.2	util-macros@1.19.3  zstd@1.5.2

This is great for us, as long as we remember to use Spack’s python command to run it.

Using the spack-python executable

What if we want to make our script available for others to use without the hassle of having to remember to use spack python?

We can take advantage of the shebang line typically added as the first line of python executable files. But there is a catch, as we will soon see.

Open the find_exclude.py script we created above in your preferred editor and add the shebang line with spack python as the arguments to env:

#!/usr/bin/env spack-python
from spack.spec import Spec
import spack.store
import spack.cmd
import sys

include_spec = Spec(sys.argv[1])
exclude_spec = Spec(sys.argv[2])

all_included = spack.store.db.query(include_spec)
result = filter(lambda spec: not spec.satisfies(exclude_spec), all_included)

spack.cmd.display_specs(result)

Then exit our editor and add execute permissions to the script before running it as follows:

$ chmod u+x find_exclude.py
$ ./find_exclude.py %gcc ^mpich
-- linux-ubuntu18.04-x86_64 / gcc@7.5.0 -------------------------
autoconf@2.69	     bison@3.8.2			 cmake@3.24.3	gettext@0.21.1	krb5@1.19.3	      libiconv@1.16	 libtool@2.4.7	 ncurses@6.3	 openssh@9.1p1	 pigz@2.7	readline@8.1.2	    xz@5.2.7
automake@1.16.5	     bzip2@1.0.8			 diffutils@3.8	hdf5@1.12.2	libedit@3.1-20210216  libpciaccess@0.16	 libxml2@2.10.1	 numactl@2.0.14	 openssl@1.1.1s	 pkgconf@1.8.0	tar@1.34	    zlib@1.2.13
berkeley-db@18.1.40  ca-certificates-mozilla@2022-10-11	 gdbm@1.23	hwloc@2.8.0	libevent@2.1.12	      libsigsegv@2.13	 m4@1.4.19	 openmpi@4.1.4	 perl@5.36.0	 pmix@4.1.2	util-macros@1.19.3  zstd@1.5.2

If you are lucky, it worked on your system, but there is no guarantee. Some systems only support a single argument on the shebang line (see here). spack-python, which is a wrapper script for spack python, solves this issue.

Bring up the file in your editor again and change the env argument to spack-python as follows:

#!/usr/bin/env spack-python
from spack.spec import Spec
import spack.store
import spack.cmd
import sys

include_spec = Spec(sys.argv[1])
exclude_spec = Spec(sys.argv[2])

all_included = spack.store.db.query(include_spec)
result = filter(lambda spec: not spec.satisfies(exclude_spec), all_included)

spack.cmd.display_specs(result)

Exit your editor and let’s run the script again:

$ ./find_exclude.py %gcc ^mpich
-- linux-ubuntu18.04-x86_64 / gcc@7.5.0 -------------------------
autoconf@2.69	     bison@3.8.2			 cmake@3.24.3	gettext@0.21.1	krb5@1.19.3	      libiconv@1.16	 libtool@2.4.7	 ncurses@6.3	 openssh@9.1p1	 pigz@2.7	readline@8.1.2	    xz@5.2.7
automake@1.16.5	     bzip2@1.0.8			 diffutils@3.8	hdf5@1.12.2	libedit@3.1-20210216  libpciaccess@0.16	 libxml2@2.10.1	 numactl@2.0.14	 openssl@1.1.1s	 pkgconf@1.8.0	tar@1.34	    zlib@1.2.13
berkeley-db@18.1.40  ca-certificates-mozilla@2022-10-11	 gdbm@1.23	hwloc@2.8.0	libevent@2.1.12	      libsigsegv@2.13	 m4@1.4.19	 openmpi@4.1.4	 perl@5.36.0	 pmix@4.1.2	util-macros@1.19.3  zstd@1.5.2

Congratulations! It will now work on any system with Spack installed.

You now have the basic tools to create your own custom Spack queries and prototype ideas. We hope one day you’ll contribute them back to Spack.