|
| 1 | +# Apptainer with uv |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This page describes a workflow for using `uv` with an Apptainer overlay on Torch. Rather than installing packages directly into the container image, both `uv` and the Python environment are stored inside the writable overlay (`/ext3`), allowing the environment to persist across container sessions while leaving the base container unchanged. |
| 6 | + |
| 7 | +This guide assumes you have already created an overlay and know how to launch it. |
| 8 | + |
| 9 | +## Install uv |
| 10 | + |
| 11 | +Launch an Apptainer container with your overlay mounted in read-write mode: |
| 12 | + |
| 13 | +```bash |
| 14 | + |
| 15 | +apptainer exec --fakeroot \ |
| 16 | + |
| 17 | + --overlay overlay-15GB-500K.ext3:rw \ |
| 18 | + |
| 19 | + /share/apps/images/ubuntu-22.04.2.sif \ |
| 20 | + |
| 21 | + /bin/bash |
| 22 | + |
| 23 | +``` |
| 24 | + |
| 25 | +Inside the container, install `uv` into the overlay: |
| 26 | + |
| 27 | +```bash |
| 28 | + |
| 29 | +curl -LsSf https://astral.sh/uv/install.sh | \ |
| 30 | + |
| 31 | +env UV_INSTALL_DIR="/ext3/.uv" sh |
| 32 | + |
| 33 | +``` |
| 34 | + |
| 35 | +Load `uv` into the current shell: |
| 36 | + |
| 37 | +```bash |
| 38 | + |
| 39 | +source /ext3/.uv/env |
| 40 | + |
| 41 | +``` |
| 42 | + |
| 43 | +Configure where `uv` should install Python versions: |
| 44 | + |
| 45 | +```bash |
| 46 | + |
| 47 | +export UV_PYTHON_INSTALL_DIR="/ext3/.uv/python" |
| 48 | + |
| 49 | +``` |
| 50 | + |
| 51 | +## Configure the Environment |
| 52 | + |
| 53 | +Configure where the project virtual environment should be stored: |
| 54 | + |
| 55 | +```bash |
| 56 | + |
| 57 | +export UV_PROJECT_ENVIRONMENT="/ext3/.venv" |
| 58 | + |
| 59 | +``` |
| 60 | + |
| 61 | +If your project already contains a `pyproject.toml` file, install the project dependencies: |
| 62 | + |
| 63 | +```bash |
| 64 | + |
| 65 | +uv sync |
| 66 | + |
| 67 | +``` |
| 68 | + |
| 69 | +## Create an Activation Script |
| 70 | + |
| 71 | +Create `/ext3/env.sh`: |
| 72 | + |
| 73 | +```bash |
| 74 | + |
| 75 | +touch /ext3/env.sh |
| 76 | + |
| 77 | +nano /ext3/env.sh |
| 78 | + |
| 79 | +``` |
| 80 | + |
| 81 | +Add the following: |
| 82 | + |
| 83 | +```bash |
| 84 | + |
| 85 | +#!/bin/bash |
| 86 | + |
| 87 | +unset -f which |
| 88 | + |
| 89 | +source /ext3/.uv/env |
| 90 | + |
| 91 | +export UV_PYTHON_INSTALL_DIR="/ext3/.uv/python" |
| 92 | + |
| 93 | +export UV_PROJECT_ENVIRONMENT="/ext3/.venv" |
| 94 | + |
| 95 | +source /ext3/.venv/bin/activate |
| 96 | + |
| 97 | +export PATH=/ext3/.venv/bin:$PATH |
| 98 | + |
| 99 | +export PYTHONPATH=/ext3/.venv/bin:$PATH |
| 100 | + |
| 101 | +``` |
| 102 | + |
| 103 | +## Reusing the Environment |
| 104 | + |
| 105 | +The next time you start the same container with the same overlay: |
| 106 | + |
| 107 | +```bash |
| 108 | + |
| 109 | +apptainer exec --fakeroot \ |
| 110 | + |
| 111 | + --overlay overlay-15GB-500K.ext3:rw \ |
| 112 | + |
| 113 | + /share/apps/images/ubuntu-22.04.2.sif \ |
| 114 | + |
| 115 | + /bin/bash |
| 116 | + |
| 117 | +``` |
| 118 | + |
| 119 | +Reactivate the environment: |
| 120 | + |
| 121 | +```bash |
| 122 | + |
| 123 | +source /ext3/env.sh |
| 124 | + |
| 125 | +``` |
| 126 | + |
| 127 | +This workflow also works with Open OnDemand JupyterLab. |
0 commit comments