How to use the command 'module' (with examples)
- Linux
- December 17, 2024
The module
command is a powerful utility commonly used in high-performance computing (HPC) environments to manage and modify the user’s environment. It allows users to dynamically change their shell environment and access specific software versions or libraries without altering system-wide settings. By using the module
command, users can easily switch between different software toolchains, set up paths, and handle environment variables efficiently.
Use case 1: Display available modules
Code:
module avail
Motivation: Before loading any specific module, you may want to see a list of all the available modules and their versions on the system. This command provides a comprehensive view of the software packages that can be accessed or loaded, helping users determine which modules they might need for their tasks.
Explanation:
The avail
argument is used with the module
command to request a list of all available modules in the current module path. It checks the directories specified in the module path and lists all the modules, which can be immensely helpful when you are unsure of the exact module names or the available versions you can work with.
Example Output:
----------------------------- /usr/share/modulefiles -----------------------------
R/3.6.1 gcc/9.3.0 python/3.8.5 openmpi/4.0.3
Use case 2: Search for a module by name
Code:
module avail module_name
Motivation: When a user knows the name of a module but is not sure if it is available on the system or needs to verify its version, this command filters the list of modules to those matching the specified name. This is particularly useful when dealing with large installations where numerous modules are managed.
Explanation:
The avail
followed by module_name
lets the user query the module system to check for specific modules by name. It acts like a search function and only returns the modules that have names matching what was specified, thus narrowing down the options for easier selection.
Example Output:
----------------------------- /usr/share/modulefiles -----------------------------
python/3.8.5 python/3.9.1
Use case 3: Load a module
Code:
module load module_name
Motivation: To use the functionality of a specific software package, it typically needs to be loaded into the current environment. This command sets up the required environment variables and paths so the software or library can be accessed and executed.
Explanation:
The load
keyword along with module_name
updates the user’s environment to include the module’s settings. This process modifies the PATH and other pertinent environment variables, making the tools and functionalities of the module readily available for use in the current session.
Example Output:
Loading module 'python/3.8.5'
Use case 4: Display loaded modules
Code:
module list
Motivation: Users often need to confirm which modules are currently active in their session, especially when dealing with complex workflows that rely on specific software packages or library versions. This command shows all modules loaded at that time.
Explanation:
The list
argument in the module
command tells the system to return a list of currently loaded modules. It is a convenient way to view which modules are influencing the session and to confirm that the desired modules have been correctly loaded.
Example Output:
Currently Loaded Modulefiles:
1) gcc/9.3.0 2) python/3.8.5
Use case 5: Unload a specific loaded module
Code:
module unload module_name
Motivation: There are instances where a specific module needs to be deactivated, whether to resolve conflicts, switch versions, or to clean up the environment after completing a specific task. This command allows users to remove a module from the current session.
Explanation:
The unload
followed by module_name
instructs the system to remove the influence of the specified module from the user’s environment, undoing any changes made when the module was loaded. This helps in effectively managing and controlling the software environment.
Example Output:
Unloading module 'python/3.8.5'
Use case 6: Unload all loaded modules
Code:
module purge
Motivation: Sometimes a user may want to start with a clean slate, free from any active module configuration. This command unloads all previously loaded modules, which can be essential for avoiding unexpected behavior due to lingering settings from past environments.
Explanation:
The purge
command completely removes all modules from the current environment, resetting it to its default state. This is useful in preparing for a new workflow or testing set-up without interference from previously loaded modules.
Example Output:
Unloading all loaded modules...
Use case 7: Specify user-created modules
Code:
module use path/to/module_file1 path/to/module_file2 ...
Motivation: In some cases, users may create their own modules, or alternative modulefiles, which are not maintained in the system’s default module path. To use these custom modules, you need to specify their location, enabling the usage of personalized setups not typically found in standard repositories.
Explanation:
The use
keyword, followed by the paths to custom module files, informs the system to include these paths in the module search path. This allows custom or experimental modules to be accessed just like the standard ones, enabling flexible and specialized environments tailored to the user’s requirements.
Example Output:
Adding /path/to/module_file1 to module search paths
Adding /path/to/module_file2 to module search paths
Conclusion:
Using the module
command provides users with a flexible and dynamic way to manage software environments on Unix-like systems. By understanding and applying the shown use cases, users can customize their computational setups to suit specific needs and tasks efficiently. Whether managing complex dependencies or simply switching between different software versions, the module
command provides a streamlined approach to environment management.