[Server Management] Ubuntu20.04 installs the package management tool module and uses it
What Module
module is a tool that specializes in managing environment variables. The full name is module environment. It is generally used in software or runtime devices with multiple versions, and these environment variables need to be configured separately. Its official website is:
https://modules.readthedocs.io/en/latest/index.html
Usually, for multi-version software packages, we can modify ~/.bashrc
the files by modifying PATH
global LD_LIBRARY_PATH
environment variables such as . This is the simplest and rude method, but you need to be familiar with the installation location of each software package, in. When there are a large number of software packages, the server users may not know the installation path of the software packages, nor do they know which versions of the packages are available, which makes it very troublesome to switch software versions. The module lets us get rid of this trouble.
Install Module
The Module tool can be installed using binary compilation (the process is more complicated, refer to: Installing Modules on Unix ), or can be quickly installed using yum/apt, as follows:
- Redhat/Centos and other operating systems that use yum, the command is as follows:
sudo yum install environment-modules
- Ubuntu and other apt operating systems, the command is as follows:
sudo apt-get install environment-modules
Note: This article is for tool installation and management under Ubuntu20.04, using sudo apt-get install environment-modules
, so the installation path is /usr/share/modules
, if it is installed using source code, the installation path is specified by./configure --prefix=/usr/local/tools/modules ...
the –prefix of the command . At this time, the corresponding path below is also It has to be modified to the corresponding installation location!
After installing the module tool, you will find that it is not an executable binary file, you need to initialize the module tool once. Inside /usr/share/modules/init (note here, it may be module or modules) you can find binary initialization files for each script, find your current script, source this binary file, for example, you are a bash script , source /usr/share/modules/init/bash
after that, you can use the module tool.
cd /usr/share/modules/init
ls
source /usr/share/modules/init/bash
In order to avoid the need every time you open the terminal source /usr/share/modules/init/bash
, you need to add this command to /etc/profile
the file, as follows:
- Open
/etc/profile
the file:
sudo vim /etc/profile
- Add the following statement at the end of the file:
if [ -f /usr/share/modules/init/bash ]; then
source /usr/share/modules/init/bash
fi
Configure Module
The Module tool relies on the MODULEPATH environment variable to find the configuration information directory. That is to say, after you set up the directory structure and configure the environment variable, you only need to set the environment variable of this module, and then the module tool will automatically search for this path. All configuration information under .
echo $MODULEPATH
Returns the following results:
/etc/environment-modules/modules:/usr/share/modules/versions:/usr/share/modules/$MODULE_VERSION/modulefiles:/usr/share/modules/modulefiles
**Note: **In general, after we install the module, the MODULEPATH environment variable will be automatically configured, and no additional configuration is required.
When we want a software package to be called through the module module, after installing the software package, we need to /usr/share/modules/modulefiles
add the corresponding Modulefile file in the path. Therefore, we need to know how to write Modulefile files.
We can look at an example to understand the Modulefile file:
View /usr/share/modules/modulefiles
those currently in the path
cd /usr/share/modules/modulefiles
ls
Returns the following results:
dot module-git module-info modules null use.own
We use module avail
the command to see which packages are available
module avail
Returns the following results:
---------------------------------------- /usr/share/modules/modulefiles -----------------------------------------
dot module-git module-info modules null use.own
We look at the modules file,
cd /usr/share/modules/modulefiles
vim modules
You can see the following code:
#%Module1.0#####################################################################
##
## modules modulefile
##
proc ModulesHelp {
} {
global version prefix
puts stderr "\tmodules - loads the modules software & application environment"
puts stderr "\n\tThis adds $prefix/* to several of the"
puts stderr "\tenvironment variables."
puts stderr "\n\tVersion $version\n"
}
module-whatis "loads the modules environment"
# for Tcl script use only
set version 4.4.1
set prefix /usr/share/modules
setenv MODULESHOME $prefix
prepend-path PATH /usr/bin
prepend-path MANPATH /usr/share/man
# enable module versioning modulepath
#module use /usr/share/modules/versions
Here is a brief explanation of commonly used commands:
#%Module1.0
: Help identify this file as a modulefile, without this statement this file will not be recognized;prepend-path
: Add the tool path to the front of the environment variable;setenv
: Configure the environment variables you need into the system.
For specific commands, please refer to https://modules.readthedocs.io/en/latest/modulefile.html
In particular, if a software has multiple versions, we can /usr/share/modules/modulefiles
build a subdirectory for this software under the directory, and then build subdirectories of multiple versions under the subdirectory, in
For example, I have two versions of cuda here, and the installation paths are respectively /usr/local/cuda-11.6
, /usr/local/cuda-12.0
and the following operations need to be performed:
1. /usr/share/modules/modulefiles
Create a dedicated folder for cuda under the path
cd /usr/share/modules/modulefiles
sudo mkdir cuda
cd cuda
2. Write Modulefile files corresponding to different versions of cuda in the cuda folder:
(a) Write the Modulefile of cuda-11.6:
sudo vim 11.6
(b) Add the following code and save:
#%Module1.0#####################################################################
setenv CUDA_HOME /usr/local/cuda-11.6
prepend-path PATH /usr/local/cuda-11.6/bin
prepend-path LD_LIBRARY_PATH /usr/local/cuda-11.6/lib64
© Write the Modulefile of cuda-12.0:
sudo vim 11.6
(b) Add the following code and save:
#%Module1.0#####################################################################
setenv CUDA_HOME /usr/local/cuda-12.0
prepend-path PATH /usr/local/cuda-12.0/bin
prepend-path LD_LIBRARY_PATH /usr/local/cuda-12.0/lib64
3. Open a new terminal and test the available software packages:
module avail
Returns the following results:
---------------------------------------- /usr/share/modules/modulefiles -----------------------------------------
cuda/11.6 cuda/12.0 dot module-git module-info modules null use.own
It can be found that the module tool is already available cuda/11.6
, cuda/12.0
two versions
The configuration process of other multi-version software is the same as above, here is a summary:
1. Install multiple versions of the software package;
2. /usr/share/modules/modulefiles
Create a subdirectory for this software in the directory;
3. Create a subdirectory for each version in the subdirectory Modulefile file, use setenv
, prepend-path
and other commands to configure the corresponding global variables (this has to refer to the global variable configuration of the software itself);
4. Use source /usr/share/modules/init/bash
the command to refresh the module module.
Use Module
Here is a list of commonly used commands:
1. Display available modules
module avail
2. Load the module
module load/add [模块名称]
3. Uninstall the module
module unload/rm [模块名称]
4. Display the loaded modules
module list
This article briefly tests the version switching of CUDA:
- switch to cuda-11.6
module load cuda/11.6
module list
Returns the following results:
Currently Loaded Modulefiles:
1) cuda/11.6
Check whether the setting is effective:
nvcc -V
Returns the following results:
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2022 NVIDIA Corporation
Built on Tue_Mar__8_18:18:20_PST_2022
Cuda compilation tools, release 11.6, V11.6.124
Build cuda_11.6.r11.6/compiler.31057947_0
- switch to cuda-12.0
module unload cuda/11.6
module load cuda/12.0
module list
Returns the following results:
Currently Loaded Modulefiles:
1) cuda/12.0
Check whether the setting is effective:
nvcc -V
Returns the following results:
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2023 NVIDIA Corporation
Built on Fri_Jan__6_16:45:21_PST_2023
Cuda compilation tools, release 12.0, V12.0.140
Build cuda_12.0.r12.0/compiler.32267302_0
Reference:
https://modules.readthedocs.io/en/latest/INSTALL.html
https://www.fasteda.cn/post/22.html
https://blog.csdn.net/Michael177/article/details/ 121152904