Why torch.cuda.is_available() returns False, and how to fix it
torch.cuda.is_available() returns False
Also appears as
- AssertionError: Torch not compiled with CUDA enabled
- UserWarning: CUDA initialization: No NVIDIA GPU detected
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.
Affects: Any fresh PyTorch install, particularly on machines where pip installed a CPU-only wheel by default, or containers launched without GPU passthrough flags.
Fix it in a few minutes
- 1Check whether you have a CPU-only wheel: python -c "import torch; print(torch.version.cuda)". None means CPU-only torch was installed.
- 2If 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.
- 3Confirm nvidia-smi works outside Python first; if it fails, fix the driver before touching torch.
- 4Check CUDA_VISIBLE_DEVICES is not set to an empty string or an invalid index.
- 5If running in a container, confirm it was launched with --gpus all and the NVIDIA Container Toolkit is installed on the host.
How to confirm this is your problem
- 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
Root causes and fixes
Installed the CPU-only torch wheel instead of a CUDA-enabled build
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.
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
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
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.
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
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.
echo $CUDA_VISIBLE_DEVICES unset CUDA_VISIBLE_DEVICES
GPU in a degraded hardware state or not properly seated
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.
Diagnostic commands
Check if torch itself is CUDA-enabled at all
python -c "import torch; print(torch.version.cuda)"
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
nvidia-smi
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
echo $CUDA_VISIBLE_DEVICES
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
docker run --gpus all nvidia/cuda:12.1-base nvidia-smi
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.
Stopping it from happening again
- 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 this becomes an architecture problem
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.
Frequently asked questions
How do I quickly tell if I have a CPU-only or GPU-enabled torch build?
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?
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?
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?
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.
Size it properly next time
Free calculators that prevent this class of failure before you provision hardware.
Self-Hosted LLM Hardware Estimator
Estimate the VRAM footprint, GPU count, and hardware budget required to self-host an open-weight LLM with your concurrency and context needs.
Free ToolOn-Prem AI Deployment Checklist
A 30-point pre-deployment checklist covering use cases, hardware, security, model operations, and rollout for self-hosted enterprise LLMs.
Free ToolNVIDIA GPU Selector for LLM Workloads
Score your workload across model size, concurrency, latency, budget, and facility power to get a recommended GPU tier from RTX-class to multi-node B200 clusters.
Related problems
nvidia-smi command not found or fails to communicate with the driver
nvidia-smi not found or unable to communicate almost always means the NVIDIA kernel module never loaded, and the two most common reasons are that the driver was never installed, or the driver is installed but Secure Boot is blocking the unsigned kernel module from loading. WSL2 users hit a different variant: the driver must be installed on the Windows host, never inside the Linux guest.
CUDA version mismatch between PyTorch and the system driver
PyTorch ships its own bundled CUDA runtime inside the wheel, so it never uses your system's CUDA toolkit (the one nvcc reports). The only number that matters is the driver's maximum supported CUDA version, shown top right in nvidia-smi output. Fix the mismatch by installing a torch wheel built for a CUDA version at or below that number, not by touching nvcc or the toolkit.
GPU not visible inside a Docker container
Docker containers cannot see a host GPU unless the NVIDIA Container Toolkit is installed and the nvidia runtime is registered with the daemon, since containers are isolated from host devices by default. The fix is almost always to install nvidia-container-toolkit, run nvidia-ctk runtime configure, restart Docker, and launch with --gpus all. If nvidia-smi already fails on the host itself, the problem is the driver, not Docker.
NVIDIA driver installation fails on Ubuntu
The single most common reason NVIDIA driver installation fails on Ubuntu is Secure Boot rejecting the unsigned or self-signed kernel module at load time, since most machines now ship with Secure Boot enabled out of the box. The fix is enrolling the MOK key the installer generates, or disabling Secure Boot in the BIOS, then clearing any lingering nouveau or mixed-install conflicts before rebooting.
GuideOn-Prem LLM Inference Hardware in 2026: A Roundup
On-prem LLM inference hardware for 2026: H100 vs H200 vs B200 pricing, when A100 fleets still work, and how to size GPUs against real serving needs.
GuideOn-Prem LLM Deployment Architecture: Reference Guide
Reference architecture for on-prem LLM deployment: inference servers, GPU sizing, RAG pipelines, and security zones for regulated manufacturers.
Still stuck, or tired of fighting your own infrastructure?
Netray deploys and operates on-prem AI for regulated manufacturers and defense suppliers. We have debugged this stack in production, on air-gapped networks, at scale.