Of course! You can add a dependency to a specific group (like dev, test, or docs) using the --group or -G flag with uv add.
The Short Answer
Use the --group or -G flag. For the dev group, you would do:
bash
uv add ruff --group dev
Or, using the short flag:
bash
uv add ruff -G dev
Detailed Explanation & Example
Let's walk through a full example.
1. The Command
Imagine you want to add ruff (a linter) and black (a formatter) as development-only dependencies.
You can add them one by one:
bash
uv add ruff --group dev
uv add black --group dev
Or add them both at once:
bash
uv add ruff black --group dev
2. What Happens in pyproject.toml
When you use the --group flag, uv doesn't create a proprietary [tool.uv.group.dev] section. Instead, it correctly uses the standard PEP 621 [project.optional-dependencies] table. This is a major advantage, as it makes your project compatible with any other modern Python packaging tool (like pip, build, hatch, etc.).
A "group" in uv is an "extra" in standard Python packaging terminology.
Before running the command, your pyproject.toml might look like this:
toml
[project]
name = "my-cool-project"
version = "0.1.0"
dependencies = [
"requests",
]
After running uv add ruff black --group dev, uv will modify it to look like this:
toml
[project]
name = "my-cool-project"
version = "0.1.0"
dependencies = [
"requests",
]
# This section is added or updated by uv
[project.optional-dependencies]
dev = [
"ruff",
"black",
]
3. Common Groups
You can name your groups whatever you like. Common conventions are:
dev: For development tools like linters, formatters, and pre-commit hooks.
bash
uv add ruff black mypy -G dev
test: For testing frameworks and plugins.
bash
uv add pytest pytest-cov -G test
docs: For documentation generators.
bash
uv add sphinx furo -G docs
How to Install Dependencies from a Group
Adding the dependency to pyproject.toml is only the first step. To install the dependencies for a specific group into your virtual environment, you use uv pip install or uv sync.
Using uv pip install
This is the standard way to install project "extras".
bash
# Install the project itself and the 'dev' dependencies
uv pip install -e ".[dev]"
# Install the project and both 'dev' and 'test' dependencies
uv pip install -e ".[dev,test]"
-e installs your project in "editable" mode.
.[dev] is the standard syntax for installing an extra named dev.
Using uv sync (Recommended for Reproducibility)
The uv sync command is great for ensuring your environment exactly matches your pyproject.toml (or uv.lock file if you use one).
bash
# Sync the environment with main dependencies and the 'dev' group
uv sync --extra dev
# Sync with multiple groups
uv sync --extra dev --extra test
# A convenient way to install ALL optional dependencies
uv sync --all-extras