|
| 1 | +# Native uv Python and package management |
| 2 | + |
| 3 | +This tutorial is an alternative to *Option 1: Using uv* in the [README.md](./README.md) document for those who prefer `uv`'s native commands over the `uv pip` interface. While `uv pip` is faster than pure `pip`, `uv`'s native interface is even faster than `uv pip` as it has less overhead and doesn't have to handle legacy support for PyPy package dependency management. |
| 4 | + |
| 5 | +Otherwise, similar to *Option 1: Using uv* in the [README.md](./README.md) , this section guides you through the Python setup and package installation procedure using `uv`. |
| 6 | + |
| 7 | +In this tutorial, I am using a computer running macOS, but this workflow is similar for Linux machines and may work for other operating systems as well. |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +## 1. Install uv |
| 12 | + |
| 13 | +Uv can be installed as follows, depending on your operating system. |
| 14 | + |
| 15 | + |
| 16 | +**macOS and Linux** |
| 17 | + |
| 18 | +```bash |
| 19 | +curl -LsSf https://astral.sh/uv/install.sh | sh |
| 20 | +``` |
| 21 | + |
| 22 | +or |
| 23 | + |
| 24 | +```bash |
| 25 | +wget -qO- https://astral.sh/uv/install.sh | sh |
| 26 | +``` |
| 27 | + |
| 28 | + |
| 29 | +**Windows** |
| 30 | + |
| 31 | +```bash |
| 32 | +powershell -c "irm https://astral.sh/uv/install.ps1 | more" |
| 33 | +``` |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +> [!NOTE] |
| 38 | +> For more installation options, please refer to the official [uv documentation](https://docs.astral.sh/uv/getting-started/installation/#standalone-installer). |
| 39 | +
|
| 40 | + |
| 41 | +## 2. Install Python |
| 42 | + |
| 43 | +You can install Python using uv: |
| 44 | + |
| 45 | +```bash |
| 46 | +uv python install 3.10 |
| 47 | +``` |
| 48 | + |
| 49 | + |
| 50 | +> [!NOTE] |
| 51 | +> I recommend installing a Python version that is at least 2 versions older than the most recent release to ensure PyTorch compatibility. For example, if the most recent version is Python 3.13, I recommend installing version 3.10 or 3.11. You can find out the most recent Python version by visiting [python.org](https://www.python.org/downloads/). |
| 52 | +
|
| 53 | + |
| 54 | +## 3. Install Python packages and dependencies |
| 55 | + |
| 56 | +To install all required packages from a `pyproject.toml` file (such as the one located at the top level of this GitHub repository), run the following command, assuming the file is in the same directory as your terminal session: |
| 57 | + |
| 58 | +```bash |
| 59 | +uv add . --dev |
| 60 | +``` |
| 61 | + |
| 62 | +<img src="https://sebastianraschka.com/images/LLMs-from-scratch-images/setup/uv-setup/uv-add.png?1" width="700" height="auto" alt="Uv install"> |
| 63 | + |
| 64 | +Note that the `uv add` command above will create a separate virtual environment via the `.venv` subfolder. |
| 65 | + |
| 66 | +You can install new packages, that are not specified in the `pyproject.toml` via `uv add`, for example: |
| 67 | + |
| 68 | +```bash |
| 69 | +uv add packaging |
| 70 | +``` |
| 71 | + |
| 72 | + |
| 73 | +## Optional: Manage virtual environments manually |
| 74 | + |
| 75 | +Alternatively, you can still install the dependencies directly from the repository using `uv pip install`. Note that this requires creating and activating the virtual environment manually: |
| 76 | + |
| 77 | + |
| 78 | +**1. Create a new virtual environment** |
| 79 | + |
| 80 | +Run the following command to manually create a new virtual environment, which will be saved via a new `.venv` subfolder: |
| 81 | + |
| 82 | +```bash |
| 83 | +uv venv --python=python3.10 |
| 84 | +``` |
| 85 | + |
| 86 | + |
| 87 | +**2. Activate virtual environment** |
| 88 | + |
| 89 | +Next, we need to activate this new virtual environment. |
| 90 | + |
| 91 | +On macOS/Linux: |
| 92 | + |
| 93 | +```bash |
| 94 | +source .venv/bin/activate |
| 95 | +``` |
| 96 | + |
| 97 | +On Windows (PowerShell): |
| 98 | + |
| 99 | +```bash |
| 100 | +.venv\Scripts\activate |
| 101 | +``` |
| 102 | + |
| 103 | + |
| 104 | +**3. Install dependencies** |
| 105 | + |
| 106 | +Finally, we can install dependencies from a remote location using the `uv pip` interface: |
| 107 | + |
| 108 | +```bash |
| 109 | +uv pip install -U -r https://raw.githubusercontent.com/rasbt/LLMs-from-scratch/refs/heads/main/requirements.txt |
| 110 | +``` |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | +## 4. Run Python code |
| 116 | + |
| 117 | + |
| 118 | +**Finalizing the setup** |
| 119 | + |
| 120 | +Your environment should now be ready to run the code in the repository. |
| 121 | + |
| 122 | +Optionally, you can run an environment check by executing the `python_environment_check.py` script in this repository: |
| 123 | + |
| 124 | +```bash |
| 125 | +uv run python setup/02_installing-python-libraries/python_environment_check.py |
| 126 | +``` |
| 127 | + |
| 128 | + |
| 129 | + |
| 130 | +<img src="https://sebastianraschka.com/images/LLMs-from-scratch-images/setup/uv-setup/uv-run-check.png?1" width="700" height="auto" alt="Uv install"> |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | +Or, if you don't want to type `uv run python` ever time you execute code, manually activate the virtual environment first. |
| 135 | + |
| 136 | +On macOS/Linux: |
| 137 | + |
| 138 | +```bash |
| 139 | +source .venv/bin/activate |
| 140 | +``` |
| 141 | + |
| 142 | +On Windows (PowerShell): |
| 143 | + |
| 144 | +```bash |
| 145 | +.venv\Scripts\activate |
| 146 | +``` |
| 147 | + |
| 148 | +Then, run: |
| 149 | + |
| 150 | + |
| 151 | +```bash |
| 152 | +python setup/02_installing-python-libraries/python_environment_check.py |
| 153 | +``` |
| 154 | + |
| 155 | + |
| 156 | +**Launching JupyterLab** |
| 157 | + |
| 158 | +You can launch a JupyterLab instance via: |
| 159 | + |
| 160 | +```bash |
| 161 | +uv run jupyter lab |
| 162 | +``` |
| 163 | + |
| 164 | +Or, if you manually activated the environment as described earlier, you can drop the `uv run` prefix. |
| 165 | + |
| 166 | + |
| 167 | + |
| 168 | +--- |
| 169 | + |
| 170 | +Any questions? Please feel free to reach out in the [Discussion Forum](https://github.com/rasbt/LLMs-from-scratch/discussions). |
0 commit comments