Fix torch.cuda.is_available() Returns False

Quick answer

torch.cuda.is_available() returns False most often because you installed a CPU-only PyTorch wheel — check torch.version.cuda, and if it prints None, reinstall PyTorch from the correct CUDA index. Otherwise the NVIDIA driver is missing, too old, or hidden: run nvidia-smi, check CUDA_VISIBLE_DEVICES, and for Docker or WSL2 confirm GPU passthrough. PyTorch bundles its own CUDA runtime, so you do not need a system CUDA toolkit, only a recent enough driver.

You have an NVIDIA GPU, you installed PyTorch, and yet:

>>> import torch
>>> torch.cuda.is_available()
False

There are half a dozen reasons this happens, but they are not equally likely. The overwhelming majority of the time, the GPU is fine and the driver is fine — you just installed the wrong PyTorch. One command tells you whether that is your problem, and it takes two seconds.

This guide starts with that command, then works through the rest in the order you should actually check them. It also clears up the single most common piece of bad advice online: that you need to install the CUDA Toolkit. You almost certainly do not.

Quick Answer

Run print(torch.version.cuda). If it prints None, you installed a CPU-only wheel — reinstall PyTorch from the correct CUDA index. If it prints a version (e.g. 12.4), the wheel is fine and the problem is the driver (check nvidia-smi), an environment variable (CUDA_VISIBLE_DEVICES), or container GPU passthrough. PyTorch bundles its own CUDA runtime, so you never need the system CUDA toolkit — only a recent enough driver.

TL;DR

  • First test: torch.version.cuda. None → CPU-only wheel (the #1 cause).
  • Fix that: uninstall, then reinstall from the CUDA index (e.g. cu124).
  • Myth: you do not need the CUDA Toolkit. PyTorch bundles the runtime.
  • If the wheel is fine: nvidia-smi (driver), then CUDA_VISIBLE_DEVICES.
  • Docker: needs --gpus all + NVIDIA Container Toolkit.
  • WSL2: needs the Windows driver, not a Linux one.
  • Still False with everything set: the driver is too old for the wheel's CUDA.

The Two-Second Diagnosis

Before anything else, ask PyTorch what it was built with:

import torch
print(torch.__version__)      # e.g. 2.4.0+cpu  or  2.4.0+cu124
print(torch.version.cuda)     # None  → CPU build ;  '12.4' → CUDA build
  • +cpu in the version, or torch.version.cuda == NoneCPU-only wheel. No amount of driver fixing will help; you must reinstall PyTorch.
  • +cu124 and a CUDA version string → the build is fine; skip to the driver and environment checks.

This one distinction routes you to the right fix and saves hours of chasing drivers that were never the problem.

The Diagnostic Ladder

Diagnostic ladder for torch.cuda.is_available() == False: check the wheel, driver, env, then containerWork top to bottom. If torch.version.cuda is None you have a CPU-only wheel. Else check nvidia-smi for the driver, then CUDA_VISIBLE_DEVICES, then Docker or WSL2 GPU access. If all pass, the driver is too old for the wheel's CUDA runtime.torch.cuda.is_available()returns Falsetorch.version.cudais None?yesCPU-only wheel →reinstall the CUDA buildnonvidia-smiworks?yesDriver missing / broken →install driver, then rebootnoCUDA_VISIBLE_DEVICEShiding the GPU?yesUnset it, or set to avalid device index (0)noinside Dockeror WSL2?yesdocker --gpus all /install the Windows drivernoDriver too old for the wheel's CUDA →upgrade the NVIDIA driver
Diagnostic ladder for torch.cuda.is_available() == False: check the wheel, driver, env, then container

Work top to bottom — each rung rules out a whole category:

  1. CPU-only wheel? torch.version.cuda is None → reinstall the CUDA build.
  2. Driver working? nvidia-smi fails → install/repair the driver, reboot.
  3. GPU hidden? CUDA_VISIBLE_DEVICES empty or -1 → unset it.
  4. Container/WSL? Missing --gpus all or a Linux driver in WSL → fix passthrough.
  5. Everything passes but still False? The driver is too old for the wheel's CUDA runtime → upgrade the driver.

The Mental Model That Prevents Most Mistakes

What PyTorch actually needs: a bundled CUDA runtime plus a recent NVIDIA driver — not the system CUDA toolkitYour code sits on the PyTorch wheel, which already bundles the CUDA runtime and cuDNN. Below that is the NVIDIA driver, which is required and must be recent enough, then the GPU. The system CUDA toolkit and nvcc are not required for PyTorch — only for compiling custom CUDA extensions.Your Python / PyTorch codePyTorch wheelbundles the CUDA runtime + cuDNNNVIDIA driverREQUIRED — must be recent enoughGPU hardwareSystem CUDA Toolkit (nvcc)NOT required for PyTorch —only to compile customCUDA extensionsnot on the runtime path
What PyTorch actually needs: a bundled CUDA runtime plus a recent NVIDIA driver — not the system CUDA toolkit

Here is the idea that most articles get wrong. When you install a GPU PyTorch wheel, it already contains the CUDA runtime and cuDNN it needs. Those libraries ship inside the wheel. The only thing PyTorch needs from your system is a compatible NVIDIA driver.

The system CUDA Toolkit (the thing that installs nvcc) is a separate product for compiling CUDA code. PyTorch does not use it at runtime. Installing it will not fix is_available(), and having the "wrong" toolkit version installed does not break PyTorch either — the wheel ignores it.

⚠️

Stop installing the CUDA Toolkit to fix this. It is the most common wasted step. The toolkit matters only when you compile custom CUDA extensions from source. For normal training and inference, a driver + the right PyTorch wheel is the entire requirement.

Step-by-step Solution

Fix 1: Reinstall the correct CUDA wheel (the #1 fix)

If torch.version.cuda is None, replace the CPU wheel. Uninstall first so pip doesn't keep the cached CPU build:

pip uninstall -y torch torchvision torchaudio
 
# Install the CUDA build — pick the index that matches a CUDA your driver supports
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu124
  • --index-url .../cu124 → pulls the CUDA 12.4 wheels (with the runtime bundled).
  • Use the official PyTorch selector to get the exact command for your OS, package manager, and CUDA version.
  • The bundled runtime means you do not separately install CUDA 12.4 on the system.

Verify immediately:

import torch
print(torch.version.cuda)          # '12.4'
print(torch.cuda.is_available())   # True
print(torch.cuda.get_device_name(0))
🧭

conda users: the CPU vs GPU split bites here too. Use the pytorch channel and name the CUDA package explicitly, e.g. conda install pytorch pytorch-cuda=12.4 -c pytorch -c nvidia. A plain conda install pytorch can resolve to the CPU build.

Fix 2: Repair the NVIDIA driver

If the wheel is a CUDA build but is_available() is still False, check the driver:

nvidia-smi
nvidia-smi outputMeaningAction
Shows GPU + driver versionDriver is fineMove to Fix 3
command not foundNo driver installedInstall the NVIDIA driver
couldn't communicate with the NVIDIA driverDriver/kernel mismatchReboot; reinstall if it persists
Works, but PyTorch wants newer CUDADriver too oldUpgrade the driver

After a fresh driver install, reboot — the kernel module must load cleanly.

Fix 3: Unhide the GPU

An environment variable can hide every GPU from PyTorch even when the driver and wheel are perfect:

echo $CUDA_VISIBLE_DEVICES        # empty is fine; "" or "-1" hides all GPUs
import os
# If it was set to "" or "-1" somewhere (a script, a shell profile, a scheduler):
os.environ.pop("CUDA_VISIBLE_DEVICES", None)   # or set it before importing torch

Set it before import torch, or unset it entirely. A value of "" or -1 means "no visible devices," which is a legitimate but easily-forgotten way to get False.

Fix 4: Confirm you're in the right environment

The classic trap: you installed the CUDA wheel in one environment and are running Python in another. Make them match:

which python                       # is this the venv/conda env you installed into?
python -c "import torch, sys; print(sys.executable, torch.__file__, torch.version.cuda)"

If sys.executable points at a different interpreter than the one you pip installed into, activate the correct environment and reinstall there.

Platform-Specific Notes

Docker

A CUDA wheel and a healthy host driver are not enough — the container needs GPU access:

# On the host: install the NVIDIA Container Toolkit, then:
docker run --gpus all --rm nvidia/cuda:12.4.0-base-ubuntu22.04 nvidia-smi
  • --gpus all → exposes the host GPUs to the container. Without it, the container sees none.
  • Confirm nvidia-smi works inside the container before blaming PyTorch.
  • In Kubernetes, this is the nvidia.com/gpu resource request plus the device plugin.

WSL2 (Windows)

The counter-intuitive rule: install the driver on Windows, not in Linux.

  • Install the current NVIDIA Windows driver (it includes WSL CUDA support).
  • Do not install a Linux display driver inside the WSL distribution — it breaks the passthrough.
  • Then nvidia-smi works inside WSL2 and PyTorch sees the GPU.

Cloud instances

If nvidia-smi reports no device at all, verify you actually launched a GPU instance type (e.g. an A10/A100/T4 shape). A CPU instance will never expose a GPU no matter how PyTorch is installed. On some images the driver is present but needs a reboot after first boot.

Verification Steps

  1. torch.version.cuda prints a version, not None.
  2. torch.cuda.is_available() returns True.
  3. torch.cuda.get_device_name(0) prints your GPU.
  4. A tiny tensor actually lands on the GPU:
import torch
x = torch.rand(3).cuda()
print(x.device)          # cuda:0
  1. nvidia-smi shows your Python process using GPU memory while that tensor lives.

Green state: torch.version.cuda is set, is_available() is True, a .cuda() tensor reports cuda:0, and nvidia-smi lists your process. All four together mean the whole stack is wired up.

Prevention

  • Install PyTorch from the CUDA index URL, never a bare pip install torch.
  • Pin the wheel with its build tag (+cu124) in requirements so CI can't drift to CPU.
  • Keep the NVIDIA driver current; don't install the CUDA Toolkit to "fix" GPU issues.
  • Use one virtual environment per project and verify sys.executable.
  • In Docker, standardise on --gpus all and test nvidia-smi in the image build.
  • Add a startup assertion: assert torch.cuda.is_available() in GPU jobs so failures are loud.

Troubleshooting Matrix

SymptomLikely causeFix
torch.version.cuda is NoneCPU-only wheelReinstall from CUDA index
+cpu in torch.__version__CPU-only wheelReinstall from CUDA index
nvidia-smi: command not foundNo driverInstall NVIDIA driver, reboot
couldn't communicate with driverDriver/kernel mismatchReboot; reinstall driver
Wheel + driver fine, still FalseCUDA_VISIBLE_DEVICES hides GPUUnset the variable
Works in terminal, not in appWrong virtual environmentActivate correct env, reinstall
False only in DockerNo GPU passthrough--gpus all + Container Toolkit
False only in WSL2Linux driver installedUse the Windows driver instead
False after driver updateReboot pendingReboot the machine

External References

FAQs

Is torch.cuda.is_available() slow the first time? The first call initialises the CUDA context and can take a moment. That is normal and unrelated to it returning False — a False result is immediate.

Does the CUDA version in the wheel need to match my driver exactly? No. It needs a driver that is new enough for that CUDA runtime, thanks to NVIDIA's forward-compatibility. A newer driver runs older CUDA wheels fine; a driver that is too old for the wheel's CUDA is what fails.

Can I have both CPU and GPU PyTorch installed? Not usefully in the same environment — the last install wins. Use separate environments if you need both, and check torch.version.cuda in each.

Why does pip install torch give me the CPU build? On some platforms the default PyPI wheel is CPU-only. You have to pass the CUDA --index-url to get the GPU build. This is the single most common way people end up here.

is_available() was True yesterday and False today — what changed? Usually a driver update pending a reboot, a changed CUDA_VISIBLE_DEVICES in a new shell, or an environment/requirements.txt change that pulled the CPU wheel. Check torch.version.cuda and nvidia-smi first.

Key takeaways

  • The fastest diagnosis is torch.version.cuda — if it prints None, you have a CPU-only wheel; that is the number one cause.
  • PyTorch bundles its own CUDA runtime and cuDNN, so you do not need a system CUDA toolkit — only a compatible NVIDIA driver.
  • Reinstall from the correct CUDA index (for example the cu124 wheel index), after uninstalling the CPU build.
  • If torch.version.cuda is set but the GPU still isn't found, run nvidia-smi to check the driver, then CUDA_VISIBLE_DEVICES.
  • In Docker you need --gpus all and the NVIDIA Container Toolkit; in WSL2 you need the Windows driver, not a Linux one.
  • A driver that is too old for the wheel's CUDA runtime also returns False — upgrade the driver, not the toolkit.

Frequently asked questions

Why does torch.cuda.is_available() return False even though I have a GPU?

Usually because PyTorch itself was installed as a CPU-only wheel. Run print(torch.version.cuda); if it prints None, your PyTorch has no CUDA support regardless of your GPU, and you must reinstall the CUDA build. If it prints a version like 12.4, the problem is the driver, an environment variable, or container GPU passthrough instead.

How do I know if I installed the CPU-only version of PyTorch?

Check torch.version.cuda. None means a CPU-only build; a version string like '12.4' means a CUDA build. You can also look at the wheel name — CPU builds have +cpu, CUDA builds have +cu124 or similar. pip install torch without the CUDA index URL often gives the CPU wheel.

Do I need to install the CUDA Toolkit for PyTorch to use my GPU?

No. The PyTorch GPU wheel already bundles the CUDA runtime and cuDNN it needs. You only need a recent enough NVIDIA driver. The system CUDA toolkit (nvcc) is required only if you compile custom CUDA extensions from source, not for normal training or inference.

Why does nvidia-smi work but torch.cuda.is_available() is still False?

nvidia-smi only proves the driver and GPU are fine. If PyTorch still says False, you almost certainly have a CPU-only wheel (torch.version.cuda is None), CUDA_VISIBLE_DEVICES is hiding the GPU, or you are running in a different virtual environment from the one where you installed the CUDA build.

How do I fix torch.cuda.is_available() False in Docker?

Install the NVIDIA Container Toolkit on the host and run the container with GPU access: docker run --gpus all .... Without --gpus all the container sees no GPU even with a CUDA wheel and a working host driver. Confirm inside the container with nvidia-smi before testing PyTorch.

torch.cuda.is_available() is False in WSL2 — what's wrong?

In WSL2 you must install the NVIDIA driver on Windows, not inside the Linux distribution. Do not install a Linux display driver in WSL. With the correct Windows driver and a CUDA PyTorch wheel, nvidia-smi works inside WSL2 and is_available() returns True.


Related Posts