To bind specific CUDA versions to different Conda environments, you can set environment-specific variables within each Conda environment. This way, each environment will use its respective CUDA version automatically when activated. Here's how you can achieve this:
Step 1: Locate Your CUDA Installations
Ensure that you know where each CUDA version is installed. Typically, they are located in `/usr/local/`, e.g., `/usr/local/cuda-12.2` for CUDA 12.2 and `/usr/local/cuda-11.3` for CUDA 11.3.
Step 2: Create or Modify Conda Environments
If you haven't already created the Conda environments, you can create them using:
conda create -n env1 python=3.x # Replace 3.x with the specific Python version you want
conda create -n env2 python=3.x
Step 3: Set Environment-Specific Variables
You'll need to set the `PATH` and `LD_LIBRARY_PATH` environment variables for each environment to point to the correct CUDA installation. You can do this by adding activation and deactivation scripts to the Conda environments.
For env1 (CUDA 12.2):
1. Navigate to the environment's directory:
cd ~/miniconda3/envs/env1 # or ~/anaconda3/envs/env1 depending on your installation
2. Create a directory for activation and deactivation scripts if they don't already exist:
mkdir -p ./etc/conda/activate.d
mkdir -p ./etc/conda/deactivate.d
3. Create an activation script:
vim ./etc/conda/activate.d/env_vars.sh
Add the following lines:
#!/bin/sh
export OLD_PATH=$PATH
export OLD_LD_LIBRARY_PATH=$LD_LIBRARY_PATH
export PATH=/usr/local/cuda-12.2/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda-12.2/lib64:$LD_LIBRARY_PATH
4. Create a deactivation script:
vim ./etc/conda/deactivate.d/env_vars.sh
Add the following lines:
#!/bin/sh
export PATH=$OLD_PATH
unset OLD_PATH
export LD_LIBRARY_PATH=$OLD_LD_LIBRARY_PATH
unset OLD_LD_LIBRARY_PATH
For env2 (CUDA 11.3):
Repeat the steps above for `env2`, but replace the paths with those pointing to CUDA 11.3:
- Activation script: `/usr/local/cuda-11.3/bin` and `/usr/local/cuda-11.3/lib64`
- Deactivation script: Reset to `OLD_PATH` and `OLD_LD_LIBRARY_PATH`
Step 4: Activate the Environments and Test
Activate each environment and verify that the correct CUDA version is being used:
conda activate env1
nvcc --version # Should show CUDA 12.2
conda deactivate
conda activate env2
nvcc --version # Should show CUDA 11.3
This setup ensures that each time you activate a specific Conda environment, it automatically configures itself to use the designated CUDA version.