Install TensorFlow with CUDA, cuDNN in Anaconda Environment

Introduction

Last update: 11/11/2023 – TensorFlow 2.14

Notice:

Prerequisites

NVIDIA Driver: a.k.n. Graphics card driver. 535 is the latest version as I am writing this article.

sudo apt install nvidia-driver-535

Then, reboot your system. Check the driver is properly installed with the following command.

nvidia-smi

Install CUDA (a.k.a CUDA Toolkit) and cuDNN

Assuming an Anaconda environment you use is jaerock. Python 3.10 is chosen to be safe since TensorFlow 2.14 requires 3.9 - 3.11. Also, note that cuDNN 8.6 is recommended according to TensorFlow 2.14 software requirements. No conda package of cuDNN 8.6 was found in the Anaconda default channel. So, 8.9 is used instead.

conda create --name jaerock python=3.10
conda activate jaerock
conda install cudatoolkit=11.8 cudnn=8.9

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 the conda environment or just close the current working terminal and re-open it.

conda activate jaerock
pip install tensorflow[and-cuda]

After installing, you can test if the TensorFlow runs 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 see Num GPUs Available: #

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

1