Skip to Content

Why pin CPU-only torch first in a Dockerfile?

Category: DevOps & Deployment

Answer

A vanilla pip install torch on a CUDA machine pulls CUDA-enabled wheels (~2.5 GB). Pin CPU-only with --index-url https://download.pytorch.org/whl/cpu and you save 2 GB per layer, faster builds, smaller images. If you ever need GPU, switch the index URL at build time.

Concrete examples from the fca project context

Example 1

pip install torch==x.y.z —index-url https://download.pytorch.org/whl/cpu

Example 2

Then pip install -r requirements.txt (avoid letting requirements.txt pull torch again).

Example 3

Bake this into the builder stage; CPU-only is fine for inference unless the workload actually needs GPU.

Data flow / flow chart

pip install torch --index-url .../whl/cpu
   THEN pip install -r requirements.txt
   (do not let requirements.txt bring in another torch)

Takeaway

Two GB per image adds up. Pin CPU-only torch first; switch the index URL only when you need GPU.