UV in 30 mins
TL;DR uv is a very fast Python package manager written in Rust that replaces
pip, pip-tools, and virtualenv with a single tool that is 10-100x faster.
Introduction#
-
This tutorial explores why
uvis becoming the go-to package manager for Python developers and how to use it effectively -
Python's packaging ecosystem challenges:
- Long criticized for complexity
- Slow performance
-
uvaddresses these pain points:- Unified solution
- Fast performance
- Modern dependency management
- Efficient virtual environment handling
Why uv?#
- Speed: Written in Rust with highly optimized algorithms,
uvis 10-100x faster thanpipfor dependency resolution and installation - All-in-one tool: Replaces multiple tools (
pip,pip-tools,virtualenv,pyenv) with a single unified interface - Drop-in replacement: Compatible with
pipandrequirements.txtformat, making migration seamless - Better dependency resolution: Uses a modern SAT solver for more reliable dependency resolution
- Cross-platform: Works seamlessly on Linux, macOS, and Windows
- Disk space efficient: Smart caching reduces redundant downloads and storage
-
Project management: Built-in support for creating and managing Python projects
-
Here's a speed comparison for installing packages:
Tool Time (seconds) Notes pip install45.2 Standard installation poetry install38.7 With lock file uv pip install2.1 First install with caching uv pip install0.3 Subsequent install (cached)
Installation#
-
On macOS and Linux using the official installer:
-
Using Homebrew on macOS:
-
Using
pip(ironically):
Verifying installation#
- Verify the installation:
Basic Usage#
Creating a Virtual Environment#
-
Create a virtual environment:
-
This creates a
.venvdirectory with a fresh Python environment -
Activate the environment:
-
Create a virtual environment with a specific Python version in one line:
Installing Packages#
- Install a package using
uv pip: -
Create venv and activate
-
Check that
requestsis not installed -
Install
requests -
Check whether
requestsis installed -
Install multiple packages:
-
Install from a requirements file:
-
Install with specific version constraints:
-
Install from a requirements file with pinned versions:
Project Management#
Initializing a New Project#
-
Create a new project with scaffolding:
-
This creates a new project with:
pyproject.toml: Project configuration and dependenciesREADME.md: Project documentationsrc/myproject/: Source code directory.python-version: Python version specification
Dev vs Prod Dependencies#
-
Production Dependencies = required to run your application in production
- In
pyproject.toml:
- In
-
Development Dependencies = needed only for development (testing, linting, formatting)
- Not required in production
- Example:
pytest - In
pyproject.toml:
Working with pyproject.toml#
-
Example
pyproject.tomlconfiguration: -
Add a dependency to your project:
-
Add a development dependency:
-
Remove a dependency:
Syncing Dependencies#
-
Sync your environment with project dependencies:
- This installs all dependencies specified in
pyproject.tomland creates or updates the lock file
- This installs all dependencies specified in
Dependency Locking#
Creating Lock Files#
-
Generate a lock file for reproducible builds:
- This creates
uv.lockwith exact versions of all dependencies and their transitive dependencies
- This creates
-
Install from lock file:
- The
--frozenflag ensures no changes are made to the lock file
- The
-
Update dependencies to latest compatible versions:
-
Update a specific package:
Advanced Features#
Python Version Management#
-
List available Python versions:
-
Install a specific Python version:
-
Use a specific Python version for a project:
- This creates a
.python-versionfile in your project
- This creates a
Running Commands#
-
Run Python scripts without activating the environment:
-
Run a command with dependencies:
- This temporarily installs
requestsand runs the command
- This temporarily installs
Tool Management#
-
Install and run Python tools globally:
-
Run a tool without installing:
- The
uvxcommand (oruv tool run) is similar tonpxfor Node.js
- The
-
List installed tools:
Caching#
-
Show cache information:
-
Clean the cache:
-
Clean cache for a specific package:
Practical Examples#
Migrating from pip#
-
If you have an existing project with
requirements.txt: -
Generate a lock file from requirements:
Creating a requirements.txt#
-
Freeze current environment to requirements:
Working with Multiple Environments#
-
Create separate environments for different purposes:
Building Distributions#
-
Build a package for distribution:
- This creates wheel and source distributions in the
dist/directory
- This creates wheel and source distributions in the
Installing from Local Path#
-
Install a package in development mode:
-
Install from a local wheel:
Configuration#
Project Configuration#
-
Configure
uvbehavior inpyproject.toml:
Global Configuration#
-
Create a global config at
~/.config/uv/uv.toml:
Environment Variables#
-
Control
uvbehavior with environment variables:
Tips and Tricks#
Offline Installation#
-
Cache packages for offline use:
-
Install from cached packages:
Upgrading All Packages#
-
Upgrade all dependencies to latest compatible versions:
Resolving Conflicts#
-
uvprovides detailed error messages for dependency conflicts:- Shows exactly which constraints conflict
- Suggests resolutions
Pre-release Versions#
-
Install pre-release versions:
Platform-Specific Dependencies#
-
Specify platform-specific dependencies in
pyproject.toml:
Integration with Development Tools#
Docker Integration#
-
Example Dockerfile using
uv:
CI/CD Integration#
-
Example GitHub Actions workflow:
Pre-commit Hooks#
-
Example pre-commit configuration:
Common Gotchas#
Python Version Not Found#
-
If
uvcannot find a Python version:- This installs the required Python version
Lock File Out of Sync#
-
If dependencies change, regenerate the lock file:
Cache Issues#
-
If you encounter unexpected behavior, clear the cache:
Private Package Indexes#
-
Configure authentication for private PyPI mirrors:
- Alternative: use keyring integration for secure credential storage
Comparison with Other Tools#
uv vs pip:
| Feature | uv | pip |
|---|---|---|
| Speed | +++ | + |
| Dependency resolver | Modern | Legacy |
| Lock files | Built-in | Requires pip-tools |
| Virtual environments | Built-in | Requires virtualenv |
| Project management | Yes | No |
uv vs poetry:
| Feature | uv | poetry |
|---|---|---|
| Speed | +++ | ++ |
| Installation time | Seconds | Minutes |
| Learning curve | Low | Medium |
| Plugin system | Limited | Rich |
uv vs pip-tools:
| Feature | uv | pip-tools |
|---|---|---|
| Speed | +++ | + |
| Ease of use | Higher | Medium |
| Compatibility | Excellent | Excellent |
| Maintenance | Active | Active |