Skip to main content

Modules

Create module files to set environment variables for various compilers and libraries.

Install Environment modules in Debian/Ubuntu:

sudo apt update
sudo apt install --no-install-recommends environment-modules

source following script so that module command is available. You may include in your .bashrc as well.

source /etc/profile.d/modules.sh

Here we will try to create a module for GCC. Download GCC source code and build:

wget https://mirror.freedif.org/GNU/gcc/gcc-12.3.0/gcc-12.3.0.tar.gz
tar zxf gcc-12.3.0.tar.gz
cd gcc-12.3.0
./contrib/download_prerequisites
cd ..
mkdir objdir
cd objdir
${PWD}/../gcc-12.3.0/configure --prefix=/workspaces/compilers/gcc-12.3.0 --enable-languages=c,c++,fortran,go --disable-multilib
make -j16 # time consuming task, parallelize on large number of processors
make install

cd ..
rm gcc-12.3.0.tar.gz
rm -rf gcc-12.3.0
rm -rf objdir

Here is our modulefile:

hpc/modulefiles/gcc-12.3.0
#%Module 1.0
## module gcc-12.3.0
## /workspaces/linux/src/modulefiles/gcc-12.3.0
## Author: Pranab Das

proc ModulesHelp { } {
global version modroot
puts stderr "gcc-12.3.0 - sets the Environment for gcc-12.3.0"
}

module-whatis "Sets the Environment for gcc-12.3.0"

set topdir /workspaces/compilers/gcc-12.3.0
set version 12.3.0

setenv CC ${topdir}/bin/gcc
setenv GCC ${topdir}/bin/gcc
setenv FC ${topdir}/bin/gfortran
setenv F77 ${topdir}/bin/gfortran
setenv F90 ${topdir}/bin/gfortran

prepend-path PATH ${topdir}/include
prepend-path PATH ${topdir}/bin
prepend-path MANPATH ${topdir}/man
prepend-path LD_LIBRARY_PATH ${topdir}/lib

We will store our module files under /workspaces/linux/hpc/modulefiles.

export MODULEPATH=${MODULEPATH}:/workspaces/linux/hpc/modulefiles

Now we can test our module:

gcc --version
module --help
module avail
module load gcc-12.3.0 # module add gcc-12.3.0
gcc --version
module list
module purge # module remove gcc-12.3.0
gcc --version

Useful commands to use in module file:

CommandDescription
setenvset the specified environment variable to the supplied value.
unsetenvunset the specified environment variable. If an argument is supplied, then, while unloading the module, the variable will be re-set, to that argument.
append-pathput the supplied argument on the end of the specified variable. The variable should be a list of colon-separated entries. PATH is one such variable.
prepend-pathput the supplied argument at the start of the specified variable.
remove-pathremove the supplied argument from the specified variable, wherever along its length it might be.
prereqinsist that the specified module be loaded before loading the current module. (Note: Usually, it's easier to just put a "module load" in to get the dependency.)
conflictinsist that the specified module not be loaded.
module loadload the specified module.
module-whatisfollow with a help string, to be printed whenever the user issues the "module whatis" command on this module. It should briefly describe the software loaded by this module.

Resources