Environment Modules 工具管理系统环境

通过 Modules 工具统一管理系统环境

编译器多版本切换

通过 apt 安装的编译器如 gcc g++ gfortran 等可通过 update-alternatives 工具切换

gcc

alias

使用 alias 会将 gcc 命令临时映射到对应版本,不会作用于整个系统

1
2
3
4
5
#%Module
set version 10.5.0
set-alias gcc gcc-10

conflict gcc

update-alternatives

安装其他 gcc 版本,使用 update-alternatives 工具添加并设置优先级(自动选择高优先级版本)

1
2
3
4
5
6
7
8
9
10
11
# install gcc
sudo apt -y install gcc-10 g++-10
# add to update-alternatives
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-13 13
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 10
# manual config
sudo update-alternatives --config gcc
# set version
sudo update-alternatives --set gcc /usr/bin/gcc-10
# remove version
sudo update-alternatives --remove gcc /usr/bin/gcc-10

通过 Modules 工具配置,文件结构如下

1
2
3
4
gcc
├── 10.5.0
├── 13.2.0
└── common

版本 Modules 文件如下

1
2
3
4
#%Module
set version 10.5.0
set moduledir [file dirname $ModulesCurrentModulefile]
source $moduledir/common

公共执行文件 common 内容如下

1
2
3
4
5
6
7
8
9
10
11
12
#%Module

proc ModulesHelp {} {
puts stderr "gcc $::version"
puts stderr {}
}

set scriptroot /opt/tools/Modules/script/gcc

module-whatis "load gcc-$version"

source-sh bash $scriptroot/$version

shell 脚本内容如下

1
2
3
#!/bin/bash
# gcc_version = "10.5.0"
sudo update-alternatives --set gcc /usr/bin/gcc-10

gfortran

Modules 配置方式与 gcc 基本一致

python 虚拟环境

python pip 直接安装工具会报错:

1
error: externally-managed-environment

该问题可通过创建虚拟环境解决:

1
2
3
4
5
6
7
8
9
10
11
# 安装 python3-venv
sudo apt install python3-venv
# 创建虚拟环境
mkdir ~/Environment && cd ~/Environment
python3 -m venv python
# 激活虚拟环境
source ~/Environment/python/bin/activate
# 虚拟环境安装
pip install xxxxx
# 退出虚拟环境
deactivate

通过 Modules 工具配置

1
2
3
4
5
6
7
8
#%Module

set version 24.0
set prefix /path/to/Environment/python

module-whatis "load python3 virtual environment"

source-sh bash $prefix/bin/activate

CMake

安装历史版本 CMake Release

1
2
3
4
5
6
7
# download
wget https://github.com/Kitware/CMake/archive/refs/tags/v2.8.12.2.tar.gz
tar -xvf v2.8.12.2.tar.gz && cd cmake-2.8.12.2
# build and instrall
mkdir build && cd build
../bootstrap --prefix=$HOME/local/cmake/cmake-2.8.12
make && make install

CMake 2.8 与 qt5 冲突

CMake Warning at Modules/FindQt4.cmake:659 (message):
/usr/bin/qmake reported QT_INSTALL_LIBS as “/usr/lib/x86_64-linux-gnu” but
QtCore could not be found there. Qt is NOT installed correctly for the
target build environment.
Call Stack (most recent call first):
Tests/RunCMake/CMakeLists.txt:104 (find_package)

CMake Error at /usr/lib/x86_64-linux-gnu/cmake/Qt5Core/Qt5CoreConfig.cmake:2 (message):
Qt 5 Core module requires at least CMake version 3.1.0
Call Stack (most recent call first):
Tests/RunCMake/CMakeLists.txt:105 (find_package)

参考

  1. How to switch between multiple GCC and G++ compiler versions on Ubuntu 22.04 LTS Jammy Jellyfish
  2. Installing GFortran
  3. Ubuntu Manuals update-alternatives
0%