Binding specific CUDA versions to different Conda env

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.

  • 12
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值