AssertionError: Torch Not Compiled With CUDA Enabled – Your Complete Fix Guide

AssertionError: Torch Not Compiled With CUDA Enabled – Your Complete Fix Guide

Stuck with the frustrating "AssertionError: Torch not compiled with CUDA enabled" message? You're not alone. This common PyTorch error halts deep learning projects in their tracks, preventing your code from tapping into the massive parallel processing power of your NVIDIA GPU. Whether you're a researcher training complex neural networks or a developer building AI-powered applications, encountering this error means your carefully crafted model is stuck running on the CPU, wasting precious time and resources. But here's the good news: this error is almost always fixable with a systematic approach. This comprehensive guide will walk you through every possible cause and solution, transforming that roadblock into a stepping stone toward seamless GPU-accelerated computing.

Understanding the Core Problem: What This Error Really Means

Before diving into fixes, it's crucial to understand what "AssertionError: Torch not compiled with CUDA enabled" actually signifies. At its heart, this error is PyTorch's way of telling you that the installed version of the library lacks the necessary binary components to communicate with your NVIDIA GPU via the CUDA platform. Think of it like trying to play a Blu-ray disc in a standard DVD player—the hardware (your GPU) is capable, but the software (PyTorch) doesn't have the right "decoder" to utilize it.

This mismatch typically stems from one of three scenarios: you installed the CPU-only version of PyTorch, your PyTorch build was compiled for a different CUDA toolkit version than what's on your system, or your NVIDIA driver/CUDA toolkit installation is incomplete or incompatible. The error is an assertion, meaning PyTorch performed a check (likely torch.cuda.is_available()) and found the condition false, then raised this specific error to alert you. It’s not a bug in your code; it's an environment configuration issue. Understanding this distinction is the first step toward a solution, as it shifts your focus from debugging model logic to verifying your software stack.

The Critical First Step: Verifying Your Current Setup

You cannot fix what you don't fully understand. The absolute first action when facing this error is to run a simple diagnostic script to see exactly what your current PyTorch installation believes about its environment. Open a Python terminal or a new notebook cell and execute the following code:

import torch print(f"PyTorch Version: {torch.__version__}") print(f"CUDA Available: {torch.cuda.is_available()}") if torch.cuda.is_available(): print(f"CUDA Device Count: {torch.cuda.device_count()}") print(f"Current CUDA Device: {torch.cuda.current_device()}") print(f"CUDA Device Name: {torch.cuda.get_device_name(0)}") else: print("CUDA is NOT available. Let's troubleshoot.") 

This script provides your baseline. The key output is the CUDA Available: False line, which confirms the error's premise. However, the version string (torch.__version__) is your most valuable clue. A version like 2.1.0+cpu explicitly tells you it's the CPU-only build. A version like 2.1.0+cu118 indicates it's a CUDA 11.8-enabled build. If it shows +cu118 but torch.cuda.is_available() is still False, the problem lies elsewhere—likely with your NVIDIA drivers or CUDA toolkit installation. Always start here; guessing without this data is a recipe for wasted time.

Root Cause #1: The CPU-Only PyTorch Installation (Most Common)

Why You Might Have Installed the Wrong Build

The single most frequent cause of this error is accidentally installing the CPU-only version of PyTorch. This happens for a few common reasons. First, the default command on the official PyTorch Get Started page often has a dropdown for your OS, package manager, language, and compute platform. If "CPU" is selected under "Compute Platform," you'll get the non-CUDA build. Second, many tutorials and older code snippets use the simple pip install torch command, which historically defaulted to the CPU version. Third, if you're on a system without an NVIDIA GPU (like some cloud instances or laptops with integrated graphics), the CPU version is the correct and only choice.

How to Identify a CPU-Only Build

Your version string is the telltale sign. As mentioned, look for the +cpu suffix. For example:

  • torch==2.1.0 (often implies CPU)
  • torch==2.1.0+cpu (explicitly CPU)
  • torch==2.1.0+cu118 (CUDA 11.8 enabled)

You can also check directly in Python:

print(torch.version.cuda) # This will print None for a CPU build 

If this prints None, you have a CPU-only installation.

The Fix: Reinstall with the Correct CUDA Version

The solution is to uninstall the current version and install the correct CUDA-enabled build that matches your system's NVIDIA driver and CUDA toolkit capability. Do not just run pip install torch again. You must specify the correct index URL and version.

  1. Uninstall the existing PyTorch:

    pip uninstall torch torchvision torchaudio -y 

    (If you used conda, use conda remove pytorch torchvision torchaudio -y).

  2. Find Your Compatible CUDA Version: You need to know the maximum CUDA toolkit version your NVIDIA driver supports. Use the NVIDIA driver version you have installed (check with nvidia-smi in your terminal; the top-right corner shows the driver version and the supported CUDA version). Cross-reference this with PyTorch's official compatibility table on their website. For example, driver version 525.60.13 supports up to CUDA 12.0. You then install the PyTorch build for that CUDA version (e.g., cu118 for CUDA 11.8, cu121 for CUDA 12.1).

  3. Install the Correct Build: Use the exact command from the PyTorch website for your OS, package manager (pip/conda), and the correct CUDA version. A typical pip command looks like:

    pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118 

    Replace cu118 with your target version (e.g., cu121, cu117).

  4. Verify the Fix: Rerun your diagnostic script. You should now see CUDA Available: True and a valid torch.version.cuda string.

Root Cause #2: CUDA Toolkit Version Mismatch

The Invisible Compatibility Chain

Even if you installed a CUDA-enabled PyTorch (e.g., +cu118), it won't work if your system's NVIDIA driver or CUDA Toolkit is incompatible. PyTorch's CUDA builds are compiled against a specific version of the CUDA Toolkit (e.g., 11.8). They require a NVIDIA driver that supports at least that toolkit version. The driver acts as the bridge between the OS and the GPU hardware, while the CUDA Toolkit provides the development libraries. PyTorch's binaries link against these toolkit libraries. If your installed driver is too old, it can't support the toolkit version PyTorch was built with. If you have a newer driver but an older toolkit, it might still work due to forward compatibility, but it's not guaranteed.

How to Check Your NVIDIA Driver and CUDA Toolkit

You need to check two things on your host machine:

  1. NVIDIA Driver: Open a terminal/command prompt and run:

    nvidia-smi 

    The output displays the driver version (e.g., 525.60.13) and the CUDA version it supports (e.g., CUDA Version: 12.0). This supported CUDA version is the minimum toolkit version your system can run. Your PyTorch build's CUDA version must be equal to or older than this number.

  2. CUDA Toolkit (Optional but Recommended): While PyTorch's CUDA builds often bundle the necessary runtime libraries, having the full CUDA Toolkit installed can be helpful for other projects. Check with:

    nvcc --version 

    If this command isn't found, you likely don't have the full toolkit installed, which is fine for PyTorch alone but might be needed for custom CUDA kernels.

Resolving the Mismatch

  • If your driver is too old: Update your NVIDIA driver. Download the latest compatible driver from the NVIDIA website. A driver update is often the simplest fix.
  • If your driver is new but you have an old toolkit: You usually don't need to change anything. New drivers are backward compatible. The issue is more likely if your driver is older than the CUDA version your PyTorch build expects.
  • If you need a different PyTorch build: Based on your nvidia-smi output, choose a PyTorch version built for an equal or older CUDA toolkit. For example, if nvidia-smi shows CUDA Version: 11.8, you can install a cu117 (11.7) or cu113 (11.3) build, but not a cu121 (12.1) build. Reinstall PyTorch as described in Root Cause #1 with the appropriate --index-url (e.g., cu117).

Root Cause #3: Incomplete or Corrupted NVIDIA Software Stack

The Missing Pieces: Driver vs. Toolkit vs. Runtime

Sometimes, even with a correct driver and PyTorch build, the underlying CUDA runtime libraries (cudart, cublas, cudnn, etc.) are missing, corrupted, or not on the system's library path. This is more common on fresh system setups, minimal Docker containers, or after a partial uninstall. The nvidia-smi command works because it uses the driver directly, but higher-level CUDA applications like PyTorch need the toolkit's runtime components.

Diagnosing Library Issues

On Linux, you can check for key libraries:

ldconfig -p | grep cuda 

Look for entries like libcudart.so, libcublas.so, libcudnn.so. On Windows, check the C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\vX.Y\bin directory (where X.Y is your version) and ensure it's in your system PATH.

A more definitive test is to try running a simple CUDA sample if you have the toolkit installed, or use a tool like torch.utils.cpp_extension to check for CUDA compiler availability, though this is more advanced.

The Fix: Reinstall the CUDA Toolkit (or Use PyTorch's Bundled Runtime)

  1. Clean Reinstall of CUDA Toolkit: The most reliable fix is to completely uninstall your current NVIDIA CUDA Toolkit and drivers (using the NVIDIA uninstaller or system package manager) and perform a clean installation of the exact toolkit version your PyTorch build expects (e.g., CUDA 11.8). Download the installer from the NVIDIA website, choose a "Custom" installation, and select all components. Reboot after installation.
  2. Leverage PyTorch's CUDA Runtime (Easier): Modern PyTorch pip/conda packages for CUDA often include the necessary CUDA runtime libraries within the package itself, reducing system dependencies. This is why the --index-url https://download.pytorch.org/whl/cu118 method is preferred—it pulls a self-contained wheel. If you're using this method and still have issues, your problem is likely the driver (Root Cause #2) or a corrupted PyTorch install. In this case, a clean reinstall of PyTorch (as in Root Cause #1) is the first step before touching the system toolkit.
  3. Docker Users: If you're in a Docker container, ensure your base image includes the correct CUDA toolkit. The official pytorch/pytorch images tagged with cu118 etc., are pre-configured. If building your own, you must start from an nvidia/cuda base image that matches your target toolkit version.

Advanced Scenarios and Special Cases

WSL2 (Windows Subsystem for Linux) Users

Running PyTorch with CUDA in WSL2 requires specific steps. You must:

  1. Have Windows 11 with the latest updates and WSL2 installed.
  2. Install an NVIDIA GPU driver for Windows that supports WSL2 (available on NVIDIA's site).
  3. Install a WSL2-compatible CUDA toolkitinside your WSL2 Linux distribution. The nvidia-smi command should work from the WSL2 terminal.
  4. Install the Linux version of the CUDA-enabled PyTorch within WSL2. The standard pip install torch --index-url https://download.pytorch.org/whl/cu118 command works here. The error often occurs if step 2 (Windows driver) is missing or outdated.

Conda vs. Pip: Which is Better for CUDA?

Both can work, but there are nuances:

  • Conda: The conda-forge and pytorch channels often provide packages that are more tightly integrated with the system's CUDA toolkit if you install it via conda (conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia). This can be cleaner for managing multiple CUDA versions but can also lead to conflicts if you mix conda and pip installs.
  • Pip: The download.pytorch.org wheels are generally more self-contained and are the recommended method for most users, especially beginners. They are less likely to have system dependency issues.
    Rule of Thumb: Stick to one package manager per environment. If you start with conda, use conda for everything. If you use pip, use pip. Mixing them is a frequent source of "works on my machine" problems.

Multi-Version CUDA Systems

Developers sometimes need multiple CUDA toolkits for different projects. While possible, it's complex. The safest approach is to use conda environments or Docker containers to isolate each project with its specific PyTorch/CUDA version. Do not attempt to switch system-wide CUDA toolkits frequently. Your PyTorch installation's CUDA version is fixed at install time and will look for that specific toolkit version's libraries.

Proactive Prevention and Best Practices

Creating a Bulletproof Setup from Scratch

To avoid this error entirely, follow this checklist for a new machine:

  1. Update NVIDIA Drivers: Download and install the latest Game Ready or Studio driver from NVIDIA's website.
  2. Verify Driver Installation: Run nvidia-smi successfully.
  3. Choose Your PyTorch Version: Go to pytorch.org. Select your OS, package (pip/conda), language (Python), and most importantly, the CUDA version that matches the "CUDA Version" shown in your nvidia-smi output (or one version older for maximum compatibility).
  4. Create a Fresh Virtual Environment: This is non-negotiable.
    # Using venv python -m venv pytorch_env source pytorch_env/bin/activate # Linux/Mac pytorch_env\Scripts\activate # Windows # Or using conda conda create -n pytorch_env python=3.10 conda activate pytorch_env 
  5. Install PyTorch: Run the exact command from the website. Do not modify it.
  6. Immediately Verify: Run the diagnostic script from the beginning. torch.cuda.is_available() must return True before you write a single line of model code.

The "It Works on My Machine" Protocol

To ensure reproducibility and avoid environment drift:

  • Freeze your dependencies: Use pip freeze > requirements.txt or conda env export > environment.yml. This captures the exact PyTorch version and its CUDA suffix.
  • Document the CUDA Version: In your project's README.md, explicitly state: "This project requires PyTorch installed with CUDA 11.8 support. Install using: pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118".
  • Use Docker for Deployment: For any serious project, create a Dockerfile based on an official pytorch/pytorch:2.1.0-cuda11.8-cudnn8-runtime image (adjust tags as needed). This guarantees the environment is identical everywhere.

Frequently Asked Questions (FAQ)

Q1: I have an AMD GPU. Can I use CUDA?
No. CUDA is a proprietary parallel computing platform and API model created by NVIDIA. It only works on NVIDIA GPUs. If you have an AMD GPU, you must look into alternative frameworks like PyTorch with ROCm support (AMD's platform) or OpenCL. The error you're seeing confirms you have an NVIDIA GPU but a software mismatch.

Q2: My nvidia-smi works, but PyTorch still says CUDA isn't available. Help!
This is the classic symptom of Root Cause #1 (CPU-only PyTorch) or Root Cause #3 (missing CUDA runtime libraries). Re-run the diagnostic script and check torch.version.cuda. If it's None, you have the CPU build. If it shows a version (e.g., 11.8) but is_available() is still False, you have a library path issue. Try the clean reinstall steps for both the driver (update it) and PyTorch (use the correct --index-url).

Q3: I'm on a cloud instance (AWS, GCP, Azure). How do I fix this?
Cloud instances often come with drivers pre-installed. Your main task is to install the correct PyTorch CUDA build for the instance's GPU and pre-installed driver/CUDA version. Check the cloud provider's documentation for the instance type (e.g., an AWS p3.2xlarge has a V100 GPU and typically comes with CUDA 11.x). Then, use the matching PyTorch install command. Sometimes, you may need to install the CUDA toolkit yourself if the base image is minimal.

Q4: Can I use a CUDA version newer than what nvidia-smi shows?
No. Your nvidia-smi output shows the maximum CUDA toolkit version your current driver supports. You cannot use a PyTorch build compiled for a newer toolkit version. You must use a build for that version or older. For example, if nvidia-smi says CUDA Version: 11.8, you can use cu118, cu117, cu113, etc., but not cu121.

Q5: Why does torch.cuda.is_available() return False even after a correct install?
Possible reasons include:

  • The NVIDIA driver service isn't running (reboot).
  • You're in a headless SSH session without proper GPU context (use ssh -X or check cloud instance config).
  • Another program is holding the GPU in an exclusive process mode.
  • System library paths (LD_LIBRARY_PATH on Linux) are misconfigured.
  • You have multiple NVIDIA GPUs and there's a conflict (try CUDA_VISIBLE_DEVICES=0 environment variable).

Conclusion: From Error to Empowerment

The "AssertionError: Torch not compiled with CUDA enabled" is not a reflection of your coding skill or the viability of your deep learning project. It is a configuration mismatch, a temporary state between your hardware's potential and your software's setup. By systematically working through the diagnostic steps—first confirming your PyTorch build, then validating your NVIDIA driver, and finally ensuring CUDA runtime libraries are present—you transform this error from a frustrating stop sign into a valuable learning experience about the software stack that powers modern AI.

Remember the golden rule: Your PyTorch CUDA version must be equal to or older than the CUDA version supported by your NVIDIA driver. Armed with the nvidia-smi command and the torch.__version__ string, you have all the data you need to make the correct installation choice. Take the time to set up your environment properly from the start, using virtual environments and explicit install commands. This upfront investment pays massive dividends in training speed, model iteration time, and overall developer sanity. Now, armed with this guide, you can confidently resolve this error, get your models running on the GPU, and focus on what truly matters: building intelligent systems. You've got this.

Torch not compiled with CUDA enabled error - Jetson AGX Xavier - NVIDIA
Torch Not Compiled With CUDA Enabled: Causes, Diagnosis, And
Optimizing Torch: Enabling Cuda For Enhanced Performance