|
| 1 | +(uv-page)= |
| 2 | + |
| 3 | +# uv environments |
| 4 | + |
| 5 | +[uv](https://docs.astral.sh/uv/) is an extremely fast Python package and project manager, written in Rust. It can install Python packages, manage virtual environments, and handle Python versions. |
| 6 | + |
| 7 | +Nextflow has built-in support for uv that allows the configuration of workflow dependencies using Python packages, requirements files, or pyproject.toml files. |
| 8 | + |
| 9 | +This allows Nextflow applications to use Python packages managed by uv, taking advantage of its speed and reliability for creating reproducible Python environments. |
| 10 | + |
| 11 | +## Prerequisites |
| 12 | + |
| 13 | +This feature requires the [uv](https://docs.astral.sh/uv/getting-started/installation/) package manager to be installed on your system. |
| 14 | + |
| 15 | +## How it works |
| 16 | + |
| 17 | +Nextflow automatically creates and activates uv virtual environments given the dependencies specified by each process. |
| 18 | + |
| 19 | +Dependencies are specified by using the {ref}`process-uv` directive, providing either the names of the required Python packages, the path of a requirements file, the path of a pyproject.toml file, or the path of an existing virtual environment directory. |
| 20 | + |
| 21 | +You can specify the directory where the uv environments are stored using the `uv.cacheDir` configuration property (see the {ref}`configuration page <config-uv>` for details). When using a computing cluster, make sure to use a shared file system path accessible from all compute nodes. |
| 22 | + |
| 23 | +:::{warning} |
| 24 | +The uv environment feature is not supported by executors that use remote object storage as the work directory, e.g. AWS Batch. |
| 25 | +::: |
| 26 | + |
| 27 | +### Enabling uv environments |
| 28 | + |
| 29 | +The use of uv packages specified using the {ref}`process-uv` directive needs to be enabled explicitly by setting the option shown below in the pipeline configuration file (i.e. `nextflow.config`): |
| 30 | + |
| 31 | +```groovy |
| 32 | +uv.enabled = true |
| 33 | +``` |
| 34 | + |
| 35 | +Alternatively, it can be specified by setting the variable `NXF_UV_ENABLED=true` in your environment or by using the `-with-uv` command line option. |
| 36 | + |
| 37 | +### Use Python package names |
| 38 | + |
| 39 | +Python package names can be specified using the `uv` directive. Multiple package names can be specified by separating them with a blank space. For example: |
| 40 | + |
| 41 | +```nextflow |
| 42 | +process hello { |
| 43 | + uv 'numpy pandas matplotlib' |
| 44 | +
|
| 45 | + script: |
| 46 | + ''' |
| 47 | + python my_script.py |
| 48 | + ''' |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +Using the above definition, a uv virtual environment that includes NumPy, Pandas, and Matplotlib is created and activated when the process is executed. |
| 53 | + |
| 54 | +The usual pip package syntax and naming conventions can be used. The version of a package can be specified using pip version specifiers like so: `numpy>=1.24 pandas==2.0.0`. |
| 55 | + |
| 56 | +### Use requirements files |
| 57 | + |
| 58 | +uv environments can also be defined using a requirements file. For example, given a `requirements.txt` file: |
| 59 | + |
| 60 | +``` |
| 61 | +numpy>=1.24.0 |
| 62 | +pandas>=2.0 |
| 63 | +scikit-learn |
| 64 | +matplotlib |
| 65 | +``` |
| 66 | + |
| 67 | +The environment for a process can be specified like so: |
| 68 | + |
| 69 | +```nextflow |
| 70 | +process hello { |
| 71 | + uv '/path/to/requirements.txt' |
| 72 | +
|
| 73 | + script: |
| 74 | + ''' |
| 75 | + python my_script.py |
| 76 | + ''' |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +### Use pyproject.toml files |
| 81 | + |
| 82 | +uv can also install dependencies from a `pyproject.toml` file: |
| 83 | + |
| 84 | +```nextflow |
| 85 | +process hello { |
| 86 | + uv '/path/to/pyproject.toml' |
| 87 | +
|
| 88 | + script: |
| 89 | + ''' |
| 90 | + python my_script.py |
| 91 | + ''' |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +### Use existing environments |
| 96 | + |
| 97 | +If you already have a uv virtual environment, you can use it directly by specifying the path: |
| 98 | + |
| 99 | +```nextflow |
| 100 | +process hello { |
| 101 | + uv '/path/to/existing/venv' |
| 102 | +
|
| 103 | + script: |
| 104 | + ''' |
| 105 | + python my_script.py |
| 106 | + ''' |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +### Environment caching |
| 111 | + |
| 112 | +Nextflow caches uv environments so that they are created only once for each unique set of packages. The cache directory can be configured using the `uv.cacheDir` setting or the `NXF_UV_CACHEDIR` environment variable. |
| 113 | + |
| 114 | +### Python version |
| 115 | + |
| 116 | +You can specify the Python version to use when creating virtual environments: |
| 117 | + |
| 118 | +```groovy |
| 119 | +uv.pythonVersion = '3.12' |
| 120 | +``` |
| 121 | + |
| 122 | +### Advanced settings |
| 123 | + |
| 124 | +The following settings are available in the `uv` scope of the Nextflow configuration: |
| 125 | + |
| 126 | +- `uv.enabled`: Enable the use of uv environments (default: `false`) |
| 127 | +- `uv.cacheDir`: The path where uv environments are stored |
| 128 | +- `uv.createTimeout`: Timeout for environment creation (default: `20 min`) |
| 129 | +- `uv.installOptions`: Extra command line options for `uv pip install` |
| 130 | +- `uv.pythonVersion`: Python version for virtual environment creation |
0 commit comments