Conda Environments¶
Since Python supports a wide range of additional libraries in machine learning or data science research, it is not always possible to install every package on HPC. Also, users sometimes need to use a specific version of Python or its libraries to conduct their research. Therefore, in that case, users can build their own Python version along with a specific library. One of the ways to accomplish this is to use Conda.
What is Conda¶
Conda as a package manager helps you find and install packages. If you need a package that requires a different version of Python, you do not need to switch to a different environment manager, because conda is also an environment manager.
Availability¶
| Software | Version | Dependent Toolchain | Module Load Command |
|---|---|---|---|
| Miniforge3 | 24.11.3-0 | - | module load Miniforge3/24.11.3-0 |
Conda User Commands¶
| Task | Command |
|---|---|
| Activate environment: | conda activate [environment_name] |
| Deactivate environment: | conda deactivate [environment_name] |
| Show the list of environments: | conda env list |
| Delete environment: | conda remove --name [environment_name] --all |
| Export environment: | conda env export > [environment_name].yml |
| Import environment from YAML: | conda env create -f [environment_name].yml |
| Import environment to different location: | conda env create -f [environment_name].yml -p [PATH] |
Create and Activate a Conda Virtual Environment¶
Tip
The login node has limited memory and processing resources, which can slow down conda environment creation and package installation. It is recommended to start an interactive session on a compute node before creating or modifying your conda environment.
Load the Miniforge3 Module
Create Environment with conda¶
To create an environment use the conda create command. Once the environment is created, you need to use conda activate to activate the environment.
To create an environment with a specific python version, use conda create --name ENV python=3.9 where ENV is the name of the environment. You can choose any environment name of your choice.
Info
You don't need to specify the Python version when creating a Conda environment. If you don't specify it, Conda will use the latest available version by default. In that case, simply use conda create --name ENV.
Activate and Deactivate Conda Environment¶
Once you create an environment, you need to activate the environment to install python packages
Use conda activate ENV to activate the Conda environment (ENV is the name of the environment). Following the activation of the conda environment, the name of the environment appears at the left of the hostname in the terminal.
Once you finish the installation of Python packages, deactivate the conda environment using conda deactivate ENV.
Warning
Please note that you may need to create multiple Conda environments, as some packages may not work in a single environment. For example, if you want to install PyTorch and TensorFlow, it's advisable to create separate environments as sometimes both packages in a single environment can cause errors. To create another environment make sure to deactivate the previous environment by using the conda deactivate command.
Install Python Packages Via Conda¶
Once Conda environment is activated, you can install packages via conda install package_name command. For example, if you want to install matplotlib, you need to use
conda-forge is the name of the conda channel.
Warning
Make sure to activate the conda environment prior to installing Python packages.
Conda Channel¶
Conda Channel refers to a repository or collection of software packages that are available for installation using Conda. Conda Channels are used to organize and distribute packages, and they play a crucial role in the Conda ecosystem. Channels can be specified using the --channel or -c option with the conda install command i.e.
conda install -c channel_name package_name.
Tip
Since memory and CPU usage are limited, it's better to start an interactive session with the compute node whenever you are installing Python packages via Conda.
In the above example, the command conda install -c conda-forge matplotlib will install matplotlib from conda-forge channel which is a community-maintained collection of Conda packages where a wide range of packages contributed by the community are available.
Users can prioritize channels by listing them in a specific order, so that Conda searches channels in the order they are listed, installing the first version of a package that it finds. To list the channels, create a file .condarc in the $HOME directory and add the following
.condarc is that you don't have to mention the channel name every time you install a package. However, please note that you still need to use the channel name if you want to install Python packages that require a specific channel other than the conda-forge channel.
Examples of Conda Environemnt¶
Here, we provide some examples of how to use conda to install applications.
Note
For the following examples please make sure to start an interactive session on GPU node.
Install TensorFlow with GPU¶
-
The following example will create a new conda environment and install TensorFlow in the environment.
-
Activate the new 'tf' environment
-
Install tensorflow-gpu
-
Check if TensorFlow can be loaded
-
Check if TensorFlow is compiled with GPU
You can also verify using this simple TensorFlow test program to make sure the virtual env can access a GPU.
tf.gpu.test.py
Slurm script to submit the job
Next, deactivate the environment using conda deactivate tf command.
Install PyTorch with GPU¶
- To install PyTorch with GPU, load the
Miniforge3module as described above and then use the following
Info
In the example above, we mentioned the channel name as we intend to install PyTorch and PyTorch-CUDA from a specific channel. For the default channel please see Channels.
- Check the Torch version
- Check the CUDA version
- Check whether Torch is compiled with CUDA
Warning
While checking the CUDA version or PyTorch compilation using the commands mentioned above, make sure to start an interactive session on a GPU node; otherwise, the command will not recognize CUDA or the GPU.
- A simple PyTorch test program is given below to check whether PyTorch has been installed properly. Program is called
torch_tensor.py
User can use the following job script to run the script.
torch-cuda.submit.sh
Warning
When working with Python, it is generally advised to avoid mixing package management tools such as pip and conda within the same environment. Pip and Conda manage dependencies differently, and their conflict can lead to compatibility issues and unexpected behavior. Mixing the two can result in an environment where packages installed with one tool may not interact seamlessly with those installed using the other.
Mamba: The Conda Alternative¶
Mamba is a fast, robust, and cross-platform package manager and particularly useful for building complicated environments, where conda is unable to 'solve' the required set of packages within a reasonable amount of time.
Users can install packages with mamba in the same way as with conda.
Example of Installing PyTorch via mamba¶
This will install pytorch in the torch-cuda environment.
Export and Import Conda Environment¶
Exporting and importing Conda environments allows users to capture and reproduce the exact set of dependencies for a project. With Conda, a popular package and environment management system, users can export an environment, including all installed packages, into a YAML file. This file can then be shared or version-controlled. Importing the environment from the YAML file on another system ensures consistent dependencies, making it easier to recreate the development or execution environment.
Tips
When installing Python packages via Conda, ensure that you perform the installation on the compute node rather than the login node. The CPU and memory resources on login nodes are limited, and installing Python packages on the login node can be time-consuming. To avoid this, initiate an tnteractive session with compute node.
Export Conda Environment¶
To export a conda environment to a new directory or a different machine, you need to activate the environment first that you intend to export. Please see Conda environment on how to activate the environment. Once your environment is activated, you can export it to a YAML file:
my_environment.yml file to make sure it has the correct environment name and other settings. The last line of the file specifies the path of the environment.
Once the YAML file is ready, you can transfer the my_environment.yml file to the new machine or directory where you want to replicate the environment. See cluster file transfer for details on transferring the files to clusters.
Set Different Location for Conda Environment and Package¶
Since Conda, by default, downloads packages and creates environments in the $HOME directory, users might encounter disk quota errors if multiple environments are created. In such cases, please follow the above steps to move the existing environments from $HOME to /project. For future environment and package downloads, create a .condarc file in the $HOME directory and add the following:
Replace /path/to/custom/conda/envs/directory with the path you want to use.
Import Environment on New Machine¶
On the new machine, first load Anaconda and initialize conda as before. Then, create the environment from the YAML file:
conda activate my_env where my_env is the environment name.
You can check your current environments using conda env list.
Importing to a Different Location¶
If you want to import the conda environment to a different location, use the --prefix or -p option
Warning
It is advisable to use the /project directory to store the Conda environment rather than using the $HOME directory. On Wulver, the storage space on $HOME is limited (50G) and cannot be increased. See Wulver Filesystems for details.