Contact Advanced Research Computing
Mambaforge – Environments
Mamba environments allow you to isolate packages between environments to ensure that the correct dependency versions are used for your programs or scripts. Additionally, environments ensure that there are no dependency and dependency version collisions created due to having a monolithic set of dependencies for everything.
We also have anaconda3 available as a module, but we have deliberately chosen mambaforge for use within our documentation due to the ability to be a drop-in replacement for Anaconda and for the major improvements listed on the mamba Github page, particularly the parallel downloads and faster dependency solving.
More information on conda/mamba environments can be found on the Conda documentation page. While the main command on that page uses the conda command, you can replace it with mamba and expect the same results.
Loading the Mambaforge Module
In order to run any mamba commands on Monsoon, you must first load the mamba executable into your PATH by executing the following command:
module load mambaforge
Once you have loaded the mambaforge module, you can check that mamba is now available by using the following command:
mamba --version
Creating an Environment
To create a basic environment, use the mamba create command:
mamba create -n [env_name]
You can also specify a specific version of Python.
mamba create -n [env_name] python=[ver_num]
For example, to create an environment with the name demo_environment with Python version 3.10, a mamba create command would look like:
mamba create -n demo_environment python=3.10
Additionally, you can specify where the environment should be located on the filesystem by replacing the -n [env_name] flag with ‐‐prefix [directory]:
mamba create --prefix /scratch/abc123/conda_envs/demo_environment python=3.10
Note: In order to perform actions on an environment in a custom directory such as activating and removing such environment, you must provide the full path to the environment in place of the environment name.
Activating an Environment
Once an environment is created, it still needs to be activated in order to use it. This can be done using the mamba activate command.
mamba activate [env_name]
To see what mamba environments are currently available to you, you can use the following command:
mamba info --envs
For example, to activate the demo_environment created in the previous section, a mamba activate command would look like:
mamba activate demo_environment
Once you have activated your environment, your shell prompt will now display the name of the active environment before your regular prompt.
(demo_environment) [abc123@wind]:~$
Exporting an Environment
Exporting environments is particularly useful if you need to move your environment from one location to another, particularly as conda environments can take up a lot of disk space. One such action would be moving an environment from your /home to your /scratch directory.
To export an environment, use the mamba list command and write the output into a file:
mamba list --explicit > [env_file]
Note: You must first activate your target conda environment. Otherwise, the default conda environment will be exported, which probably isn’t what you are trying to do.
For example, to export the active mamba environment to a file called demo_environment_export.txt, the commands would look like:
mamba activate demo_environment
mamba list --explicit > demo_environment_export.txt
Note: Exported mamba environments are platform-dependent. To identify what platform your exported environment will work on, view the 3rd line in your export file containing the platform keyword.
# platform: linux-64
Note: This export process will only export the installed packages and does not include mamba environment configuration.
Importing an Environment
To import an environment, use the mamba create command:
mamba create -n [env_name] --file [env_file]
For example, to import an environment with the target name imported_environment from the file demo_environment_export.txt, the command would look like:
mamba create -n imported_environment --file ./demo_environment_export.txt
Note: Exported conda environments are platform-dependent. To identify what platform your exported environment will work on, view the 3rd line in your export file containing the platform keyword.
# platform: linux-64
Note: This import process will only import the packages listed in the import file and does not include mamba environment configuration.
In order to specify where the conda environment should be imported to, replace the -n flag along with the name with ‐‐prefix [path]:
mamba create --prefix /scratch/abc123/conda_envs/demo_environment --file ./demo_environment_export.txt
Note: In order to perform actions on an environment in a custom directory such as activating and removing such environment, you must provide the full path to the environment in place of the environment name.
Deactivating an Environment
To deactivate an environment, use the following command:
mamba deactivate
Once you have ran this command, all the programs within the environment that were executable from your PATH will no longer be accessible, and any Python scripts ran will no longer use the environment-specific libraries that were previously accessible.
Additionally, your shell prompt will return back to normal.
[abc123@wind]:~$
Removing an Environment
To remove an environment, use the mamba remove command:
mamba remove -n [env_name] --all
This command will remove the specified environment along with all the packages that were installed inside of it.
If you installed your environment in a custom directory, use the following command instead:
mamba remove --prefix [env_dir] --all
To verify that the environment was removed, you can use the following command and check that your environment is no longer in the list:
mamba info --envs
Unloading the Mambaforge Module
Once you are finished using the mambaforge module, you can either exit your shell or run the following command:
module unload mambaforge
Once you have ran this command, the mamba command will no longer be executable as it will be removed from your PATH.
Something to keep in mind is that running this command is that it will NOT automatically deactivate your mamba environments. If you unloaded the mambaforge module before you deactivated the environment, you must first load the mambaforge module, deactivate the conda environment, then unload the mambaforge module again. Additionally, deactivating an environment after reloading the mambaforge module may remove the environment shell prompt. This can be fixed by activating another environment and deactivating it.
Setting the Default Environment Location
When installing conda packages, you may run into either a permission or a storage issue. To fix this, you will need to define a custom location for your mamba environments.
To do this, you will need to create and/or edit the file ~/.condarc using a program such as nano with the following contents (feel free to change the directory listed to a better location if it suits you):
envs_dirs:
- /scratch/<user_id>/conda/envs
pkgs_dirs:
- /scratch/<user_id>/conda/pkgs
Note: Make sure you replace <user_id> with your actual user id.
Once the file is present, you should be able to run your mamba commands without error. If you get any errors when running mamba commands, make sure the syntax is the same as in the example above.