You may have your own project, and you want to focus on some specific packages related to your project. Furthermore, you may have a special configuration for a group of packages. For example, building all the packages in the same group the same way. Such problems can be handled by spack environments.

In this tutorial, you’re going to learn how to create, activate, and deactivate environments. Also, you will learn how to manage packages in activated environments. For more details, visit Spack Documentation.

Creating Environments

Use spack env create to create a new environment:

spack env create <env_name>

For example, to create an environment called myenv type:

spack env create myenv

To show created environments, you type:

spack env list


==> 1 environments

The output shows that only one environment myenv has been created so far.

Activating Environments

Now you can activate created environments using spack env activate. To activate myenv environment, you type:

spack env activate myenv

To show in which environment you’re now, type:

spack env status

The following output shows that you’re in myenv environment.

==> In environment myenv

Deactivating Environments

To leave the current environment and go back to normal Spack, type:

spack env deactivate

Now, you can check if you have left myenv by typing:

spack env status

The following output shows no activated environments.

==> No active environment

Installing and Uninstalling Packages

Once you have created and activated an environment, you can install and uninstall packages as normal Spack. If you would like to install a package you type:

spack install <package_name>

You can also uninstall a package by typing:

spack uninstall <package_name>
Uninstalling a package from an environment doesn’t uninstall it from other environments.

Using Packages

Installed packages into an environment are linked into a single prefix or a view. When you activate the environment, Spack adds subdirectories from the view to PATH, LD_LIBRARY_PATH, CMAKE_PREFIX_PATH, and other environment variables. This makes the environment easier to use.

To use a package in normal Spack, you need to load it using spack load or module load commands. With environments, you can activate an environment to get everything in the environment on your PATH.

Suppose that you’re in myenv and already installed tcl package, you can use see that tclsh is in your PATH without loading it.

Now you can run tclsh as any other program:

% echo "welcome to Discovery"
welcome to Discovery