# Why torch.cuda.is_available() returns False, and how to fix it

> Source: https://netray.co/troubleshoot/gpu-not-detected-pytorch
> Publisher: Netray (netray.co), AI and integration engineering for ERP and manufacturing systems.
> Section: On-Prem AI Troubleshooting

## Short answer

torch.cuda.is_available() returning False almost always means either the installed torch wheel is a CPU-only build, or the process cannot see the GPU due to a driver, container, or environment variable problem. Checking torch.version.cuda for None immediately tells you whether you have a CPU-only wheel, which is the single most common cause and the fastest thing to rule out.

## Error Title

torch.cuda.is_available() returns False even though a GPU is present

## Error Signature

torch.cuda.is_available() returns False

## Alternate Signatures

- AssertionError: Torch not compiled with CUDA enabled
- UserWarning: CUDA initialization: No NVIDIA GPU detected
- torch.cuda.device_count() returns 0
- torch cuda not available
- PyTorch not detecting GPU

## Category

installation

## Stack

- pytorch
- cuda
- nvidia-driver

## Affects

Any fresh PyTorch install, particularly on machines where pip installed a CPU-only wheel by default, or containers launched without GPU passthrough flags.

## Quick Fix

### Fix it in a few minutes

**Steps:**

- Check whether you have a CPU-only wheel: python -c "import torch; print(torch.version.cuda)". None means CPU-only torch was installed.
- If it printed None, reinstall torch from the CUDA-specific index url matching your driver, e.g. pip install torch --index-url https://download.pytorch.org/whl/cu121.
- Confirm nvidia-smi works outside Python first; if it fails, fix the driver before touching torch.
- Check CUDA_VISIBLE_DEVICES is not set to an empty string or an invalid index.
- If running in a container, confirm it was launched with --gpus all and the NVIDIA Container Toolkit is installed on the host.

## Symptoms

- torch.cuda.is_available() prints False despite nvidia-smi showing a healthy GPU outside Python
- torch.version.cuda prints None, indicating a CPU-only build
- AssertionError explicitly stating torch was not compiled with CUDA enabled
- Code that ran fine on one machine reports no GPU on another with the same script

## Causes

### Installed the CPU-only torch wheel instead of a CUDA-enabled build

**Likelihood:** most-common

**Explanation:** On some platforms and package indexes, a bare pip install torch resolves to the CPU-only build rather than a CUDA-enabled one, particularly inside certain Docker base images, conda environments, or when a requirements.txt lacks an explicit CUDA index URL. The CPU wheel imports fine and runs fine on CPU, but torch.cuda.is_available() will always return False regardless of the hardware present.

**Fix:** Reinstall torch explicitly from the CUDA-specific PyTorch index for your driver's supported CUDA version, rather than a bare pip install torch with no index specified.

**Commands:**

- pip uninstall -y torch
- pip install torch --index-url https://download.pytorch.org/whl/cu121

### Driver not installed or too old for the installed torch CUDA build

**Likelihood:** common

**Explanation:** Even with a correctly installed CUDA-enabled torch wheel, if the underlying driver is missing or predates the CUDA version torch bundles, CUDA initialization fails silently in some contexts and torch.cuda.is_available() reports False rather than raising the driver-too-old error you would see with direct GPU calls.

**Fix:** Confirm nvidia-smi runs successfully outside Python first; if it fails, resolve the driver installation before revisiting torch, since torch cannot detect a GPU the driver itself cannot expose.

### GPU not passed through to a container running the code

**Likelihood:** common

**Explanation:** Containers do not have GPU access by default. Without launching with --gpus all (or the equivalent Kubernetes device plugin resource request) and having the NVIDIA Container Toolkit configured on the host, the container's PyTorch process runs in a GPU-invisible environment even though the host machine has a perfectly working GPU and driver.

**Fix:** Launch the container with --gpus all (Docker) or the appropriate GPU resource limit and runtimeClassName (Kubernetes), and confirm the NVIDIA Container Toolkit is installed and configured on the host.

**Commands:**

- docker run --gpus all -it your-image python -c "import torch; print(torch.cuda.is_available())"

### CUDA_VISIBLE_DEVICES is unset correctly, empty, or invalid

**Likelihood:** occasional

**Explanation:** Setting CUDA_VISIBLE_DEVICES to an empty string, a nonexistent GPU index, or leaving it inherited as empty from a parent process or job scheduler explicitly hides all GPUs from the process, which is indistinguishable from a real detection failure unless you check this environment variable directly.

**Fix:** Print and inspect the CUDA_VISIBLE_DEVICES environment variable in the exact process context that fails, and unset it or correct it to a valid GPU index list.

**Commands:**

- echo $CUDA_VISIBLE_DEVICES
- unset CUDA_VISIBLE_DEVICES

### GPU in a degraded hardware state or not properly seated

**Likelihood:** rare

**Explanation:** A GPU experiencing Xid errors, a failed PCIe link retrain, or one that lost power or physical connection after a maintenance window can disappear from the PCI bus entirely, which no software fix in torch or the driver stack can resolve short of a physical reseat or hardware reset.

**Fix:** Check dmesg and nvidia-smi -q for Xid errors or a missing GPU from lspci, and if the hardware itself is the problem, escalate to a physical reseat, reboot, or hardware replacement.

## Diagnostics

### Check if torch itself is CUDA-enabled at all

**Command:** python -c "import torch; print(torch.version.cuda)"

**Interpretation:** None means you have a CPU-only build and must reinstall from a CUDA-specific index; any other value confirms a CUDA-enabled build is present.

### Confirm the GPU is visible outside Python entirely

**Command:** nvidia-smi

**Interpretation:** If this fails, the problem is the driver or hardware, not torch; fix this layer first since torch cannot detect a GPU the driver cannot see.

### Check for an environment variable hiding the GPU

**Command:** echo $CUDA_VISIBLE_DEVICES

**Interpretation:** An empty string or an out-of-range index explicitly hides GPUs from the process; unset or correct it and retest.

### Confirm GPU passthrough inside a container context specifically

**Command:** docker run --gpus all nvidia/cuda:12.1-base nvidia-smi

**Interpretation:** If this test container also fails to see the GPU, the problem is Docker/NVIDIA Container Toolkit configuration on the host, not your application code.

## Prevention

- Always install torch from an explicit CUDA index URL in requirements.txt, never a bare pip install torch with no index specified.
- Add a startup health check to services that asserts torch.cuda.is_available() is True before accepting traffic, so failures surface immediately rather than in production.
- Standardize container base images with GPU passthrough and the NVIDIA Container Toolkit pre-validated as part of your CI pipeline.
- Monitor for Xid errors and GPU disappearance from the PCI bus as part of routine fleet health checks, not just application-level logging.

## When To Escalate

If GPUs intermittently disappear from multiple nodes under load, or Xid errors recur even after driver reinstalls, this moves from a software configuration issue to a hardware reliability or data center power and cooling investigation that needs infrastructure-level attention.

## Faq

### Why does torch.cuda.device_count() return 0?

**Answer:** It has the same causes as torch.cuda.is_available() returning False: a CPU-only torch wheel, a driver that nvidia-smi cannot reach, a container started without --gpus all, or CUDA_VISIBLE_DEVICES set to an empty or invalid value. Check torch.version.cuda and nvidia-smi first.

### Why does device = torch.cuda.is_available() not give me a device?

**Answer:** torch.cuda.is_available() returns a boolean, not a device. Use device = torch.device("cuda" if torch.cuda.is_available() else "cpu") and move the model with model.to(device). Also note there is no torch.is_cuda_available function; the correct call is torch.cuda.is_available().

### How do I quickly tell if I have a CPU-only or GPU-enabled torch build?

**Answer:** Run python -c "import torch; print(torch.version.cuda)". If it prints None, you have a CPU-only wheel and torch.cuda.is_available() will always return False no matter what GPU hardware or driver is present. Reinstalling from the correct CUDA-specific index URL is the fix.

### Why does nvidia-smi work but PyTorch still cannot see the GPU?

**Answer:** This split usually means the torch wheel itself is CPU-only, the process is running inside a container without GPU passthrough enabled, or CUDA_VISIBLE_DEVICES is set to hide all devices. nvidia-smi succeeding confirms the driver and hardware are fine, so the remaining causes are all in the application layer.

### Do I need to set CUDA_VISIBLE_DEVICES manually?

**Answer:** Only if you want to restrict which GPUs a process can use, such as pinning different jobs to different cards. Leaving it unset lets torch see all available GPUs by default. An accidentally inherited empty or invalid value from a job scheduler or parent shell is a common, easy-to-miss cause of this error.

### Is a degraded GPU a common cause of this error?

**Answer:** It is the least common cause but does happen, typically surfacing as a GPU that worked previously and then disappears after a driver update, kernel upgrade, or power event. Check dmesg for Xid error codes and confirm the GPU still appears in lspci before assuming it is purely a software configuration issue.

## Search terms

torch.cuda.is_available() returns false, torch not compiled with cuda enabled, pytorch gpu not detected, torch.version.cuda none, cuda_visible_devices pytorch, docker pytorch gpu not available

---
Talk to an engineer: https://netray.co/contact?intent=erp_ai | Machine catalog: https://netray.co/agent/catalog.json
