Environment Modules 是一种简化的 Shell 初始化工具,可用于修改终端环境。
安装
依赖 Tcl 版本 >= 8.5,查看 Tcl 版本:
1
| echo puts \$tcl_version | tclsh
|
依赖 Tcl 配置脚本 tclConfig.sh
安装 tcl 工具,并通过 --with-tcl 指定 tclConfig.sh 文件所在路径
下载安装包(此处需关闭代理)
1 2
| curl -LJO https://github.com/cea-hpc/modules/releases/download/v5.4.0/modules-5.4.0.tar.gz tar xfz modules-5.4.0.tar.gz
|
默认安装路径为 --prefix=/usr/local/Modules,此处安装至 /opt/tools/Modules/build 路径下,modulefile文件默认路径为 --modulefilesdir=/usr/local/Modules/modulefiles ,此处设置为 /opt/tools/Modules/modulefiles
1
| ./configure --prefix=/opt/tools/Modules/build --modulefilesdir=/opt/tools/Modules/modulefiles --with-tcl=/usr/lib
|
编译并安装
配置
初始化
- 全局初始化,将 profile 文件添加至
/etc/profile.d 中
1 2
| ln -s /opt/tools/Modules/build/init/profile.sh /etc/profile.d/modules.sh ln -s /opt/tools/Modules/build/init/profile.csh /etc/profile.d/modules.csh
|
- 单用户初始化,添加至
~/.bashrc 中
1
| source /opt/tools/Modules/build/init/bash
|
- 验证初始化完成
module -V
添加模块路径及模块
-
通过 ./config --etcdir 选项指定路径下的 initrc 文件定义 module paths modulefiles 等,文件默认路径为 PREFIX/etc
-
定义 module paths to enable by default,initrc 文件中已添加 ./config --modulefilesdir 定义的路径 module use --append {PREFIX/modulefiles},此处在initrc 文件中添加自定义路径 /opt/tools/Modules/Compiler
1 2
| module use --append {/opt/tools/Modules/Compiler}
|
执行 echo $MODULEPATH,显示模块路径
1
| /opt/tools/Modules/modulefiles:/opt/tools/Modules/Compiler
|
模块路径也可通过 modulespath 文件定义,且优先级高于 initrc 文件;添加时直接写入文件路径即可 /path/to/other/modulefiles
- 在自定义路径
/opt/tools/Modules/Compiler 下添加文件 gcc/11.3.2 及 gcc/13.2.0,文件内首行输入 #%Module,工具会自动将文件识别为 modulefile,执行 module avail 可显示已识别的模块路径及模块
1 2
| ------------ /opt/tools/Modules/Compiler ------------ gcc/11.3.2 gcc/13.2.0
|
定义默认模块
- 在
initrc 文件中定义默认导入模块
1 2 3 4 5
| if {[is-saved default]} { module restore } else { module load {gcc/13.2.0} }
|
在打开新终端时会自动导入该模块 Loading gcc/13.2.0
- 在
.modulerc 文件中定义默认版本,首先在 gcc/11.3.2 中定义版本变量
1 2
| set ModulesVersion 11.3.2
|
对应需要定义默认版本程序的文件夹下添加文件 gcc/.modulerc,定义默认版本
1 2
| module-version ./11.3.2 default
|
执行 ml load gcc && ml list 会导入默认版本模块
1 2
| Currently Loaded Modulefiles: 1) gcc/11.3.2
|
默认版本也可以通过 .version 文件定义
编写 Modulefile
脚本转换
Modulefile 通过 Tcl 语言编写,可通过命令 sh-to-mod 将 shell 脚本转换为 modulefile 格式
1
| sh-to-mod shell shellscript [arg...]
|
例如,bash 脚本 gcc_13.2.0.sh
1 2 3 4 5 6 7 8 9
| #!/bin/bash version="13.2.0" prefix="/opt/compiler/gcc/13.2.0" alias gcc="gcc-13" export GCC_HOME="$prefix" export GCC_INCLUDE="$prefix/include" export GCC_LIB="$prefix/lib" export PATH="$prefix/bin:$PATH" EOF
|
通过 module sh-to-mod bash gcc_13.2.0.sh 转换脚本
1 2 3 4 5 6
| prepend-path PATH /opt/compiler/gcc/13.2.0/bin set-alias gcc gcc-13 setenv GCC_HOME /opt/compiler/gcc/13.2.0 setenv GCC_INCLUDE /opt/compiler/gcc/13.2.0/include setenv GCC_LIB /opt/compiler/gcc/13.2.0/lib
|
关键字
常见关键字如下
| 关键字 |
作用 |
| set |
设置局部变量 |
| set-alias name string |
设置指令的别名 |
| setenv variable value |
设置环境变量值 |
| prepend-path variable value |
添加或预置 value 至 variable |
| module-whatis string |
用于 module whatis 显示信息 |
| conflict modulefile |
列出与当前模块冲突的模块文件列表 |
| prereq-all [options] modulefile |
声明当前加载模块的依赖模块文件列表 |
| module-alias name modulefile |
设置模块文件的别名 |
对于 bash 脚本 gcc_13.2.0.sh,编写对应 modulefile 如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| #%Module
proc ModulesHelp {} { puts stderr "gcc $::version" puts stderr {Copyright (C) 2023 Free Software Foundation, Inc.} puts stderr {} }
set version 13.2.0 set prefix /opt/compiler/gcc/13.2.0 set-alias gcc gcc-13
module-whatis "load gcc-$version"
conflict openmpi prereq x86-gnu
setenv GCC_HOME $prefix setenv GCC_INCLUDE $prefix/include prepend-path LD_LIBRARY_PATH $prefix/lib prepend-path PATH $prefix/bin
|
- 开头
#%Module 称为 Modules magic cookie,工具会自动将文件识别为 modulefile
- Tcl procedure
ModulesHelp 用于添加模块帮助信息,可通过 ml help gcc/13.2.0 输出
1 2 3 4 5 6 7
| ------------------------------------------------------------------- Module Specific Help for /opt/tools/Modules/Compiler/gcc/13.2.0:
gcc 13.2.0 Copyright (C) 2023 Free Software Foundation, Inc.
-------------------------------------------------------------------
|
- module-whatis 添加模块信息,执行
ml whatis gcc/13.2.0
1
| gcc/13.2.0: load gcc-13.2.0
|
- 执行
ml show gcc/13.2.0 显示模块完整信息
1 2 3 4 5 6 7 8 9 10 11 12
| ------------------------------------------------------------------- /opt/tools/Modules/Compiler/gcc/13.2.0:
set-alias gcc gcc-13 module-whatis {load gcc-13.2.0} conflict openmpi prereq x86-gnu setenv GCC_HOME /opt/compiler/gcc/13.2.0 setenv GCC_INCLUDE /opt/compiler/gcc/13.2.0/include prepend-path LD_LIBRARY_PATH /opt/compiler/gcc/13.2.0/lib prepend-path PATH /opt/compiler/gcc/13.2.0/bin -------------------------------------------------------------------
|
高级功能
- 通过 Tcl 编程简化定义,参考案例 Environment Modules Cookbook
- 案例文件相应文件参考 Github-Modules Example
- 更多
module 选项参考 Sub Commands,环境变量参考 Environment
- 更多
modulefile 命令参考 Specific Tcl Commands,变量参考 Variables
常用指令
| Command |
Description |
| ml avail |
查看可用模块 |
| ml load/add modulefile |
加载模块 |
| ml list |
查看已加载模块 |
| ml unload/rm modulefile |
卸载模块 |
| ml switch/swap mod1 mod2 |
卸载mod1并加载mod2 |
| ml purge |
清空模块 |
| ml whatis modulefile |
显示模块信息 |
| ml show modulefile |
显示模块详细信息 |
| ml help modulefile |
显示模块帮助信息 |
| module lint modulefile |
检查模块语法 |
| module use modulepath |
添加模块路径 |
| module source modulefile |
执行脚本文件 |
| module sh-to-mod shellfile |
脚本文件转换 |
| module help |
帮助 |
参考
Environment Modules 文档
Ubuntu Source Package: modules
环境变量管理工具 Modules 安装和使用 - YEUNGCHIE
用Modules管理环境变量
使用 module 配置环境