Installation & Environmentpytorchcuda

Why PyTorch throws a cuDNN version error, and how to fix it

Error
RuntimeError: cuDNN version incompatibility: PyTorch was compiled against (8, 9, 0) but linked against (8, 5, 0)

Also appears as

  • libcudnn.so.8: cannot open shared object file: No such file or directory
  • RuntimeError: cuDNN error: CUDNN_STATUS_NOT_INITIALIZED

Short answer

PyTorch wheels bundle their own cuDNN version internally, so a separately installed system-wide cuDNN is usually unnecessary and often the actual cause of this error. When LD_LIBRARY_PATH exposes a different cuDNN version than the one torch was compiled against, torch loads the wrong one at runtime and throws a version incompatibility error. Removing the manual cuDNN path and letting torch use its bundled copy resolves most cases.

Affects: PyTorch installs on machines with a manually installed system-wide cuDNN, or environments where multiple ML frameworks each bundle a conflicting cuDNN version.

Fix it in a few minutes

  1. 1Check which cuDNN version torch reports internally: python -c "import torch; print(torch.backends.cudnn.version())".
  2. 2Check for a conflicting system-wide cuDNN on the library path: echo $LD_LIBRARY_PATH and ldconfig -p | grep cudnn.
  3. 3Remove any manually added cuDNN directories from LD_LIBRARY_PATH so torch falls back to its own bundled copy.
  4. 4If another framework like TensorFlow is imported in the same process and needs its own cuDNN, isolate the two into separate environments rather than sharing a process.
  5. 5Re-run your script and confirm torch.backends.cudnn.is_available() returns True with no version warning.

How to confirm this is your problem

  • RuntimeError explicitly naming two different cuDNN version numbers as compiled against versus linked against
  • libcudnn.so.8 or a similarly named file reported as missing despite cuDNN appearing installed
  • CUDNN_STATUS_NOT_INITIALIZED error on the first convolution or attention operation
  • The same code works in one virtual environment but fails in another on the identical machine

Root causes and fixes

Most common

A manually installed system-wide cuDNN shadows torch's bundled copy

PyTorch wheels already include a specific cuDNN version compiled and tested against that exact torch release. When a different cuDNN version is separately installed system-wide, for example copied manually into /usr/local/cuda, and its directory appears in LD_LIBRARY_PATH ahead of torch's bundled path, the dynamic linker loads the system version instead, which was never validated against that torch build.

Fix: Remove the manually installed cuDNN directory from LD_LIBRARY_PATH entirely and let torch load its own bundled cuDNN, which is almost always sufficient since torch does not require a separate system install.

Commands
echo $LD_LIBRARY_PATH
python -c "import torch; print(torch.backends.cudnn.version())"
Common

Multiple environments each with a different cuDNN package are active

Conda environments and pip virtual environments can each install their own cudnn package independently. If a shell session accidentally activates the wrong environment, or a script is launched with an inherited environment variable pointing at a different environment's cuDNN, the version torch expects and the version actually loaded diverge silently.

Fix: Deactivate all environments and start from a clean shell, then activate only the single intended environment, verifying with which python and pip show torch before running the script again.

Common

Manually copied cuDNN files into the CUDA toolkit directory

Some older tutorials instruct users to manually download cuDNN from NVIDIA and copy its header and library files directly into the CUDA toolkit's include and lib64 directories. This global system change affects every CUDA application on the machine, including ones like torch that never needed it, and any mismatch between this manually installed version and what torch expects produces the incompatibility error.

Fix: Remove the manually copied cuDNN files from the CUDA toolkit directory unless another application on the same machine specifically requires them, since torch itself does not need a system cuDNN install.

Occasional

A different framework imported earlier in the process pins a conflicting cuDNN

If TensorFlow, JAX, or another CUDA-aware library is imported before torch in the same Python process and that library dynamically loads its own bundled or system cuDNN first, torch's subsequent attempt to initialize CUDA operations can conflict with the already-loaded library version in memory.

Fix: Avoid importing multiple heavy CUDA frameworks in the same process where possible, or explicitly control import order and verify each framework's expected cuDNN version is compatible before combining them.

Rare

Partial or corrupted manual cuDNN installation missing library symlinks

A manual cuDNN install that copies the versioned library files (e.g. libcudnn.so.8.9.0) but omits creating the standard unversioned symlink (libcudnn.so) that some tools expect can produce a missing library error even though the actual file exists on disk somewhere.

Fix: Recreate the expected symlinks pointing to the actual versioned library file, or simply remove the partial manual install and rely on torch's bundled cuDNN instead.

Diagnostic commands

Check the cuDNN version torch itself reports

python -c "import torch; print(torch.backends.cudnn.version())"

This is the version torch is actually using at runtime; compare it against the version named in any error message to confirm which cuDNN got loaded.

Check for system-installed cuDNN libraries that might conflict

ldconfig -p | grep cudnn

Any entries here indicate a system-wide cuDNN install exists outside of torch's bundled copy, which is the most likely source of a version conflict.

Check what library paths are exposed to the current process

echo $LD_LIBRARY_PATH

A manually added CUDA or cuDNN path listed here, especially ahead of the standard system paths, is the classic cause of loading the wrong cuDNN version.

Confirm cuDNN is usable at all after cleaning up the environment

python -c "import torch; print(torch.backends.cudnn.is_available())"

True with no version warning confirms the fix worked; False or a repeated warning means another conflicting path or environment is still active.

Stopping it from happening again

  • Avoid manually installing a system-wide cuDNN unless a specific non-torch application explicitly requires it.
  • Keep LD_LIBRARY_PATH minimal and free of manually added CUDA or cuDNN directories in your shell profile and Dockerfiles.
  • Isolate different CUDA-aware frameworks like TensorFlow and PyTorch into separate virtual environments or containers rather than one shared process.
  • Document any legitimate reason for a manual cuDNN install on a given machine so future engineers do not remove it blindly or reintroduce the same conflict elsewhere.

When this becomes an architecture problem

If multiple teams share GPU machines with conflicting framework and cuDNN requirements that cannot be resolved through environment isolation alone, this becomes an infrastructure segmentation decision, such as moving to per-team containers or dedicated nodes, rather than an ongoing environment variable troubleshooting exercise.

Frequently asked questions

Do I need to install cuDNN separately for PyTorch to work?

No. PyTorch wheels already bundle a specific, tested cuDNN version internally. A separate system-wide cuDNN install is usually unnecessary for PyTorch alone, and when one exists, it is frequently the actual cause of version mismatch errors rather than a requirement for torch to function.

What does the compiled against versus linked against error actually mean?

It means torch was built and tested against one specific cuDNN version, but at runtime the dynamic linker loaded a different cuDNN version found elsewhere on the library search path, typically from a manually installed system-wide copy. The two versions are not guaranteed to be compatible, so torch raises this error rather than risk incorrect results.

How do I remove a conflicting system cuDNN safely?

First confirm no other application on the machine specifically depends on that system-wide cuDNN install. Then remove its directory from LD_LIBRARY_PATH, or uninstall the package if it was installed via a package manager, and re-run python -c "import torch; print(torch.backends.cudnn.version())" to confirm torch now uses its own bundled version cleanly.

Can this error happen even in a fresh virtual environment?

Yes, if LD_LIBRARY_PATH is set globally in your shell profile rather than per-environment, a fresh virtual environment still inherits the same conflicting system path. Check your shell startup files (.bashrc, .profile) for any CUDA or cuDNN path exports that persist across environments.

Related problems

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.

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

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.

bitsandbytes cannot find or detect a working CUDA setup

bitsandbytes needs to locate the exact CUDA runtime shared library at import time and load a matching precompiled binary for that version. The setup fails most often because LD_LIBRARY_PATH points at a different CUDA installation than the one it detected, or because an older bitsandbytes version could not auto-detect the GPU correctly. Upgrading to the latest bitsandbytes and running its built-in diagnostic resolves the majority of cases.

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.

Guide

On-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.

Guide

vLLM Production Deployment: A Practitioner's Guide

Deploy vLLM in production: continuous batching, PagedAttention, config flags that matter, and the metrics to watch before you trust it with real traffic.

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.