CUDA and cuDNN inside a Conda Env

DO NOT INSTALL cuda through $ sudo apt install cuda since this will install the latest NVIDIA driver as well without asking. The newest NVIDIA driver might not work with a particular kernel version. Through my ordeals, I figured out that only some particular combinations work.

The safest way to install CUDA is to use a conda environment. First, install cuda and cudnn inside your conda environment. All the conda related libraries are located in ~/anaconda3/envs/<env-name>/lib. To let your environment know the location of the CUDA libraries LD_LIBRARY_PATH needs to be used.

Activate the environment first. Assuming the environment name is env-name, the command is like this.

conda activate env-name

Then run the following commands.

mkdir -p $CONDA_PREFIX/etc/conda/activate.d
echo 'export OLD_LD_LIBRARY_PATH=${LD_LIBRARY_PATH}' > $CONDA_PREFIX/etc/conda/activate.d/env_vars.sh
echo 'export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$CONDA_PREFIX/lib/' >> $CONDA_PREFIX/etc/conda/activate.d/env_vars.sh

mkdir -p $CONDA_PREFIX/etc/conda/deactivate.d
echo 'export LD_LIBRARY_PATH=${OLD_LD_LIBRARY_PATH}' >  $CONDA_PREFIX/etc/conda/deactivate.d/env_vars.sh
echo 'unset OLD_LD_LIBRARY_PATH' >> $CONDA_PREFIX/etc/conda/deactivate.d/env_vars.sh

Deactivate your environment and activate it again. Check if your TensorFlow properly works with GPUs.

python3 -c "import os; os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'; import tensorflow as tf; print('Num GPUs Available: ', len(tf.config.list_physical_devices('GPU')))"

You will sse Num GPUs Available: #

If # is other than 0, you are all set.

6
1

Comments are closed.