CUDA
Anaconda
Installation Guide
GPU Computing
NVIDIA
How to check if cuda is installed correctly on Anaconda
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Verifying CUDA installation in an Anaconda environment requires checking multiple layers: the NVIDIA driver, the CUDA toolkit, cuDNN, and the deep learning framework (PyTorch or TensorFlow). Each component must be compatible with the others. The quickest checks are nvidia-smi for the driver, nvcc --version for the CUDA toolkit, and torch.cuda.is_available() or tf.config.list_physical_devices('GPU') for framework-level GPU detection.
Step 1: Check NVIDIA Driver
bash
# Check if the NVIDIA driver is installed and the GPU is visible
nvidia-smi1+-----------------------------------------------------------------------------+
2| NVIDIA-SMI 535.129.03 Driver Version: 535.129.03 CUDA Version: 12.2 |
3|-------------------------------+----------------------+----------------------+ | GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
4| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
5| 0 NVIDIA RTX 4090 Off | 00000000:01:00.0 On | Off |
6| 0% 35C P8 17W / 450W | 512MiB / 24564MiB | 0% Default | +-------------------------------+----------------------+----------------------+ ``` `nvidia-smi` shows the driver version and the maximum CUDA version the driver supports. If this command fails, the NVIDIA driver is not installed. The "CUDA Version" shown here is the driver's CUDA capability, not the installed toolkit version. ## Step 2: Check CUDA Toolkit ```bash # Check CUDA toolkit version nvcc --version # nvcc: NVIDIA (R) Cuda compiler driver # Cuda compilation tools, release 12.2, V12.2.140 # Check CUDA path which nvcc # /home/user/anaconda3/envs/myenv/bin/nvcc # Check conda-installed CUDA packages conda list | grep cuda # cudatoolkit 11.8.0 # cuda-nvcc 12.2.140 ``` `nvcc --version` reports the installed CUDA compiler version. If `nvcc` is not found, the CUDA toolkit may not be installed in your conda environment. ## Step 3: Check with PyTorch ```python import torch # Basic GPU check print(f"PyTorch version: {torch.__version__}") print(f"CUDA available: {torch.cuda.is_available()}") print(f"CUDA version: {torch.version.cuda}") print(f"cuDNN version: {torch.backends.cudnn.version()}") print(f"GPU count: {torch.cuda.device_count()}") if torch.cuda.is_available(): print(f"GPU name: {torch.cuda.get_device_name(0)}") print(f"GPU memory: {torch.cuda.get_device_properties(0).total_mem / 1e9:.1f} GB") # Test GPU computation x = torch.randn(1000, 1000, device='cuda') y = torch.matmul(x, x) print(f"GPU computation successful: {y.shape}") ``` `torch.cuda.is_available()` is the definitive check for PyTorch GPU support. If it returns `False`, PyTorch was installed without CUDA support or the CUDA/driver versions are incompatible. ## Step 4: Check with TensorFlow ```python import tensorflow as tf print(f"TensorFlow version: {tf.__version__}") print(f"Built with CUDA: {tf.test.is_built_with_cuda()}") # List GPU devices gpus = tf.config.list_physical_devices('GPU') print(f"GPUs available: {len(gpus)}") for gpu in gpus: print(f" {gpu}") # Test GPU computation if gpus: with tf.device('/GPU:0'): a = tf.random.normal([1000, 1000]) b = tf.matmul(a, a) print(f"GPU computation successful: {b.shape}") ``` ## Installing CUDA in Conda ```bash # Create a new environment with PyTorch + CUDA conda create -n gpu-env python=3.10 conda activate gpu-env # PyTorch with CUDA 12.1 (recommended) conda install pytorch torchvision torchaudio pytorch-cuda=12.1 -c pytorch -c nvidia # Or TensorFlow with GPU support pip install tensorflow[and-cuda] # Or install CUDA toolkit separately conda install -c nvidia cuda-toolkit=12.2 ``` Conda can install CUDA libraries locally within the environment, independent of the system CUDA installation. This avoids version conflicts with the system-wide CUDA. ## Checking cuDNN ```bash # conda-installed cuDNN conda list | grep cudnn # cudnn 8.9.7.29 # System cuDNN cat /usr/local/cuda/include/cudnn_version.h | grep CUDNN_MAJOR -A 2 ``` ```python # Python check import torch print(f"cuDNN enabled: {torch.backends.cudnn.enabled}") print(f"cuDNN version: {torch.backends.cudnn.version()}") # TensorFlow import tensorflow as tf print(f"cuDNN: {tf.test.is_built_with_cuda()}") ``` cuDNN is required for deep learning frameworks. Without it, some operations fall back to slower CUDA implementations or CPU. ## Comprehensive Diagnostic Script ```python import sys import subprocess def check_cuda(): print("=" * 50) print("CUDA Environment Check") print("=" * 50) # Python print(f"\nPython: {sys.version}") # nvidia-smi try: result = subprocess.run(['nvidia-smi'], capture_output=True, text=True) driver_line = [l for l in result.stdout.split('\n') if 'Driver Version' in l] if driver_line: print(f"Driver: {driver_line[0].strip()}") except FileNotFoundError: print("nvidia-smi: NOT FOUND") # PyTorch try: import torch print(f"\nPyTorch: {torch.__version__}") print(f"CUDA available: {torch.cuda.is_available()}") if torch.cuda.is_available(): print(f"CUDA version: {torch.version.cuda}") print(f"GPU: {torch.cuda.get_device_name(0)}") except ImportError: print("PyTorch: NOT INSTALLED") # TensorFlow try: import tensorflow as tf print(f"\nTensorFlow: {tf.__version__}") gpus = tf.config.list_physical_devices('GPU') print(f"GPUs found: {len(gpus)}") except ImportError: print("TensorFlow: NOT INSTALLED") check_cuda() ``` ## Common Pitfalls * **nvidia-smi CUDA version does not mean CUDA toolkit is installed**: `nvidia-smi` shows the maximum CUDA version the driver supports, not the installed toolkit version. You still need to install the CUDA toolkit (via conda or system package). * **PyTorch CPU-only build installed**: `pip install torch` without specifying the CUDA index URL installs the CPU-only version. Use the PyTorch website's install command generator to get the correct CUDA-enabled package. * **Conda environment not activated**: Checking `nvcc --version` or `python -c "import torch"` outside the conda environment uses system binaries, not the conda-installed ones. Always `conda activate myenv` first. * **Driver too old for CUDA version**: CUDA 12.x requires NVIDIA driver 525+. If your driver is older, either update the driver or install an older CUDA version. Check the CUDA-driver compatibility matrix on NVIDIA's website. * **Multiple CUDA installations conflicting**: If system CUDA and conda CUDA are both installed, `LD_LIBRARY_PATH` may point to the wrong version. Conda environments should isolate this, but system-wide environment variables can override conda settings. ## Summary * Run `nvidia-smi` to verify the NVIDIA driver and GPU are detected * Run `nvcc --version` to check the CUDA toolkit version in your conda environment * Use `torch.cuda.is_available()` or `tf.config.list_physical_devices('GPU')` for framework-level verification * Install CUDA via conda (`pytorch-cuda=12.1`) to avoid system-wide version conflicts * Ensure driver version is compatible with your CUDA toolkit version * Use the diagnostic script to check all components at once
