How to Install CUDA and cuDNN: A Step-by-Step Guide
CUDA (Compute Unified Device Architecture) and cuDNN (CUDA Deep Neural Network library) are essential tools for accelerating deep learning and machine learning applications on NVIDIA GPUs. In this guide, we’ll provide a straightforward, step-by-step process to install CUDA and cuDNN on your system.
What Are CUDA and cuDNN?
- CUDA: A parallel computing platform and API by NVIDIA that allows software to use GPU power for computing tasks.
- cuDNN: A GPU-accelerated library for deep learning primitives such as convolutions, pooling, and activation functions, designed to work seamlessly with CUDA.
System Requirements
Before installing CUDA and cuDNN, ensure the following:
- NVIDIA GPU: Check if your GPU is compatible with the version of CUDA you plan to install. Visit NVIDIA’s compatibility page for details.
- Operating System: CUDA supports Linux, Windows, and macOS. This guide covers Windows and Linux.
- Driver: Install the latest NVIDIA GPU driver for your system.
Step 1: Install NVIDIA GPU Driver
- Download the Driver:
- Visit the NVIDIA Driver Downloads page and select your GPU model and operating system.
- Install the Driver:
- Follow the on-screen instructions to complete the installation. Reboot your system if required.
Step 2: Download CUDA Toolkit
- Visit the CUDA Toolkit Page:
- Go to the CUDA Toolkit Downloads page.
- Select the Correct Version:
- Choose the version compatible with your operating system and GPU. Check compatibility with your deep learning framework (e.g., TensorFlow, PyTorch).
- Download and Install:
- Download the installer and follow the installation wizard.
On Linux, use the terminal:
sudo dpkg -i cuda-repo-<version>.deb
sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/<distribution>/x86_64/7fa2af80.pub
sudo apt update
sudo apt install cuda
4.Add CUDA to PATH:
Add the CUDA binary directory to your system’s PATH environment variable:
Windows: Go to Environment Variables > System Variables > Edit Path and add:
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\<version>\bin
Linux: Add the following to ~/.bashrc
:
export PATH=/usr/local/cuda-<version>/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-<version>/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
Reload the shell:
source ~/.bashrc
Step 3: Verify CUDA Installation
Check CUDA Version: Open a terminal or command prompt and run:
nvcc --version
This will display the installed CUDA version.
Test CUDA:
Navigate to the CUDA samples directory:
cd /usr/local/cuda/samples
Compile and run a sample:
make
./bin/x86_64/linux/release/deviceQuery
Step 4: Install cuDNN
- Download cuDNN:
- Visit the NVIDIA cuDNN Downloads page and log in or create an NVIDIA Developer account.
- Select the Version:
- Choose the version that matches your installed CUDA version.
- Install cuDNN:
Windows:
Extract the downloaded .zip
file and copy the files to the corresponding CUDA directories
cudnn-<version>\bin → C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\<version>\bin
cudnn-<version>\include → C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\<version>\include
cudnn-<version>\lib → C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\<version>\lib
Linux:
Extract and copy the files to the CUDA directory:
sudo cp cuda/include/cudnn*.h /usr/local/cuda/include
sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64
sudo chmod a+r /usr/local/cuda/include/cudnn*.h /usr/local/cuda/lib64/libcudnn*
Step 5: Verify cuDNN Installation
Check cuDNN Version:
Verify that cuDNN is installed correctly:
cat /usr/local/cuda/include/cudnn_version.h | grep CUDNN_MAJOR -A 2
Test cuDNN:
Use a deep learning framework (e.g., TensorFlow or PyTorch) to confirm GPU acceleration.
Step 6: Test CUDA and cuDNN with TensorFlow or PyTorch
Install TensorFlow or PyTorch:
pip install tensorflow
# or
pip install torch torchvision
Run a Test Script:
import tensorflow as tf
print("GPU Available:", tf.config.list_physical_devices('GPU'))
# Or for PyTorch
import torch
print("CUDA Available:", torch.cuda.is_available())
Common Issues and Troubleshooting
- Driver or CUDA Mismatch:
- Ensure your GPU driver and CUDA versions are compatible.
- Check NVIDIA’s compatibility table.
- PATH Issues:
- Ensure CUDA and cuDNN directories are added correctly to the PATH or LD_LIBRARY_PATH variables.
- Framework Errors:
- Use versions of TensorFlow or PyTorch compatible with your installed CUDA and cuDNN versions.
Conclusion
Installing CUDA and cuDNN is essential for leveraging the full power of NVIDIA GPUs in deep learning and other compute-intensive tasks. By following the steps outlined in this guide, you can set up a robust environment for your AI projects. With proper configuration, you can enjoy faster training and inference for your machine learning models.