Skip to content

Ability to cache Python downloads in Docker build #16289

@merlinz01

Description

@merlinz01

Question

Is there a way to cache what is downloaded for managed Python installations? As far as I can tell, uv either downloads and extracts in-memory to UV_PYTHON_INSTALL_DIR, or downloads to a temp file, extracts, and then deletes it. Neither would make it possible to cache the Python download in Docker with RUN --mount=type=cache. So with UV_CACHE_DIR cached, even though the package installation is very fast, it has to download Python every time the packages are installed, slowing down the build.

It doesn't work to cache-mount UV_PYTHON_INSTALL_DIR because then the Python binary isn't included in the final image.

This is the relevant part of the Dockerfile I'm using:

# syntax=docker/dockerfile:1-labs
FROM debian:trixie-slim
RUN apt-get update && \
    apt-get install -y ca-certificates git && \
    update-ca-certificates && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*
COPY --from=ghcr.io/astral-sh/uv:0.7 /uv /usr/bin/uv
RUN useradd myuser -d /app -M -U -u 1000
USER myuser
WORKDIR /app
COPY pyproject.toml uv.lock ./
RUN mkdir -p .cache/uv
RUN --mount=type=cache,uid=1000,gid=1000,target=/app/.cache/uv \
    uv sync --compile-bytecode --frozen --no-dev --link-mode=copy

As you can see it takes 2.8 seconds downloading Python:

 > [stage-0  9/12] RUN --mount=type=cache,uid=1000,gid=1000,target=/app/.cache/uv     uv sync --compile-bytecode --frozen --no-dev --link-mode=copy:
0.390 Downloading cpython-3.13.5-linux-x86_64-gnu (download) (33.8MiB)
3.173  Downloading cpython-3.13.5-linux-x86_64-gnu (download)
3.305 Using CPython 3.13.5
3.305 Creating virtual environment at: .venv
3.529 Installed 102 packages in 208ms
4.443 Bytecode compiled 3072 files in 912ms
4.443  + aerich==0.9.2
4.443  + aiofiles==24.1.0
4.443  + aiohappyeyeballs==2.6.1
4.443  + aiohttp==3.12.3
4.443  + aiosignal==1.3.2
...

What I would like is if there was something like $UV_PYTHON_DOWNLOAD_CACHE_DIR (default to $UV_CACHE_DIR/python-download-archives) that is where all the Python distributions (cpython-3.13.5-linux-x86_64-gnu.tar.gz etc) are downloaded to and left intact, then used for subsequent Python installs.

Possibly related:
#13304
#7865
#8025
#14265
#6212
astral-sh/setup-uv#621

Platform

Linux 6.1.0-39-amd64 x86_64 GNU/Linux

Version

uv 0.7.22

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionAsking for clarification or support

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions