2024-08-23 23:58:26 +02:00
# setup-uv
Set up your GitHub Actions workflow with a specific version of [uv ](https://docs.astral.sh/uv/ ).
2024-09-05 09:24:32 -04:00
- Install a version of uv and add it to PATH
2024-09-04 17:14:10 -04:00
- Cache the installed version of uv to speed up consecutive runs on self-hosted runners
- Register problem matchers for error output
2024-09-05 09:24:32 -04:00
- (Optional) Persist the uv's cache in the GitHub Actions Cache
- (Optional) Verify the checksum of the downloaded uv executable
2024-08-23 23:58:26 +02:00
2024-08-24 00:03:11 +02:00
## Contents
2024-09-04 17:14:10 -04:00
- [Usage ](#usage )
2025-01-27 18:17:02 +01:00
- [Install a required-version or latest (default) ](#install-a-required-version-or-latest-default )
- [Install the latest version ](#install-the-latest-version )
2024-09-16 21:08:44 +02:00
- [Install a specific version ](#install-a-specific-version )
2024-09-21 10:14:36 +02:00
- [Install a version by supplying a semver range ](#install-a-version-by-supplying-a-semver-range )
2025-01-27 18:17:02 +01:00
- [Install a required-version ](#install-a-required-version )
2024-11-28 21:41:37 +01:00
- [Python version ](#python-version )
2024-09-04 17:14:10 -04:00
- [Validate checksum ](#validate-checksum )
- [Enable Caching ](#enable-caching )
- [Cache dependency glob ](#cache-dependency-glob )
2024-09-16 21:08:44 +02:00
- [Local cache path ](#local-cache-path )
2024-11-23 16:30:54 +01:00
- [Disable cache pruning ](#disable-cache-pruning )
- [Ignore nothing to cache ](#ignore-nothing-to-cache )
2024-09-11 10:18:23 +02:00
- [GitHub authentication token ](#github-authentication-token )
2024-09-21 10:14:36 +02:00
- [UV_TOOL_DIR ](#uv_tool_dir )
- [UV_TOOL_BIN_DIR ](#uv_tool_bin_dir )
2024-11-23 09:21:51 +01:00
- [Tilde Expansion ](#tilde-expansion )
2024-09-04 17:14:10 -04:00
- [How it works ](#how-it-works )
- [FAQ ](#faq )
2024-08-24 00:03:11 +02:00
2024-08-23 23:58:26 +02:00
## Usage
2025-01-13 15:32:41 +01:00
### Install a required-version or latest (default)
2024-08-23 23:58:26 +02:00
```yaml
2024-09-05 09:24:32 -04:00
- name: Install the latest version of uv
2024-12-23 16:16:27 -05:00
uses: astral-sh/setup-uv@v5
2024-08-23 23:58:26 +02:00
```
2025-01-13 15:32:41 +01:00
If you do not specify a version, this action will look for a [required-version ](https://docs.astral.sh/uv/reference/settings/#required-version )
in a `uv.toml` or `pyproject.toml` file in the repository root. If none is found, the latest version will be installed.
2024-09-05 09:38:39 -04:00
For an example workflow, see
[here ](https://github.com/charliermarsh/autobot/blob/e42c66659bf97b90ca9ff305a19cc99952d0d43f/.github/workflows/ci.yaml ).
2025-01-13 15:32:41 +01:00
### Install the latest version
```yaml
- name: Install the latest version of uv
uses: astral-sh/setup-uv@v5
with:
version: "latest"
```
2024-09-05 09:24:32 -04:00
### Install a specific version
2024-08-23 23:58:26 +02:00
```yaml
2024-09-05 09:24:32 -04:00
- name: Install a specific version of uv
2024-12-23 16:16:27 -05:00
uses: astral-sh/setup-uv@v5
2024-08-23 23:58:26 +02:00
with:
2024-09-05 09:24:32 -04:00
version: "0.4.4"
2024-08-23 23:58:26 +02:00
```
2024-09-18 11:29:09 +02:00
### Install a version by supplying a semver range
2024-11-23 16:47:24 +01:00
You can specify a [semver range ](https://github.com/npm/node-semver?tab=readme-ov-file#ranges )
2024-09-18 11:29:09 +02:00
to install the latest version that satisfies the range.
```yaml
- name: Install a semver range of uv
2024-12-23 16:16:27 -05:00
uses: astral-sh/setup-uv@v5
2024-09-18 11:29:09 +02:00
with:
2024-10-18 12:35:52 -04:00
version: ">=0.4.0"
2024-09-18 11:29:09 +02:00
```
```yaml
- name: Pinning a minor version of uv
2024-12-23 16:16:27 -05:00
uses: astral-sh/setup-uv@v5
2024-09-18 11:29:09 +02:00
with:
2024-10-18 12:35:52 -04:00
version: "0.4.x"
2024-09-18 11:29:09 +02:00
```
2025-01-13 15:32:41 +01:00
### Install a required-version
You can specify a [required-version ](https://docs.astral.sh/uv/reference/settings/#required-version )
in either a `uv.toml` or `pyproject.toml` file:
```yaml
- name: Install required-version defined in uv.toml
uses: astral-sh/setup-uv@v5
with:
uv-file: "path/to/uv.toml"
```
```yaml
- name: Install required-version defined in pyproject.toml
uses: astral-sh/setup-uv@v5
with:
pyproject-file: "path/to/pyproject.toml"
```
2024-11-28 21:41:37 +01:00
### Python version
2024-12-20 08:24:43 +01:00
You can use the input `python-version` to
- set the environment variable `UV_PYTHON` for the rest of your workflow
- create a new virtual environment with the specified python version
- activate the virtual environment for the rest of your workflow
2024-11-28 21:41:37 +01:00
This will override any python version specifications in `pyproject.toml` and `.python-version`
```yaml
2024-12-20 08:24:43 +01:00
- name: Install the latest version of uv and set the python version to 3.13t
2024-12-23 16:16:27 -05:00
uses: astral-sh/setup-uv@v5
2024-11-28 21:41:37 +01:00
with:
2024-12-20 08:24:43 +01:00
python-version: 3.13t
- run: uv pip install --python=3.13t pip
2024-11-28 21:41:37 +01:00
```
2024-11-28 21:46:52 +01:00
You can combine this with a matrix to test multiple python versions:
```yaml
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.9", "3.10", "3.11", "3.12"]
steps:
- uses: actions/checkout@v4
- name: Install the latest version of uv and set the python version
2024-12-23 16:16:27 -05:00
uses: astral-sh/setup-uv@v5
2024-11-28 21:46:52 +01:00
with:
python-version: ${{ matrix.python-version }}
- name: Test with python ${{ matrix.python-version }}
run: uv run --frozen pytest
```
2024-08-23 23:58:26 +02:00
### Validate checksum
2024-11-23 16:47:24 +01:00
You can specify a checksum to validate the downloaded executable. Checksums up to the default version
2024-09-19 11:10:29 +02:00
are automatically verified by this action. The sha256 hashes can be found on the
2024-09-05 08:06:45 -04:00
[releases page ](https://github.com/astral-sh/uv/releases ) of the uv repo.
2024-08-23 23:58:26 +02:00
```yaml
- name: Install a specific version and validate the checksum
2024-12-23 16:16:27 -05:00
uses: astral-sh/setup-uv@v5
2024-08-23 23:58:26 +02:00
with:
2024-09-05 08:06:45 -04:00
version: "0.3.1"
checksum: "e11b01402ab645392c7ad6044db63d37e4fd1e745e015306993b07695ea5f9f8"
2024-08-23 23:58:26 +02:00
```
### Enable caching
2024-11-23 16:47:24 +01:00
If you enable caching, the [uv cache ](https://docs.astral.sh/uv/concepts/cache/ ) will be uploaded to
the GitHub Actions cache. This can speed up runs that reuse the cache by several minutes.
2024-09-16 21:08:44 +02:00
> [!TIP]
>
2024-09-16 21:33:16 +02:00
> On self-hosted runners this is usually not needed since the cache generated by uv on the runner's
> filesystem is not removed after a run. For more details see [Local cache path](#local-cache-path).
2024-08-23 23:58:26 +02:00
You can optionally define a custom cache key suffix.
```yaml
- name: Enable caching and define a custom cache key suffix
id: setup-uv
2024-12-23 16:16:27 -05:00
uses: astral-sh/setup-uv@v5
2024-08-23 23:58:26 +02:00
with:
enable-cache: true
2024-09-05 08:06:45 -04:00
cache-suffix: "optional-suffix"
2024-08-23 23:58:26 +02:00
```
2024-09-05 09:24:32 -04:00
When the cache was successfully restored, the output `cache-hit` will be set to `true` and you can
use it in subsequent steps. For example, to use the cache in the above case:
2024-08-23 23:58:26 +02:00
```yaml
- name: Do something if the cache was restored
if: steps.setup-uv.outputs.cache-hit == 'true'
run: echo "Cache was restored"
```
#### Cache dependency glob
2024-11-23 16:47:24 +01:00
If you want to control when the GitHub Actions cache is invalidated, specify a glob pattern with the
`cache-dependency-glob` input. The GitHub Actions cache will be invalidated if any file matching the glob pattern
changes. If you use relative paths, they are relative to the repository root.
2024-08-23 23:58:26 +02:00
2024-09-17 08:27:37 +02:00
> [!NOTE]
>
2024-12-08 17:51:13 +01:00
> The default is
> ```yaml
> cache-dependency-glob: |
> **/requirements*.txt
> **/uv.lock
> ```
2024-09-16 21:08:44 +02:00
2024-08-23 23:58:26 +02:00
```yaml
2024-08-25 16:21:20 +02:00
- name: Define a cache dependency glob
2024-12-23 16:16:27 -05:00
uses: astral-sh/setup-uv@v5
2024-08-23 23:58:26 +02:00
with:
enable-cache: true
2024-12-08 17:51:13 +01:00
cache-dependency-glob: "**/pyproject.toml"
2024-08-23 23:58:26 +02:00
```
```yaml
2024-09-16 21:08:44 +02:00
- name: Define a list of cache dependency globs
2024-12-23 16:16:27 -05:00
uses: astral-sh/setup-uv@v5
2024-08-23 23:58:26 +02:00
with:
enable-cache: true
2024-09-16 21:08:44 +02:00
cache-dependency-glob: |
** /requirements*.txt
** /pyproject.toml
2024-08-23 23:58:26 +02:00
```
2024-11-23 09:21:51 +01:00
```yaml
- name: Define an absolute cache dependency glob
2024-12-23 16:16:27 -05:00
uses: astral-sh/setup-uv@v5
2024-11-23 09:21:51 +01:00
with:
enable-cache: true
cache-dependency-glob: "/tmp/my-folder/requirements*.txt"
```
2024-09-06 14:44:31 +02:00
```yaml
2024-09-16 21:08:44 +02:00
- name: Never invalidate the cache
2024-12-23 16:16:27 -05:00
uses: astral-sh/setup-uv@v5
2024-09-06 14:44:31 +02:00
with:
enable-cache: true
2024-09-16 21:08:44 +02:00
cache-dependency-glob: ""
```
### Local cache path
2024-09-21 10:14:36 +02:00
This action controls where uv stores its cache on the runner's filesystem by setting `UV_CACHE_DIR` .
It defaults to `setup-uv-cache` in the `TMP` dir, `D:\a\_temp\uv-tool-dir` on Windows and
`/tmp/setup-uv-cache` on Linux/macOS. You can change the default by specifying the path with the
`cache-local-path` input.
2024-09-16 21:08:44 +02:00
```yaml
- name: Define a custom uv cache path
2024-12-23 16:16:27 -05:00
uses: astral-sh/setup-uv@v5
2024-09-16 21:08:44 +02:00
with:
cache-local-path: "/path/to/cache"
2024-09-06 14:44:31 +02:00
```
2024-10-25 08:11:32 -04:00
### Disable cache pruning
By default, the uv cache is pruned after every run, removing pre-built wheels, but retaining any
wheels that were built from source. On GitHub-hosted runners, it's typically faster to omit those
pre-built wheels from the cache (and instead re-download them from the registry on each run).
However, on self-hosted or local runners, preserving the cache may be more efficient. See
2024-11-23 16:47:24 +01:00
the [documentation ](https://docs.astral.sh/uv/concepts/cache/#caching-in-continuous-integration ) for
more information.
2024-10-25 08:11:32 -04:00
If you want to persist the entire cache across runs, disable cache pruning with the `prune-cache`
input.
```yaml
- name: Don't prune the cache before saving it
2024-12-23 16:16:27 -05:00
uses: astral-sh/setup-uv@v5
2024-10-25 08:11:32 -04:00
with:
enable-cache: true
prune-cache: false
```
2024-11-23 16:30:54 +01:00
### Ignore nothing to cache
2024-11-23 17:24:42 +01:00
By default, the action will fail if caching is enabled but there is nothing to upload (the uv cache directory does not exist).
2024-11-23 16:47:24 +01:00
If you want to ignore this, set the `ignore-nothing-to-cache` input to `true` .
2024-11-23 16:30:54 +01:00
```yaml
- name: Ignore nothing to cache
2024-12-23 16:16:27 -05:00
uses: astral-sh/setup-uv@v5
2024-11-23 16:30:54 +01:00
with:
enable-cache: true
ignore-nothing-to-cache: true
```
2024-09-11 10:18:23 +02:00
### GitHub authentication token
2024-09-16 21:08:44 +02:00
This action uses the GitHub API to fetch the uv release artifacts. To avoid hitting the GitHub API
2024-09-11 10:18:23 +02:00
rate limit too quickly, an authentication token can be provided via the `github-token` input. By
default, the `GITHUB_TOKEN` secret is used, which is automatically provided by GitHub Actions.
2024-08-23 23:58:26 +02:00
2024-09-11 10:18:23 +02:00
If the default
[permissions for the GitHub token ](https://docs.github.com/en/actions/security-for-github-actions/security-guides/automatic-token-authentication#permissions-for-the-github_token )
are not sufficient, you can provide a custom GitHub token with the necessary permissions.
2024-08-23 23:58:26 +02:00
```yaml
2024-09-11 10:18:23 +02:00
- name: Install the latest version of uv with a custom GitHub token
2024-12-23 16:16:27 -05:00
uses: astral-sh/setup-uv@v5
2024-08-23 23:58:26 +02:00
with:
2024-09-11 10:18:23 +02:00
github-token: ${{ secrets.CUSTOM_GITHUB_TOKEN }}
2024-08-23 23:58:26 +02:00
```
2024-09-21 10:14:36 +02:00
### UV_TOOL_DIR
On Windows `UV_TOOL_DIR` is set to `uv-tool-dir` in the `TMP` dir (e.g. `D:\a\_temp\uv-tool-dir` ).
On GitHub hosted runners this is on the much faster `D:` drive.
On all other platforms the tool environments are placed in the
[default location ](https://docs.astral.sh/uv/concepts/tools/#tools-directory ).
If you want to change this behaviour (especially on self-hosted runners) you can use the `tool-dir`
input:
```yaml
- name: Install the latest version of uv with a custom tool dir
2024-12-23 16:16:27 -05:00
uses: astral-sh/setup-uv@v5
2024-09-21 10:14:36 +02:00
with:
tool-dir: "/path/to/tool/dir"
```
### UV_TOOL_BIN_DIR
On Windows `UV_TOOL_BIN_DIR` is set to `uv-tool-bin-dir` in the `TMP` dir (e.g.
`D:\a\_temp\uv-tool-bin-dir` ). On GitHub hosted runners this is on the much faster `D:` drive. This
path is also automatically added to the PATH.
On all other platforms the tool binaries get installed to the
[default location ](https://docs.astral.sh/uv/concepts/tools/#the-bin-directory ).
If you want to change this behaviour (especially on self-hosted runners) you can use the
`tool-bin-dir` input:
```yaml
- name: Install the latest version of uv with a custom tool bin dir
2024-12-23 16:16:27 -05:00
uses: astral-sh/setup-uv@v5
2024-09-21 10:14:36 +02:00
with:
tool-bin-dir: "/path/to/tool-bin/dir"
```
2024-11-23 09:21:51 +01:00
### Tilde Expansion
This action supports expanding the `~` character to the user's home directory for the following inputs:
- `cache-local-path`
- `tool-dir`
- `tool-bin-dir`
- `cache-dependency-glob`
```yaml
- name: Expand the tilde character
2024-12-23 16:16:27 -05:00
uses: astral-sh/setup-uv@v5
2024-11-23 09:21:51 +01:00
with:
cache-local-path: "~/path/to/cache"
tool-dir: "~/path/to/tool/dir"
tool-bin-dir: "~/path/to/tool-bin/dir"
cache-dependency-glob: "~/my-cache-buster"
```
2024-08-23 23:58:26 +02:00
## How it works
2024-09-05 09:24:32 -04:00
This action downloads uv from the uv repo's official
[GitHub Releases ](https://github.com/astral-sh/uv ) and uses the
[GitHub Actions Toolkit ](https://github.com/actions/toolkit ) to cache it as a tool to speed up
consecutive runs on self-hosted runners.
2024-08-23 23:58:26 +02:00
2024-09-05 09:24:32 -04:00
The installed version of uv is then added to the runner PATH, enabling subsequent steps to invoke it
by name (`uv` ).
2024-08-23 23:58:26 +02:00
## FAQ
2024-09-05 09:38:39 -04:00
### Do I still need `actions/setup-python` alongside `setup-uv`?
2024-08-23 23:58:26 +02:00
2024-12-22 12:13:40 +01:00
With `setup-uv` , you can install a specific version of Python using `uv python install` rather than
2024-09-05 15:54:40 +02:00
relying on `actions/setup-python` .
2024-08-23 23:58:26 +02:00
2024-12-22 12:13:40 +01:00
Using `actions/setup-python` can be faster, because GitHub caches the Python versions alongside the runner.
2024-09-05 09:24:32 -04:00
For example:
2024-08-23 23:58:26 +02:00
```yaml
- name: Checkout the repository
uses: actions/checkout@main
- name: Install the latest version of uv
2024-12-23 16:16:27 -05:00
uses: astral-sh/setup-uv@v5
2024-08-23 23:58:26 +02:00
with:
enable-cache: true
- name: Test
2024-12-22 12:13:40 +01:00
run: uv run --frozen pytest # Uses the Python version automatically installed by uv
2024-08-23 23:58:26 +02:00
```
2024-09-05 09:24:32 -04:00
To install a specific version of Python, use
2024-09-05 08:06:45 -04:00
[`uv python install` ](https://docs.astral.sh/uv/guides/install-python/ ):
2024-08-28 13:28:02 +02:00
```yaml
- name: Install the latest version of uv
2024-12-23 16:16:27 -05:00
uses: astral-sh/setup-uv@v5
2024-08-28 13:28:02 +02:00
with:
enable-cache: true
- name: Install Python 3.12
run: uv python install 3.12
```
2024-08-23 23:58:26 +02:00
### What is the default version?
2024-09-05 09:24:32 -04:00
By default, this action installs the latest version of uv.
2024-08-23 23:58:26 +02:00
2024-09-05 09:24:32 -04:00
If you require the installed version in subsequent steps of your workflow, use the `uv-version`
output:
2024-08-23 23:58:26 +02:00
```yaml
- name: Checkout the repository
2024-08-25 16:21:20 +02:00
uses: actions/checkout@main
2024-08-23 23:58:26 +02:00
- name: Install the default version of uv
id: setup-uv
2024-12-23 16:16:27 -05:00
uses: astral-sh/setup-uv@v5
2024-08-23 23:58:26 +02:00
- name: Print the installed version
run: echo "Installed uv version is ${{ steps.setup-uv.outputs.uv-version }}"
```
2025-02-21 10:47:37 +01:00
### Should I include the resolution strategy in the cache key?
**Yes!**
The cache key gets computed by using the [cache-dependency-glob ](#cache-dependency-glob ).
If you
have jobs which use the same dependency definitions from `requirements.txt` or
`pyproject.toml` but different
[resolution strategies ](https://docs.astral.sh/uv/concepts/resolution/#resolution-strategy ),
each job will have different dependencies or dependency versions.
But if you do not add the resolution strategy as a [cache-suffix ](#enable-caching ),
they will have the same cache key.
This means the first job which starts uploading its cache will win and all other job will fail
uploading the cache,
because they try to upload with the same cache key.
You might see errors like
`Failed to save: Failed to CreateCacheEntry: Received non-retryable error: Failed request: (409) Conflict: cache entry with the same key, version, and scope already exists`
### Why do I see warnings like `Cache not found for keys`
When a workflow runs for the first time on a branch and has a new cache key, because the
[cache-dependency-glob ](#cache-dependency-glob ) found changed files (changed dependencies),
the cache will not be found and the warning `Cache not found for keys` will be printed.
While this might be irritating at first, it is expected behaviour and the cache will be created
and reused in later workflows.
The reason for the warning is, that we have to way to know if this is the first run of a new
cache key or the user accidentally misconfigured the [cache-dependency-glob ](#cache-dependency-glob )
or [cache-suffix ](#enable-caching ) and the cache never gets used.
2024-09-04 17:13:00 -04:00
## Acknowledgements
`setup-uv` was initially written and published by [Kevin Stillhammer ](https://github.com/eifinger )
2024-09-05 08:06:45 -04:00
before moving under the official [Astral ](https://github.com/astral-sh ) GitHub organization. You can
support Kevin's work in open source on [Buy me a coffee ](https://www.buymeacoffee.com/eifinger ) or
[PayPal ](https://paypal.me/kevinstillhammer ).
2024-09-04 17:13:00 -04:00
## License
MIT
2024-08-23 23:58:26 +02:00
2024-09-04 17:13:00 -04:00
< div align = "center" >
< a target = "_blank" href = "https://astral.sh" style = "background:none" >
< img src = "https://raw.githubusercontent.com/astral-sh/uv/main/assets/svg/Astral.svg" alt = "Made by Astral" >
< / a >
< / div >