Sub topic 8
What a lock file cannot pin: Python, system libraries and CUDA
This chapter assumes you can read a pip-compile or uv lock file, know what a virtual environment is, and have trained a model in PyTorch or scikit-learn. A lock file fixes the Python packages you install. Several things your code depends on sit outside that file. This chapter names them, shows how each one breaks a run, and shows how to record it.
What a lock file covers
A lock file lists every Python package in the resolved tree, with exact versions and, if you generated them, hashes. Installing from it gives you the same package files each time.
A lock file does not install or verify the interpreter, the operating system libraries, or the GPU stack. The tool that reads it only places packages into the Python it finds on the machine.
The layers underneath the lock file
Only the second box comes from the lock file. The next sections cover the rest.
The Python version
A resolver picks versions that work for a given Python. If you lock on Python 3.11 and install on 3.9, the install can fail, or it can succeed with a different set of packages if your file contains environment markers.
pip-compileresolves for the interpreter that runs it and does not record that interpreter anywhere in the output. Collaborators can lock on different versions without noticing.uvreadsrequires-pythonfrompyproject.tomland stores it inuv.lock. It refuses to install into an interpreter outside that range, but a range such as>=3.10still allows 3.10, 3.11 and 3.12.- Minor versions matter. A wheel built for
cp311does not install on 3.12, and numerical code can change between interpreter releases.
Pin the exact interpreter where the environment is built. In a container, use a base image tag such as python:3.11-slim, then pin that image by digest later. Locally, use a version manager and a .python-version file.
System libraries
Many wheels are not self-contained. They expect shared libraries from the operating system.
| Symptom at import time | Typical missing piece |
|---|---|
| `ImportError: libGL.so.1: cannot open shared object file` | OpenCV needs `libgl1` |
| `OSError: sndfile library not found` | `soundfile` needs `libsndfile1` |
| `ImportError: libgomp.so.1` | OpenMP runtime, from `libgomp1` |
| `GLIBC_2.xx not found` | Wheel built against a newer glibc than the host has |
| Compiler or header errors during install | Package has no wheel for your platform and builds from source |
The last two rows are the quiet ones. Wheels carry platform tags, such as manylinux_2_28, that state the minimum glibc they need. The resolver and the installer check these tags. If no wheel matches, pip falls back to the source distribution and compiles it with whatever compiler and headers the machine has. The lock file hash still verifies, but the compiled result depends on the toolchain. To make this failure visible, install with --only-binary :all: (pip) or --no-build (uv). The install then stops instead of compiling.
Linux distributions also differ. Alpine uses musl instead of glibc, so most manylinux wheels do not install on it. Prefer Debian- or Ubuntu-based images for ML work.
CUDA, cuDNN and the driver
GPU support has three parts, and they live in different places.
- The host driver. It is installed on the machine, provides
libcuda.so, and talks to the hardware. A container cannot bring its own driver. - The CUDA runtime and libraries. These include cuBLAS and cuDNN. PyTorch's Linux wheels install them as separate
nvidia-*Python packages, so the lock file does cover them. Thetorchversion you pick also decides the CUDA version, through the wheel index or local version tag (for example+cu121). - The CUDA toolkit with
nvcc. Only needed to compile CUDA extensions, such as custom kernels or some attention libraries. This part comes from the image, not from the lock file.
The rule that connects them: the driver must be new enough for the CUDA version your libraries use. nvidia-smi prints a "CUDA Version" in its header. That number is the newest CUDA the driver supports, not what is installed. A driver generally supports older CUDA runtimes, and within one major CUDA version, NVIDIA's minor-version compatibility lets a newer minor runtime work on an older driver, with limits. A runtime from a newer major version than the driver supports fails.
The usual error is CUDA driver version is insufficient for CUDA runtime version, or torch.cuda.is_available() returning False with no other message. The fix is to upgrade the host driver or pick an older CUDA build of the framework. Changing the lock file alone does not fix a driver problem.
Recording what the lock file cannot
Since these layers are not pinned by the lock file, record them with each run so a later failure can be traced. This script prints an environment fingerprint that you can save next to your metrics.
import json
import platform
import subprocess
import sys
def gpu_driver() -> str:
try:
out = subprocess.run(
["nvidia-smi", "--query-gpu=driver_version,name",
"--format=csv,noheader"],
capture_output=True, text=True, check=True,
)
return out.stdout.strip()
except (FileNotFoundError, subprocess.CalledProcessError):
return "no NVIDIA driver found"
def torch_info() -> dict:
try:
import torch
except ImportError:
return {"torch": None}
return {
"torch": torch.__version__,
"torch_cuda": torch.version.cuda,
"cudnn": torch.backends.cudnn.version(),
"cuda_available": torch.cuda.is_available(),
}
fingerprint = {
"python": sys.version,
"platform": platform.platform(),
"libc": " ".join(platform.libc_ver()),
"gpu_driver": gpu_driver(),
**torch_info(),
}
print(json.dumps(fingerprint, indent=2))On a machine without a GPU or without PyTorch, the script still runs and reports what is missing. Compare two fingerprints when results differ between machines. A mismatch in libc, torch_cuda or the driver points to the layer that changed.
Common errors
- Assuming
pip installon a new machine means the same Python. Checkpython --versionbefore comparing results. - Debugging a missing
.sofile by changing the lock file. The library comes from the operating system. Add it to the image withapt-get install. - Reading the
nvidia-smiCUDA version as the installed version. It is the driver's maximum. Usetorch.version.cudafor what the framework uses. - Mixing CUDA builds. Installing a
cu118extension against acu121PyTorch can load without error and then crash or silently misbehave. Keep one CUDA version across all packages. - Letting an install compile from source unnoticed. Block source builds so a missing wheel is an error you see.
The next chapters move these layers into a container, which gives you one place to pin all of them except the driver.
Exercise
A colleague's training job fails on a new server with "libGL.so.1: cannot open shared object file" after a clean install from the same hash-pinned lock file. Why did the lock file not prevent this, and where should the fix go?
Show answer
The lock file pins only Python packages, and the OpenCV wheel expects the OpenGL library libgl1 from the operating system. The fix goes in the environment definition, such as an apt-get install -y libgl1 step in the Dockerfile, not in the lock file.
Exercise
nvidia-smi on a host reports "CUDA Version: 12.2", and the image contains PyTorch built for CUDA 12.1. Should you expect it to run, and what would you check if torch.cuda.is_available() returns False?
Show answer
A driver supporting CUDA 12.2 can run a CUDA 12.1 runtime, so it should work. If it returns False, check that the container was started with GPU access (--gpus all and the NVIDIA Container Toolkit), then compare torch.version.cuda with the driver's maximum supported version.