Fixing the 'libcuda.so.1: cannot open shared object file' Error in Docker

beginner🧠 AI Tools2026-05-17| Ubuntu/Debian Linux, Docker, NVIDIA GPU, Python (TensorFlow, PyTorch, or JAX)

Error Message

ImportError: libcuda.so.1: cannot open shared object file: No such file or directory
#docker#cuda#gpu#nvidia-docker#pytorch

The Problem

You launch a Docker container to train a model, but your script crashes immediately. Instead of the expected training logs, you see a specific error message:

ImportError: libcuda.so.1: cannot open shared object file: No such file or directory

This happens when Python libraries like PyTorch or TensorFlow try to talk to your GPU and fail. While your host machine might have the latest NVIDIA drivers, Docker containers are isolated by design. They cannot see the host's GPU hardware or driver files unless you explicitly bridge the gap.

Why This Happens

The file libcuda.so.1 is the core interface for the NVIDIA driver. Unlike CUDA libraries (like libcudnn) which you can bake into a Docker image, the driver library must stay on the host OS. If you do not tell Docker to map these host drivers into the container, your AI applications will be "blind" to the hardware.

Step-by-Step Fix

Step 1: Check Your Host Drivers

First, confirm that your host machine recognizes the GPU. Run this command in your main terminal:

nvidia-smi

You should see a table showing your GPU model (e.g., RTX 4090 or A100) and the driver version (e.g., 535.129.03). If this command fails, you must install the NVIDIA drivers on your host before proceeding with Docker setup.

Step 2: Install the NVIDIA Container Toolkit

Standard Docker installations do not support GPUs. You need the NVIDIA Container Toolkit to allow the container to access the host's libcuda.so.1 file. Follow these steps for Ubuntu or Debian:

# Add the package repositories
curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg \
  && curl -s -L https://nvidia.github.io/libnvidia-container/stable/deb/nvidia-container-toolkit.list | \
    sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' | \
    sudo tee /etc/stderr/apt/sources.list.d/nvidia-container-toolkit.list

# Install the toolkit
sudo apt-get update
sudo apt-get install -y nvidia-container-toolkit

# Restart the Docker daemon to apply the new runtime
sudo systemctl restart docker

Step 3: Launch Docker with GPU Access

Installing the toolkit isn't enough; you have to trigger it when you start your container. Use the --gpus all flag to pass the drivers through. Try this test command:

docker run --rm --gpus all nvidia/cuda:12.0-base-ubuntu22.04 nvidia-smi

If you prefer docker-compose, ensure your yaml file includes the deploy section. This is required for Docker Compose version 1.28.0 and higher:

services:
  ai-app:
    image: pytorch/pytorch:2.1.0-cuda12.1-cudnn8-runtime
    deploy:
      resources:
        reservations:
          devices:
            - driver: nvidia
              count: all
              capabilities: [gpu]

Step 4: Set Capability Variables

In some edge cases, the container still won't "see" the driver libraries. You can force the mapping by adding these environment variables to your Dockerfile or run command:

  • NVIDIA_VISIBLE_DEVICES=all
  • NVIDIA_DRIVER_CAPABILITIES=compute,utility

Verifying the Fix

To be 100% sure the library is mapped, run a quick Python check inside the container environment:

docker run --rm --gpus all python:3.10-slim python3 -c "import ctypes; print(ctypes.util.find_library('cuda'))"

If the output is libcuda.so.1, your setup is correct. If it returns None, the drivers are still not being passed through correctly.

For PyTorch users, this is the ultimate test:

python3 -c "import torch; print(torch.cuda.is_available())"

A True result means your ImportError is officially gone.

Best Practices

Managing driver versions across different servers can get messy. When I download specific driver blobs or setup scripts for production, I use ToolCraft's Hash Generator to verify their SHA-256 integrity. This prevents issues caused by corrupted downloads or mismatched files during deployment.

One final tip: keep an eye on version compatibility. Your host driver version must be equal to or higher than the CUDA version your container expects. If your host has Driver 510 (CUDA 11.6), but your container wants CUDA 12.0, you will likely see the libcuda.so.1 error again.

Related Error Notes