Fix "Torch not compiled with CUDA enabled" and torch.cuda.is_available() returning False
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.
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.
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
- CPU wheel installed: on Windows,
pip install torchfrom PyPI installs the CPU build. Another dependency may also have replaced your CUDA PyTorch with the CPU build. - 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. - Mac (Apple Silicon or Intel): no PyTorch build for macOS supports CUDA. On Apple Silicon, the GPU is used through
mps. - GPU not visible in WSL2 or Docker: Windows driver too old for WSL, Linux driver wrongly installed inside WSL, container started without
--gpus all. - 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
-
Diagnose the installed build
Run this code with your project’s exact interpreter.
+cpuin the version, ortorch.version.cudabeingNone, means the wheel is CPU-only. Also checksys.executableto be sure which environment runs.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 -
Check the NVIDIA driver
On Windows and Linux,
nvidia-smimust 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-driverson Ubuntu), then reboot. You do not need the CUDA Toolkit to use PyTorch.nvidia-smi # Ubuntu: install the recommended driver, then reboot sudo ubuntu-drivers install sudo reboot -
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=autopicks the variant matching your driver.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 -
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-smiworks in the Linux terminal.wsl --update wsl --shutdown # then open your WSL distribution and run: nvidia-smi -
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.docker run --rm --gpus all pytorch/pytorch python -c "import torch; print(torch.cuda.is_available())" -
Write hardware-independent code
Do not hardcode
"cuda": pick the available device (CUDA, thenmpson Apple Silicon Macs, then CPU) and usemap_locationto load a model saved on a GPU.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/cu128line at the top ofrequirements.txt, or a dedicated index inpyproject.tomlwith uv) so a reinstall does not bring back the CPU build. - Add a startup check that prints
torch.__version__andtorch.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
- Fix error 429 "Rate limit reached" on OpenAI, Claude and Gemini APIs
- Fix "maximum context length" and "prompt is too long" (context_length_exceeded)
- Fix "CUDA out of memory" in PyTorch
- Fix error 401 "Incorrect API key provided" / "invalid x-api-key"
- Fix "could not connect to ollama" and connection refused on port 11434
- Fix "JSONDecodeError: Expecting value" when parsing an LLM response
- Fix "You are trying to access a gated repo" (401) on Hugging Face