setup-python icon indicating copy to clipboard operation
setup-python copied to clipboard

Support for `uv` Package Manager Caching

Open mgaitan opened this issue 1 year ago • 10 comments

Description Hey there! I'm suggesting we add caching support for the uv package manager in setup-python. uv (uv pip command currently) is a drop-in replace for pip, using requirements.txt or any other input file that pip supports for dependencies definitions. The only difference is its cache directory: setup-python uses the output of pip cache dir to find the cache directory, but uv uses uv cache dir instead (see https://github.com/astral-sh/uv/pull/1734). It should be a straightforward addition that could help projects using uv.

Justification uv is 10-100x faster than pip. There is a lot of interest from the community to use this new tool in CI environments (e.g https://github.com/astral-sh/uv/issues/1386)

Are you willing to submit a PR? Yep! Happy to help out and get my hands dirty with a PR to make this happen. However, I'm far of being a Typescript expert.

mgaitan avatar Feb 21 '24 19:02 mgaitan

You could also just cache the uv cache directory, e.g.,

      - uses: actions/cache@v4
        id: cache-uv
        with:
          path: ~/.cache/uv
          key: ${{ runner.os }}-python-${{ matrix.python-version }}-uv

nschloe avatar Feb 23 '24 07:02 nschloe

You could also just cache the uv cache directory, e.g.,

      - uses: actions/cache@v4
        id: cache-uv
        with:
          path: ~/.cache/uv
          key: ${{ runner.os }}-python-${{ matrix.python-version }}-uv

I have just tested this solution and I think it is not worth using it when your requirements are large packages.

I've added the cache in my public playground with https://github.com/alice-biometrics/uv-playground/commit/14871e815ad18382fa238a89d8b896a19e67f080

And run a workflow to test what happens when run without cache: https://github.com/alice-biometrics/uv-playground/actions/runs/8046635708/job/21974 Screenshot 2024-02-26 at 10 35 38 241996

surprisingly, the size of the cache is very large, so it takes a long time to save in the post-cache. Screenshot 2024-02-26 at 10 36 05

Then, I run it again to test how works with a saved cache: https://github.com/alice-biometrics/uv-playground/actions/runs/8046670133/job/21974345476 Screenshot 2024-02-26 at 10 37 23

The installation time is extremely fast, however, it takes the same amount of time to retrieve the cache as it does to install in a fresh environment with uv.

Example of a workflow without uv cache: https://github.com/alice-biometrics/uv-playground/actions/runs/7986874748/job/21808164311 Screenshot 2024-02-26 at 10 43 23

acostapazo avatar Feb 26 '24 09:02 acostapazo

Heh, I actually wrote a PR that implements this (before this issue, to boot), but no actions/setup-python maintainer seems to have seen it: https://github.com/actions/setup-python/pull/818

akx avatar Mar 27 '24 15:03 akx

I've been playing with replacing pip with uv in our Gitlab CI pipeline, and it massively speeds up installing packages when you've got the cache. However, I've noticed that the uv cache is notably larger than the pip one (along with significantly more files), and the increase in Gitlab's cache time almost outweighs the shift to uv. Anyone else run into this?

alechouse97 avatar Apr 24 '24 14:04 alechouse97

I've been playing with replacing pip with uv in our Gitlab CI pipeline, and it massively speeds up installing packages when you've got the cache. However, I've noticed that the uv cache is notably larger than the pip one (along with significantly more files), and the increase in Gitlab's cache time almost outweighs the shift to uv. Anyone else run into this?

I think that's essentially the same that's reported in https://github.com/actions/setup-python/issues/822#issuecomment-1963717925

edgarrmondragon avatar Apr 24 '24 15:04 edgarrmondragon

Another strategy worth considering would be to cache the whole .venv, as demonstrated in this article: https://adamj.eu/tech/2023/11/02/github-actions-faster-python-virtual-environments/

kdeldycke avatar Jul 24 '24 12:07 kdeldycke

I did some analysis on the uv-playground repo described here by @acostapazo. Interestingly, the pip cache is even larger than the uv cache (2.8 GB vs. 2.2 GB) though both are of course very large. Almost the entirety of the cache (at least in the uv case, I'm less familiar with pip's cache structure) are the enormous torch and nvidia_cudnn_cu12 wheels.

My guess is that it's less efficient for uv to run through the cache in such cases (big pre-built wheels) than it is to install from scratch. The main case where this would not be true is if you had to build things from source, where caching can have a huge impact. I'm experimenting with a uv command (https://github.com/astral-sh/uv/pull/5391) that would enable you to clear pre-built wheels from the cache, but retain anything that was built from source, to get the best of both worlds.

charliermarsh avatar Jul 24 '24 19:07 charliermarsh

Example from our repo: Pandas doesn't have Python 3.13 wheels out yet, but Pandas 2.2.2 can be build on Python 3.13. So it now takes around ~4 minutes to build, either with uv pip or an uncached pip. While cached pip takes only 30 seconds, uv takes only 3 seconds on Python 3.12 where all wheels are available.

So in the case wheels are not yet up yet, caching would provide huge value if also supported for uv.

EwoutH avatar Aug 21 '24 07:08 EwoutH

I have created an action to install uv and modelled it so it can nearly be used as a drop in replacement: https://github.com/eifinger/setup-uv

A simple example workflow could look like this:

- name: Checkout the repository
  uses: actions/checkout@main
- name: Install the latest version of uv
  uses: eifinger/setup-uv@v1
  with:
    enable-cache: true
- name: Test
  run: uv run --frozen pytest

eifinger avatar Aug 28 '24 11:08 eifinger

I've also been trying out https://github.com/hynek/setup-cached-uv

jobs:
  tests:
    runs-on: ubuntu-latest  # or macOS, or Windows
    steps:
      - uses: hynek/setup-cached-uv@v2

      - run: uv ...

hugovk avatar Aug 29 '24 07:08 hugovk