"No CUDA-capable device is detected" — how to fix it
My GPU is right there and CUDA says there is no device
错误信息
RuntimeError: No CUDA-capable device is detected
| 如果你看到 | 原因是 | 前往 |
|---|---|---|
nvidia-smi fails or lists no GPU | The driver is missing, outdated or not loaded | Fix 1: confirm the driver sees the GPU |
nvidia-smi lists the card, but torch.version.cuda prints None | A CPU-only build of PyTorch is installed | Fix 2: install a CUDA-enabled build |
torch.version.cuda prints a version, yet no device is found | CUDA_VISIBLE_DEVICES is empty or names a GPU that does not exist | Fix 3: check CUDA_VISIBLE_DEVICES |
| It works on the host and fails only inside a container | The container was started without GPU passthrough | Fix 4: pass the GPU through to Docker |
何时出现
Your code or runtime can't find the GPU at all — torch.cuda.is_available() returns False, and anything that needs CUDA bails immediately. The card is physically there, but the software stack can't see it.
- A fresh
pip install torchon Windows, where the default wheel from PyPI is CPU-only. - A Linux kernel update that left the NVIDIA module unbuilt, so the driver no longer loads.
- A script, notebook or job scheduler that sets
CUDA_VISIBLE_DEVICESfor you. - A Docker container started without
--gpus all. - A laptop where the NVIDIA GPU is powered down or switched off in favour of the integrated one.
实际原因
Unlike an out-of-memory error, this one is about visibility, not capacity. Something in the chain — NVIDIA driver, CUDA runtime, the framework's CUDA build, or an environment variable — is broken or mismatched, so the GPU never gets exposed to your process. Common causes: a missing/outdated driver, a CPU-only PyTorch install, a CUDA_VISIBLE_DEVICES set to empty, or running inside a container without GPU passthrough. The stack has four layers, and each depends on the one below it: the kernel driver, which nvidia-smi talks to directly; the CUDA runtime, which ships inside PyTorch, Ollama and llama.cpp builds rather than as a separate install; the framework build itself, which was either compiled with CUDA or was not; and the process environment, which can hide devices that every layer below it can see. Testing from the bottom up tells you which layer broke in about a minute, which is why the fixes start with nvidia-smi rather than with reinstalling anything.
如何修复
- 1
Confirm the driver sees the GPU
Start at the bottom of the stack.
nvidia-smitalks directly to the driver — if it lists your card, the driver is fine and the problem is higher up (framework/toolkit). Ifnvidia-smiitself fails or shows nothing, install or update the NVIDIA driver first; nothing above it can work until this does.nvidia-smi生效了吗?nvidia-smiprints a table naming your GPU, with a driver version and aCUDA Versionin its header. That CUDA figure is the newest runtime the driver supports, not a version that is installed. - 2
Install a CUDA-enabled build of your framework
A plain
pip install torchcan pull a CPU-only wheel that will never see the GPU. Reinstall the CUDA build that matches your driver. After installing, verify from Python that CUDA is now available.# example: CUDA 12.8 build of PyTorch — take the exact line from the selector on pytorch.org pip install torch --index-url https://download.pytorch.org/whl/cu128 python -c "import torch; print(torch.cuda.is_available())"生效了吗?torch.cuda.is_available()printsTrue, andtorch.version.cudaprints a version no newer than theCUDA Versionin thenvidia-smiheader. If it is newer, install an older wheel from pytorch.org or update the driver.# bash, zsh or PowerShell — the same command on all of them python -c "import torch; print(torch.cuda.is_available(), torch.version.cuda)" - 3
Check CUDA_VISIBLE_DEVICES
If this variable is set to an empty string or a non-existent index, CUDA hides every GPU. Unset it (or set it to a valid index) and retry.
echo $CUDA_VISIBLE_DEVICES # if empty-string or wrong, fix it: unset CUDA_VISIBLE_DEVICES生效了吗?After unsetting it,torch.cuda.device_count()matches the number of GPUsnvidia-smilists. On Windows, clearing it in PowerShell lasts for that session only; if it comes back, it is set in your user or system environment variables.# bash echo "$CUDA_VISIBLE_DEVICES" unset CUDA_VISIBLE_DEVICES # PowerShell $env:CUDA_VISIBLE_DEVICES Remove-Item Env:CUDA_VISIBLE_DEVICES - 4
In Docker, pass the GPU through
Containers don't get the GPU by default. You need the NVIDIA Container Toolkit installed on the host and the
--gpus allflag ondocker run, otherwise the container sees no CUDA device.docker run --gpus all <image>生效了吗?docker run --rm --gpus all ubuntu nvidia-smiprints the same GPU table as the host. If it errors instead, the NVIDIA Container Toolkit is missing or not registered with Docker. - 5
Once CUDA works, make sure your model actually fits最常见的修复方法
When the GPU is finally visible, the next wall people hit is memory — the model being too big for the card. That part is worth getting right up front: check which model and quant fit your VRAM so you don't trade a visibility error for an out-of-memory one.
生效了吗?With the GPU visible, the model loads fully onto it: in Ollama, the Processor column ofollama psreads100% GPU.检查哪些模型适配你的硬件once your GPU is detected, see which models fit its VRAM打开显存检查器 →
如果以上都没用
If nvidia-smi itself fails, the problem is the driver, and the pages below cover the usual reasons it stops loading on Linux, inside WSL2 and on hybrid laptops. If every layer checks out and one tool still sees no GPU, compare that tool's bundled CUDA version with the driver's supported version: a runtime newer than the driver is the remaining common mismatch.
- nvidia-smi stopped working after a kernel update
- Secure Boot is blocking your GPU driver module
- CUDA works on Windows but not inside WSL2
- Your laptop runs the model on the integrated GPU
- Full
nvidia-smioutput, or its exact error python -c "import torch; print(torch.__version__, torch.version.cuda)"- The value of
CUDA_VISIBLE_DEVICESin the failing environment - Operating system, GPU model, and whether you run inside Docker or WSL2
相关内容
常见问题
Is "No CUDA-capable device is detected" a hardware fault?
Rarely. It is almost always a software/visibility issue — missing or mismatched driver, a CPU-only framework build, or an environment/container setting. Start with nvidia-smi: if that sees the card, the hardware is fine.
nvidia-smi works but PyTorch still says no device — why?
That points to a CPU-only or wrong-CUDA-version PyTorch install. Reinstall the CUDA build matching your driver, then check torch.cuda.is_available() again.
Do I need the full CUDA Toolkit installed?
For most prebuilt frameworks (PyTorch, Ollama, llama.cpp releases) you only need a recent NVIDIA driver — the CUDA runtime ships with the package. You need the full toolkit mainly when compiling CUDA code yourself.
Why does PyTorch see the GPU on Linux but not on Windows with the same command?
Because pip installs different wheels. On Linux the default PyTorch wheel from PyPI includes CUDA; on Windows the default is CPU-only, so pip install torch gives you a build that cannot see any GPU. Install from the CUDA index URL that pytorch.org's selector shows for your platform.
What does the CUDA Version in nvidia-smi mean?
It is the newest CUDA runtime your installed driver can run, not a version that is installed. A framework built for CUDA 12.8 needs a driver whose header shows 12.8 or higher. If the framework's CUDA is newer than that figure, update the driver or install an older framework build.
Can a GPU be too old for current frameworks?
Yes. CUDA releases drop older architectures and frameworks follow them: CUDA 13 supports Turing and newer, so a Pascal or Maxwell card needs a build made against CUDA 12 or earlier. Check your card's compute capability against the framework's release notes before reinstalling the same build repeatedly.