Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

How to Install PyTorch with GPU CUDA Support

· 4 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Installing PyTorch with GPU (CUDA) support lets you train and run deep learning models dramatically faster by using your NVIDIA graphics card instead of just your CPU. This guide walks you through checking your hardware, installing the right drivers, setting up PyTorch with CUDA, and verifying everything works with a simple GPU test.

What You'll Need

  • An NVIDIA GPU with CUDA compute capability 3.5 or higher (check NVIDIA's list if unsure).
  • Windows, Linux, or macOS (macOS only supports CUDA on certain older models; most Macs use MPS instead).
  • Python 3.8 or later installed (recommend using Anaconda or a virtual environment).
  • Administrator or sudo access to install drivers and packages.

Step-by-Step

  1. Check your GPU and driver compatibility. Open a terminal or command prompt and run nvidia-smi. If it shows your GPU model and a driver version, you're good. If it says 'command not found', you need to install NVIDIA drivers first. Note the CUDA version shown in the top-right corner (e.g., CUDA 12.1).
  2. Install or update NVIDIA drivers. Go to NVIDIA's driver download page, select your GPU model and operating system, and download the latest driver. Install it, then reboot. After reboot, run nvidia-smi again to confirm it works.
  3. Create a Python environment (optional but recommended). Using Anaconda or venv, create a fresh environment: conda create -n pytorch-gpu python=3.10 (or python -m venv pytorch-gpu). Activate it with conda activate pytorch-gpu or source pytorch-gpu/bin/activate on Linux/macOS.
  4. Install PyTorch with CUDA support. Visit pytorch.org and select your preferences (Linux, pip, CUDA 12.1, etc.). It will give you a command like: pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121. Run that command in your terminal. If you prefer conda, use the provided conda command instead.
  5. Verify the installation. In Python, run:
    import torch
    print(torch.__version__)
    print(torch.cuda.is_available())
    print(torch.cuda.get_device_name(0))
    If torch.cuda.is_available() prints True, CUDA is working. If it prints False, see Troubleshooting below.
  6. Run a quick GPU performance test. Execute a simple matrix multiplication on GPU:
    import torch
    x = torch.rand(1000, 1000, device='cuda')
    y = torch.rand(1000, 1000, device='cuda')
    z = x @ y
    print(z.sum().item())
    If it runs without errors, your GPU is processing tensors.

Common Mistakes

  • Installing CPU-only PyTorch by mistake. The default pip install often gets CPU-only. Always use the specific index URL from the PyTorch website.
  • Mismatched CUDA versions. Your driver's CUDA version must be >= the CUDA version PyTorch expects. For example, if PyTorch needs CUDA 12.1, your driver must support at least that (check with nvidia-smi).
  • Using a non-NVIDIA GPU. CUDA only works with NVIDIA. AMD and Intel GPUs require ROCm or other backends.
  • Not activating the correct environment. If you used a virtual environment, forgetting to activate it can lead to installing into the wrong Python.

Tips

  • Always create a fresh environment for PyTorch to avoid conflicts with other packages.
  • Check the official PyTorch website for the latest stable version and CUDA version—they update regularly.
  • Use torch.cuda.is_available() as a quick sanity check before running any heavy code.
  • If you have multiple GPUs, you can specify which one to use with torch.cuda.set_device(0) or the CUDA_VISIBLE_DEVICES environment variable.
  • For deep learning, also install torchvision and torchaudio as they are often needed.

Troubleshooting

  • torch.cuda.is_available() returns False. First, run nvidia-smi to confirm the driver is installed. Then check if PyTorch was built with CUDA: torch.version.cuda should show a version. If it shows None, you installed the CPU version—reinstall with the correct index URL.
  • Error: 'CUDA driver version is insufficient'. Update your NVIDIA driver to a newer version that supports the CUDA version PyTorch requires.
  • ImportError: libcudart.so not found. This usually means the CUDA toolkit is missing or not in the library path. On Linux, add the CUDA lib directory to LD_LIBRARY_PATH or reinstall PyTorch with the bundled CUDA libraries (the default pip wheels include them).
  • Out of memory errors. Reduce batch size or use torch.cuda.empty_cache() to free unused memory.
  • PyTorch doesn't detect GPU on Windows. Ensure you have the latest drivers and that your GPU is not disabled in Device Manager. Also, avoid using WSL unless you have WSL2 with GPU support.

Frequently Asked Questions

Do I need to install CUDA toolkit separately?

No. The pip wheels from PyTorch include the necessary CUDA runtime libraries. You only need the NVIDIA driver installed.

Can I use PyTorch with CUDA on a laptop?

Yes, as long as the laptop has an NVIDIA GPU with a compatible driver. Many gaming laptops work fine. Just ensure the GPU is not being blocked by Optimus or similar power-saving features.

What is the difference between CUDA and cuDNN?

CUDA is the general parallel computing platform, while cuDNN is a library specifically for deep learning operations. PyTorch bundles cuDNN, so you don't need to install it separately.

How do I check which CUDA version PyTorch is using?

Run python -c "import torch; print(torch.version.cuda)". It will print the CUDA version PyTorch was built with.

Why is my GPU not being used even though torch.cuda.is_available() is True?

Make sure you explicitly move tensors and models to the GPU with .to('cuda') or .cuda(). By default, tensors are on CPU.

Can I install PyTorch with CUDA on a machine without an NVIDIA GPU?

No. CUDA requires NVIDIA hardware. For AMD GPUs, look into ROCm builds; for Apple Silicon, use the MPS backend.