Spack Environment is a feature that allows us to group set of packages and easily use and manage them. Spack environments are same as python environment, conda environment etc.

Spack environment consist of two components:

  1. spack.yaml: Contains environment configurations, packages to install, compilers to use and other settings.
  2. spack.lock: Contains fully concretized specs needed to reproduce exact same environment on another compatible machine. What it means is, it contains specs versions, compilers used, variants, dependencies etc.

With this two files, an environment can be shared across compatible machines.

Creating Spack Environment

We can create an environment using command,

spack env create myenvironment

it should create myenvironment environment. It creates environment spack’s default location. We can see this location by activating environment and looking for variable SPACK_ENV.

spack env activate myenviroment
echo $SPACK_ENV

It should give something like /home/<user>/spack/var/spack/environments/spack-project.

Tip

Alternatively, we can use spack cd -e myenvironment that changes directory to where myenvironment is stored and do pwd to see the location of environment.

Environment created as above is called managed environment as they are stored in spack location. These environments are named and comes in the list when we do spack env list.

There is another type of environment, independent environment. This environment are created in a custom directory either by copying spack.yaml and spack.lock file into the directory or by providing a custom directory while creating environment as shown below.

spack env create -d ./myenvironment

it should create a directory myenvironment containing spack.yaml.

Installing Packages

If we try installing package as we usually do in spack as shown below,

spack install python

it gives an error saying,

==> Error: Cannot install 'python' because no matching specs are in the current environment.
 Specs can be added to the environment with 'spack add python',
 or as part of the install command with 'spack install --add python'

It is evident that there is no python spec in environment. We need to add python to the environment before installing.

spack add python

Once it is added, we can install it.

spack install

It should python into the environment and should be accessible.

How Are We Able to Access Packages?

If you noticed, we are able to access python after installing it. How does it work? So there is a spack environment feature called views that makes it easy to access installed packages. A view contains symlinks to those packages and spack updates current environment (shell variables) to make them accessible.

We can verify this by following command,

which python3

it gives path /home/spack/myenvironment/.spack-env/view/bin/python3 where myenvironment is the environment directory that we provided.

Removing Packages

Spack allows us to remove packages from one environment without affecting another environment. This works because spack creates link to shared packages in an environment and does not contain actual packages.

If we use following command to try removing the packages, we get error.

spack remove python

It removes python from the environment and updates spack.yaml file.

References

  1. https://spack-tutorial.readthedocs.io/en/latest/tutorial_environments.html#removing-packages-from-environments