Environments

The Basics

Environment variables effect the way programs run. One common example is controlling the search path for executables. In bash the PATH variable determines which directory paths are searched for executables, and in what order. To output your PATH variable you can execute the following:

$ echo $PATH

Directory paths prepended to the PATH variable will be searched first, directory paths appended to the PATH variable will be searched last.

Prepend:

$ PATH=/path/to/executable:$PATH

Append:

$ PATH=$PATH:/path/to/executable

Any alterations made to the environment at the command line will only effect the current terminal session. Other sessions in use will not be effected, and if you logout and log back in changes to the environment will be lost.

In bash you can output all your environment variables by executing the following:

$ env

There are many variables aside from the PATH variable. Changes to environment variables can lead to executables behaving in unexpected ways, or not functioning at all. Due to this it is best to keep a 'clean' base environment and use tools to manage different environments you may want to use.

Managing Your Environment

Modules

The Coeus cluster and stand alone compute servers use Linux environment modules to allow users to quickly update their environment, including execution paths, library paths, and manual paths, for specific software packages. For example, the Coeus cluster has module environments created for each MPI available implementation (openmpi, mpich, mvapich). Modules allow for users to update their environment to use specific software, and revert back to their base environment in a single session without having to manually change environment variables.

Basic module usage

To obtain a complete list of all modules currently available on the system

> module avail

To load a module, e.g. GCC 6.3.0 compilers

> module load gcc-6.3.0

To load a module, e.g. MVAPICH 2.2.2 compiled with GCC 6.3.0.  (this will automatically load the gcc-6.3.0 module) 

> module load mvapich2-2.2/gcc-6.3.0

To obtain a complete list of currently loaded modules

> module list

Currently Loaded Modulefiles:

 1) gcc-6.3.0                2) mvapich2-2.2/gcc-6.3.0

To unload a module, e.g. MVAPICH 2.2.2 compiled with GCC 6.3.0.  (this will automatically unload the gcc-6.3.0 module, too) 

> module unload mvapich2-2.2/gcc-6.3.0

NERSC has an excellent Modules usage reference

Other Tools for Environment Control

There are other tools available for controlling your shell environment. Refer to the Using Conda and Virtual Environment FAQs for more information on managing your environment


Manual Environment Control

Your environment can also be managed manually by exporting variables such as the following example which would add a directory named bin in your home directory to the path variable. Bash will now search for executables in this directory:

export PATH=~/bin/:$PATH

Exporting environment variables in this way will only effect the current bash session. There are configuration files for bash in your home directory such as .bashrc and .bashprofile. Adding aliases and and variables here will effect all future bash sessions, not the current one as these files are sourced when you log in. 

Note that this type of modification should be used sparingly unless you know what you are doing. The good thing about using tools like virtualenv, conda, and modules is that you can easily revert to a clean base environment.