无法启动,或识别不到 GPUWindowsLinux最后更新 2026年11月6日

"No CUDA-capable device is detected" — how to fix it

My GPU is right there and CUDA says there is no device

作者 Jakub Rusinowski

错误信息

RuntimeError: No CUDA-capable device is detected
你遇到的是哪一种?
如果你看到原因是前往
nvidia-smi fails or lists no GPUThe driver is missing, outdated or not loadedFix 1: confirm the driver sees the GPU
nvidia-smi lists the card, but torch.version.cuda prints NoneA CPU-only build of PyTorch is installedFix 2: install a CUDA-enabled build
torch.version.cuda prints a version, yet no device is foundCUDA_VISIBLE_DEVICES is empty or names a GPU that does not existFix 3: check CUDA_VISIBLE_DEVICES
It works on the host and fails only inside a containerThe container was started without GPU passthroughFix 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 torch on 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_DEVICES for 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. 1

    Confirm the driver sees the GPU

    Start at the bottom of the stack. nvidia-smi talks directly to the driver — if it lists your card, the driver is fine and the problem is higher up (framework/toolkit). If nvidia-smi itself fails or shows nothing, install or update the NVIDIA driver first; nothing above it can work until this does.

    bash
    nvidia-smi
    生效了吗?nvidia-smi prints a table naming your GPU, with a driver version and a CUDA Version in its header. That CUDA figure is the newest runtime the driver supports, not a version that is installed.
  2. 2

    Install a CUDA-enabled build of your framework

    A plain pip install torch can 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.

    bash
    # 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() prints True, and torch.version.cuda prints a version no newer than the CUDA Version in the nvidia-smi header. If it is newer, install an older wheel from pytorch.org or update the driver.
    multiple shells — see comments
    # bash, zsh or PowerShell — the same command on all of them
    python -c "import torch; print(torch.cuda.is_available(), torch.version.cuda)"
  3. 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.

    bash
    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 GPUs nvidia-smi lists. 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.
    multiple shells — see comments
    # bash
    echo "$CUDA_VISIBLE_DEVICES"
    unset CUDA_VISIBLE_DEVICES
    
    # PowerShell
    $env:CUDA_VISIBLE_DEVICES
    Remove-Item Env:CUDA_VISIBLE_DEVICES
  4. 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 all flag on docker run, otherwise the container sees no CUDA device.

    bash
    docker run --gpus all <image>
    生效了吗?docker run --rm --gpus all ubuntu nvidia-smi prints the same GPU table as the host. If it errors instead, the NVIDIA Container Toolkit is missing or not registered with Docker.
  5. 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 of ollama ps reads 100% 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.

上报问题时请附上这些信息
  • Full nvidia-smi output, or its exact error
  • python -c "import torch; print(torch.__version__, torch.version.cuda)"
  • The value of CUDA_VISIBLE_DEVICES in the failing environment
  • Operating system, GPU model, and whether you run inside Docker or WSL2
同样影响: Linux

相关内容

常见问题

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.