UV: Modern Python Package Manager That's 10-100x Faster
Managing Python dependencies has always been a challenge. We need to install pip, virtualenv, pyenv, and poetry separately, then configure each tool individually. UV arrives as an all-in-one solution that replaces all these tools with 10-100x faster performance. Written in Rust by the Astral team (creators of Ruff), UV is a next-generation Python package manager that transforms how we work with the Python ecosystem.
In this tutorial, we will learn UV comprehensively from installation, project management, virtual environments, to a practical example of building an ML project from scratch.
Why UV?
Before diving into the technical details, let's understand why UV is worth considering:
- Exceptional speed: 10-100x faster than pip thanks to Rust implementation and global caching
- All-in-one: Replaces pip, pip-tools, virtualenv, pyenv, and poetry in a single binary
- Built-in lockfile: Reproducible builds without additional configuration
- Python version management: Install and switch Python versions without pyenv
- Cross-platform: Works consistently on Linux, macOS, and Windows
- Drop-in replacement: Compatible with the existing pip ecosystem
Installing UV
Linux and macOS
curl -LsSf https://astral.sh/uv/install.sh | sh
Windows (PowerShell)
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
Using pip (alternative)
pip install uv
Using Homebrew (macOS)
brew install uv
Verify the installation:
uv --version
Updating UV
uv self update
Creating Projects with uv init
UV provides an easy way to start new Python projects:
uv init my-project
cd my-project
This command creates the following project structure:
my-project/
├── .python-version
├── README.md
├── hello.py
└── pyproject.toml
The generated pyproject.toml:
[project]
name = "my-project"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.12"
dependencies = []
Initialization Options
# Create project as a library (with src layout)
uv init --lib my-library
Create project as an application
uv init --app my-app
Specify minimum Python version
uv init --python 3.11 my-project
Create project in current directory
uv init
For library projects, the structure will be different:
my-library/
├── .python-version
├── README.md
├── pyproject.toml
└── src/
└── mylibrary/
├── init.py
└── py.typed
Dependency Management
Adding Dependencies
# Add a package
uv add requests
Add with specific version constraints
uv add "pandas>=2.0,<3.0"
Add multiple packages at once
uv add numpy scikit-learn matplotlib
Add development dependency
uv add --dev pytest ruff mypy
Add optional dependency group
uv add --group docs sphinx sphinx-rtd-theme
Each time you run uv add, UV will automatically:
pyproject.tomluv.lockRemoving Dependencies
# Remove a package
uv remove requests
Remove a dev dependency
uv remove --dev mypy
Viewing the Dependency Tree
uv tree
Example output:
my-project v0.1.0
├── numpy v1.26.4
├── pandas v2.2.1
│ ├── numpy v1.26.4
│ ├── python-dateutil v2.9.0
│ │ └── six v1.16.0
│ ├── pytz v2024.1
│ └── tzdata v2024.1
└── scikit-learn v1.4.1
├── joblib v1.3.2
├── numpy v1.26.4
├── scipy v1.12.0
│ └── numpy v1.26.4
└── threadpoolctl v3.3.0
Syncing Dependencies
# Install all dependencies from lockfile
uv sync
Install including dev dependencies
uv sync --dev
Install without dev dependencies (for production)
uv sync --no-dev
Install specific group
uv sync --group docs
Virtual Environments with uv venv
Creating a Virtual Environment
# Create default venv (.venv)
uv venv
Create with custom name
uv venv my-env
Create with specific Python version
uv venv --python 3.11
Create with seed packages (pip, setuptools)
uv venv --seed
Activating the Virtual Environment
# Linux/macOS
source .venv/bin/activate
Windows (PowerShell)
.venv\Scripts\Activate.ps1
Windows (CMD)
.venv\Scripts\activate.bat
Using uv pip (Compatibility Mode)
UV also provides a pip-compatible interface:
# Install a package
uv pip install requests
Install from requirements.txt
uv pip install -r requirements.txt
Freeze dependencies
uv pip freeze
Compile requirements
uv pip compile requirements.in -o requirements.txt
Uninstall
uv pip uninstall requests
Lockfile (uv.lock)
UV automatically creates and manages the uv.lock file containing exact versions of all dependencies. This file ensures reproducible builds across all environments.
# Generate/update lockfile
uv lock
Update specific package in lockfile
uv lock --upgrade-package requests
Update all packages
uv lock --upgrade
The uv.lock file should be committed to version control (git) to ensure all developers and CI/CD pipelines use the same versions.
Example uv.lock
version = 1
requires-python = ">=3.12"
[[package]]
name = "numpy"
version = "1.26.4"
source = { registry = "https://pypi.org/simple" }
sdist = { url = "...", hash = "sha256:..." }
wheels = [
{ url = "...", hash = "sha256:..." },
]
Running Scripts with uv run
uv run allows you to execute commands in the project context without manually activating the virtual environment:
# Run a Python script
uv run python main.py
Run pytest
uv run pytest
Run a module
uv run python -m flask run
Run with inline dependencies
uv run --with rich python -c "from rich import print; print('[bold green]Hello UV![/]')"
Inline Script Dependencies
UV supports PEP 723 inline script metadata. You can define dependencies directly inside Python files:
# /// script
requires-python = ">=3.12"
dependencies = [
"requests",
"rich",
]
///
import requests
from rich import print
response = requests.get("https://api.github.com/repos/astral-sh/uv")
data = response.json()
print(f"[bold]UV Stars:[/bold] {data['stargazerscount']}")
Run without manual installation:
uv run script.py
UV will automatically create a temporary environment with the required dependencies.
Python Version Management
UV can manage Python installations without requiring pyenv:
# List available Python versions
uv python list
Install a specific Python version
uv python install 3.12
Install multiple versions
uv python install 3.11 3.12 3.13
Use a specific version for the project
uv python pin 3.12
Find the path of an installed Python
uv python find 3.12
Setting Python Version per Project
# Pin Python version for this project
uv python pin 3.11
.python-version file will be created/updated
cat .python-version
Output: 3.11
Tool Management with uv tool
UV can install and run Python CLI tools globally:
# Install a tool globally
uv tool install ruff
uv tool install black
uv tool install httpie
Run a tool without installing (like npx)
uvx ruff check .
uvx black --check .
List installed tools
uv tool list
Update a tool
uv tool upgrade ruff
Uninstall a tool
uv tool uninstall ruff
uvx is a shortcut for uv tool run. It is useful for running tools once without permanently installing them:
# Run a specific version
uvx ruff@0.3.0 check .
Run from a different package
uvx --from httpie http GET https://api.github.com
Building and Publishing Packages
Building a Package
# Build source distribution and wheel
uv build
Results will be in the dist/ folder
ls dist/
mypackage-0.1.0.tar.gz
mypackage-0.1.0-py3-none-any.whl
Publishing to PyPI
# Publish to PyPI
uv publish
Publish to TestPyPI
uv publish --index-url https://test.pypi.org/legacy/
Publish with token
uv publish --token pypi-xxxxx
Configuring pyproject.toml for Publishing
[project]
name = "my-package"
version = "0.1.0"
description = "My awesome package"
readme = "README.md"
license = { text = "MIT" }
authors = [
{ name = "Ruby Abdullah", email = "ruby@example.com" },
]
requires-python = ">=3.11"
dependencies = [
"requests>=2.28",
]
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
[project.urls]
Homepage = "https://github.com/user/my-package"
Migration from pip/Poetry
Migrating from pip + requirements.txt
# Create a new UV project
uv init my-project
cd my-project
Import from requirements.txt
uv add $(cat requirements.txt | grep -v '^#' | grep -v '^