Skip to content
FR
  1. LuzineDebug
  2. AI & LLMs
  3. AssertionError: Torch not compiled with CUDA ena…
AI & LLMs Intermediate ≈ 20 min to fix

Fix "Torch not compiled with CUDA enabled" and torch.cuda.is_available() returning False

Error message
AssertionError: Torch not compiled with CUDA enabled
  • RuntimeError: Found no NVIDIA driver on your system. Please check that you have an NVIDIA GPU and installed a driver from http://www.nvidia.com/Download/index.aspx
  • UserWarning: CUDA initialization: The NVIDIA driver on your system is too old (found version 11040).
  • RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False. If you are running on a CPU-only machine, please use torch.load with map_location=torch.device('cpu') to map your storages to the CPU.
In short

The installed PyTorch is a CPU-only build, or no usable NVIDIA driver is detected. Check torch.__version__ (a +cpu suffix confirms it) and nvidia-smi, then reinstall the CUDA wheel from the pytorch.org index, for example --index-url https://download.pytorch.org/whl/cu128. On a Mac, CUDA does not exist: use the mps device.

Be the first to confirm this fix

What this error means

PyTorch ships in several variants: CPU-only, CUDA (cu126, cu128, cu130…), ROCm for AMD. When code calls .cuda(), .to("cuda") or creates a tensor on device="cuda", PyTorch initializes CUDA. If the installed variant was built without CUDA, that initialization fails with AssertionError: Torch not compiled with CUDA enabled. If the variant is CUDA but the NVIDIA driver is missing, too old or not visible (WSL2, Docker), torch.cuda.is_available() returns False and the error becomes “Found no NVIDIA driver”. The pip wheels already bundle the CUDA libraries: only the driver must be installed on the machine.

Common causes

  1. CPU wheel installed: on Windows, pip install torch from PyPI installs the CPU build. Another dependency may also have replaced your CUDA PyTorch with the CPU build.
  2. NVIDIA driver missing or too old: the wheel’s CUDA version (for example cu128) is newer than what the driver supports, as shown by nvidia-smi.
  3. Mac (Apple Silicon or Intel): no PyTorch build for macOS supports CUDA. On Apple Silicon, the GPU is used through mps.
  4. GPU not visible in WSL2 or Docker: Windows driver too old for WSL, Linux driver wrongly installed inside WSL, container started without --gpus all.
  5. Wrong Python environment: the Jupyter notebook or editor uses a different interpreter than the one where you installed the CUDA build.

Step-by-step fix

  1. Diagnose the installed build

    Run this code with your project’s exact interpreter. +cpu in the version, or torch.version.cuda being None, means the wheel is CPU-only. Also check sys.executable to be sure which environment runs.

    python
    import sys
    import torch
    
    print(sys.executable)
    print(torch.__version__)          # e.g. 2.x.x+cpu = CPU-only build
    print(torch.version.cuda)         # None on a CPU-only build
    print(torch.cuda.is_available())
    print(torch.backends.mps.is_available())  # Apple Silicon
  2. Check the NVIDIA driver

    On Windows and Linux, nvidia-smi must list your GPU. The “CUDA Version” value at the top right is the highest CUDA version the driver supports: the PyTorch wheel you pick must not exceed it. If the command fails, install or update the driver (NVIDIA app on Windows, ubuntu-drivers on Ubuntu), then reboot. You do not need the CUDA Toolkit to use PyTorch.

    bash
    nvidia-smi
    
    # Ubuntu: install the recommended driver, then reboot
    sudo ubuntu-drivers install
    sudo reboot
  3. Reinstall the CUDA PyTorch wheel

    First uninstall the existing packages, then install from the pytorch.org CUDA index. Take the exact command from the “Get Started” page on pytorch.org: it lists the variants available (cu126, cu128, cu130…) for the current release. With uv, --torch-backend=auto picks the variant matching your driver.

    bash
    python -m pip uninstall -y torch torchvision torchaudio
    python -m pip install torch torchvision --index-url https://download.pytorch.org/whl/cu128
    
    # or with uv
    uv pip install torch torchvision --torch-backend=auto
  4. On WSL2: use the Windows driver

    In WSL2, the GPU is exposed by the NVIDIA driver installed on the Windows side. Do not install a Linux NVIDIA driver inside the WSL distribution. Update WSL from PowerShell, restart it, then check that nvidia-smi works in the Linux terminal.

    powershell
    wsl --update
    wsl --shutdown
    # then open your WSL distribution and run: nvidia-smi
  5. In Docker: give access to the GPU

    Install the NVIDIA Container Toolkit on the Linux host (Docker Desktop on Windows goes through WSL2), then start the container with --gpus all. Without that option the container sees no GPU, even if the image ships CUDA PyTorch.

    bash
    docker run --rm --gpus all pytorch/pytorch python -c "import torch; print(torch.cuda.is_available())"
  6. Write hardware-independent code

    Do not hardcode "cuda": pick the available device (CUDA, then mps on Apple Silicon Macs, then CPU) and use map_location to load a model saved on a GPU.

    python
    import torch
    
    if torch.cuda.is_available():
        device = torch.device("cuda")
    elif torch.backends.mps.is_available():
        device = torch.device("mps")
    else:
        device = torch.device("cpu")
    
    model = MyModel().to(device)
    model.load_state_dict(torch.load("model.pt", map_location=device))
    inputs = inputs.to(device)

Did this fix work for you?

Confirm it in one click: it will rise for the next developers stuck on the same error.

Keep it from coming back

  • Pin the PyTorch index in your dependencies (an --index-url https://download.pytorch.org/whl/cu128 line at the top of requirements.txt, or a dedicated index in pyproject.toml with uv) so a reinstall does not bring back the CPU build.
  • Add a startup check that prints torch.__version__ and torch.cuda.is_available(), and fails clearly when a GPU is required.
  • Use one virtual environment per project and select the same interpreter in Jupyter and in the editor.

Frequently asked questions

Do I need to install the CUDA Toolkit for PyTorch?

No. PyTorch pip wheels contain the required CUDA libraries. A recent enough NVIDIA driver is all you need. The Toolkit is only useful to compile your own CUDA extensions.

Can I use CUDA on an M1, M2, M3 or M4 Mac?

No, CUDA is for NVIDIA GPUs only. On Apple Silicon, use torch.device("mps"), which uses the GPU through Metal.

What about an AMD graphics card?

On Linux, install the ROCm variant of PyTorch from its index on pytorch.org. The API stays torch.cuda, so torch.cuda.is_available() returns True with ROCm.

Useful tools for this error

More AI & LLMs errors

All AI & LLMs errors →