Conda Environments¶
Conda is an open-source package and environment manager that, like spack, makes it easy to install, update, and run software. It works with packages in many languages, though it is most commonly associated with Python. Conda 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.
Loading and Unloading the Miniforge3 Module¶
In order to run any conda commands on Monsoon, you must first load the conda executable into your PATH by executing the following command:
Once you have loaded the miniforge3 module, you can check that conda is now available by using the following command:
Once you are finished using the miniforge3 module, you can either exit your shell or run the following command:
Once you have ran this command, the conda 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 conda environments. If you unloaded the miniforge3 module before you deactivated the environment, you must first load the miniforge3 module, deactivate the conda environment, then unload the miniforge3 module again. Additionally, deactivating an environment after reloading the miniforge3 module may remove the environment shell prompt. This can be fixed by activating another environment and deactivating it.
Creating and Deleting an Environment¶
To create a basic environment, use the conda create command:
You can also install packages while creating a conda environment by specifying them as arguments:
For example, to create an environment with the name demo_environment with the tree package, a conda create command would look like:
Additionally, you can specify where the environment should be located on the filesystem by replacing the -n [env_name] flag with ‐‐prefix [directory]:
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.
To remove an environment, use the conda remove command:
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:
To verify that the environment was removed, you can use the following command and check that your environment is no longer in the list:
Activating and Deactivating an Environment¶
Once an environment is created, it still needs to be activated in order to use it. This can be done using the conda activate command.
To see what conda environments are currently available to you, you can use the following command:
For example, to activate the demo_environment created in the previous section, a conda activate command would look like:
Once you have activated your environment, your shell prompt will now display the name of the active environment before your regular prompt.
If you recall earlier when creating the demo_environment conda environment, we installed the tree package so we have the tree command available:
To deactivate an environment, use the following command:
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.
Exporting and Importing 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 conda list command and write the output into a 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 conda environment to a file called demo_environment_export.txt, the commands would look like:
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.
Note
This export process will only export the installed packages and does not include conda environment configuration.
To import an environment, use the conda create command:
For example, to import an environment with the target name imported_environment from the file demo_environment_export.txt, the command would look like:
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.*
Note
This import process will only import the packages listed in the import file and does not include conda 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]:
conda create --prefix /scratch/$USER/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.
Setting the Default Environment Location¶
When installing conda packages, you may notice that environments take up a lot of space in your /home. To fix this, you will need to define a custom location for your conda environments. You can see where environments and downloaded packages are stored with the conda config –show command:
You can change the location of new environments and cached package downloads with the conda config –add command:
conda config --add envs_dirs "/scratch/$USER/conda/envs" # create new environments here
conda config --add pkgs_dirs "/scratch/$USER/conda/pkgs" # cache package downloads here
Once the file is present, you should be able to run your conda commands without error. If you get any errors when running conda commands, make sure the syntax is the same as in the example above.