Cuda and PyTorch Setup Guide

2025-03-22
2 min read

This guide walks you through checking, switching, and verifying your CUDA version, and setting up the correct PyTorch installation for it.


Check Current CUDA Version

To check which CUDA version is currently active:

nvcc -V

🖥️ Example Output:

nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2021 NVIDIA Corporation
Built on Thu_Nov_18_09:45:30_PST_2021
Cuda compilation tools, release 11.5, V11.5.119
Build cuda_11.5.r11.5/compiler.30672275_0

✅ This means the currently active CUDA version is 11.5.


Check Installed CUDA Versions

Run the following command to list all installed CUDA directories:

cat /usr/local/cuda-*

🖥️ Example Output:

cat: /usr/local/cuda-11.7: Is a directory
cat: /usr/local/cuda-12: Is a directory
cat: /usr/local/cuda-12.4: Is a directory
cat: /usr/local/cuda-12.8: Is a directory

✅ This indicates that the system has the following CUDA versions installed:

  • CUDA 11.7
  • CUDA 12.0
  • CUDA 12.4
  • CUDA 12.8

Change CUDA Version (in Conda Environment)

If you want to switch to CUDA 11.7 in a specific Conda environment, run:

conda env config vars set CUDA_HOME=/usr/local/cuda-11.7
conda env config vars set PATH=/usr/local/cuda-11.7/bin:$PATH
conda env config vars set LD_LIBRARY_PATH=/usr/local/cuda-11.7/lib64:$LD_LIBRARY_PATH

Then, reactivate your Conda environment:

conda deactivate
conda activate <env_name>

Verify the Change

Run the following command again:

nvcc -V

🖥️ You should see something like:

nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2022 NVIDIA Corporation
Built on Tue_May__3_18:49:52_PDT_2022
Cuda compilation tools, release 11.7, V11.7.64
Build cuda_11.7.r11.7/compiler.31294372_0

Check CUDA Version in PyTorch

To check which CUDA version your PyTorch is using:

python -c "import torch; print(torch.version.cuda)"

🖥️ Example Output:

11.7

Install PyTorch for Specific CUDA Version

To install the correct version of PyTorch that matches CUDA 11.7, run:

conda install pytorch==2.0.1 torchvision==0.15.2 torchaudio==2.0.2 pytorch-cuda=11.7 -c pytorch -c nvidia

📚 For other versions and more details, visit the official PyTorch installation guide.


Thanks for my colleague, Cheng-Tse Lee, provide many supports to setup the environment. This content is also revise by ChatGPT.