"Could not load dynamic library 'libcudart.so…'"
TensorFlow can't find the CUDA libraries it needs, so it silently falls back to the CPU. From TensorFlow 2.14 on, one install command fixes it.
What you see
Could not load dynamic library 'libcudart.so.12'; dlerror: libcudart.so.12: cannot open shared object file: No such file or directory
Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU.The library name varies (libcublas, libcudnn, libcufft…), and so does the version number, which tells you what the installed TensorFlow was built for.
Why it happens
Unlike PyTorch, which bundles its CUDA libraries in its own wheels, a plainpip install tensorflow expects the CUDA runtime and cuDNN to be present already, in the exact versions it was built with. If they're missing or a different version, you get this warning and no GPU.
The fix
TensorFlow 2.14 and newer (Linux, or WSL2 on Windows): install the variant that pulls in NVIDIA's CUDA libraries from pip:
pip install 'tensorflow[and-cuda]'Then check with python -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))".
Older TensorFlow: install the CUDA Toolkit and cuDNN versions it was built against, and make sure they're on the library path:
| TensorFlow | CUDA | cuDNN | Python |
|---|---|---|---|
| 2.21.0 | 12.5 | 9.3 | 3.10, 3.11, 3.12, 3.13 |
| 2.20.0 | 12.5 | 9.3 | 3.9, 3.10, 3.11, 3.12, 3.13 |
| 2.19.0 | 12.5 | 9.3 | 3.9, 3.10, 3.11, 3.12 |
| 2.18.0 | 12.5 | 9.3 | 3.9, 3.10, 3.11, 3.12 |
| 2.17.0 | 12.3 | 8.9 | 3.9, 3.10, 3.11, 3.12 |
| 2.16.1 | 12.3 | 8.9 | 3.9, 3.10, 3.11, 3.12 |
| 2.15.0 | 12.2 | 8.9 | 3.9, 3.10, 3.11 |
| 2.14.0 | 11.8 | 8.7 | 3.9, 3.10, 3.11 |
Every release is on TensorFlow versions.
On native Windows, no amount of library fixing helps: TensorFlow 2.10 was the last release with GPU support on native Windows. Use WSL2 (then the [and-cuda] command above), or stay on 2.10.
The same kind of error from other packages (onnxruntime-gpu, custom CUDA extensions) has the same cause: the package expects a system CUDA runtime of a specific major version. Install that version's runtime, or a build of the package that bundles it.
Sources: TensorFlow's install guide (tensorflow.org/install/pip) for [and-cuda] and Windows support; the and-cuda extra first appears in the PyPI metadata of TensorFlow 2.14.0; CUDA/cuDNN versions from TensorFlow's tested build configurations (TensorFlow 2.21.0 and earlier on this site); message text from TensorFlow's dso_loader.cc and gpu_device.cc.