Sub topic 2
Installing and invoking Ruff
This chapter assumes you can use a terminal, create and activate a virtual environment, install packages with pip, and read a simple pyproject.toml. It covers the ways to install Ruff, the two commands you will use every day, and the details of invocation that cause most first-week confusion: which files get scanned, what the exit codes mean, and how Ruff finds its configuration.
What you are installing
Ruff is a single compiled executable, written in Rust. It does not need a Python interpreter to run. The PyPI package ruff is a wheel that contains this binary, so pip install ruff places an executable named ruff on your path. It does not install a Python library you would import.
Two subcommands cover most of the work:
ruff checkruns the linter. It reports rule violations and can fix some of them.ruff formatruns the formatter. It rewrites files into a consistent layout.
The linter and the formatter are separate commands. Running one does not run the other.
Installation options
Into a virtual environment
For a project, install Ruff into the project's virtual environment. This pins the version for everyone who uses that environment.
python -m venv .venv
source .venv/bin/activate
pip install ruff
ruff --versionOn Windows, activate with .venv\Scripts\activate instead of the source line.
As a standalone tool
If you want Ruff available everywhere and not tied to one project, install it as an isolated tool. Both pipx and uv support this.
pipx install ruff
# or
uv tool install ruffYou can also run it without installing it permanently:
uvx ruff check .Choosing between them
| Method | Version is pinned per project | Available outside the project | Typical use |
|---|---|---|---|
| `pip install ruff` in a venv | Yes, if you record it in your dependencies | No | Team projects, CI |
| `pipx install ruff` or `uv tool install ruff` | No | Yes | Personal use, quick checks on any folder |
| `uvx ruff` | No, unless you pass a version | Yes | One-off runs |
Pin the version for team projects. Ruff adds and changes rules often, and an unpinned upgrade can make a clean project suddenly report new violations. Put the version in your development dependencies, for example ruff==0.6.9 (use the version you actually tested with).
Verifying the install
ruff --version
ruff --help
ruff check --help
ruff format --helpruff --help lists the subcommands. Each subcommand has its own help with all of its flags. Ruff also has a built-in reference for every rule, which you will use often:
ruff rule F401
ruff linterruff rule F401 prints the documentation for one rule: what it checks, why it matters, and whether it has a fix. ruff linter lists the rule families that your Ruff version supports.
The core commands
This is the set of commands used in the rest of the tutorial:
pip install ruff
ruff check .
ruff check --fix .
ruff format .
ruff format --check .
ruff rule F401In order, they do the following:
ruff check .lints the current directory and prints violations. It changes nothing.ruff check --fix .lints and applies the fixes that Ruff classifies as safe.ruff format .rewrites files in place into the formatted style.ruff format --check .reports which files would change, without changing them. This is the form used in CI.ruff rule F401prints the documentation for rule F401.
A first run
Save this file as example.py. It contains three deliberate problems.
import os
import sys
def total(items):
unused = 1
result = 0
for i in items:
result += i
if result == None:
return 0
return result
print(sys.argv)Now run the linter on it:
ruff check example.pyWith the default rule set, Ruff reports three violations:
F401:osis imported but never used.F841: the local variableunusedis assigned but never used.E711: the comparisonresult == Noneshould useis None.
Each report shows the file, line and column, the rule code, a message, and a code excerpt. Ruff also prints a hint such as [*] 1 fixable with the --fix option when some of the violations can be fixed automatically. The [*] marker beside a rule means that rule has an available fix.
You will see more about fixes and their safety levels in a later chapter. For now, note that ruff check on its own is read-only.
How invocation works
Which files are scanned
When you pass a directory, Ruff walks it recursively and checks Python files (.py, .pyi, and Jupyter notebooks in recent versions). When you pass a file, Ruff checks that file. With no path at all, Ruff uses the current directory.
Ruff skips files and folders it should not touch:
- Anything matched by
.gitignore,.ignoreand similar ignore files, as long as the project is inside a Git repository (this is therespect-gitignoresetting, on by default). - A built-in list of common excluded folders, such as
.git,.venv,venv,node_modules,__pycache__,build,distand.tox. - Anything you add to the
excludeorextend-excludesettings.
Exclusions have one important exception. If you pass a path explicitly on the command line, Ruff may still skip it if it matches an exclusion. When you want to force a check on an excluded file, add --force-exclude in the opposite direction: that flag makes Ruff apply exclusions even to explicitly named files. Tools such as pre-commit pass file names explicitly, so --force-exclude keeps your exclude rules in effect there.
Where configuration comes from
For each file, Ruff looks for the nearest configuration file by walking up from the file's directory. It recognizes pyproject.toml (only if it has a [tool.ruff] section), ruff.toml and .ruff.toml. The closest one applies to that file. Ruff does not merge it with parent configuration files unless you ask it to with extend.
Two flags change this behavior:
--config path/to/ruff.tomluses that file for everything.--isolatedignores all configuration files and uses defaults.
--isolated is useful for checking whether a surprising result comes from your configuration.
Default rules
Without any configuration, ruff check runs a small set of rules: E4, E7, E9 and F. These are pycodestyle errors that are unlikely to conflict with a formatter, plus the Pyflakes rules. That is why the example above reports F401, F841 and E711 without any setup. Choosing more rules is covered in a later chapter.
Exit codes and output formats
Exit codes decide whether a CI step passes. They differ between the two commands.
| Command | Exit 0 | Exit 1 | Exit 2 |
|---|---|---|---|
| `ruff check` | No violations (or all were fixed) | Violations remain | Ruff failed, for example on a bad config |
| `ruff format` | Ran successfully | Not used | Ruff failed |
| `ruff format --check` | All files already formatted | Some files would change | Ruff failed |
Two details are easy to miss. First, ruff check --fix exits with 1 if violations remain after fixing, so a fix run can still fail a script. Second, ruff format without --check exits 0 even when it changed files. If you want a failing exit code for unformatted files, use --check.
To see what the formatter would do without writing anything, add --diff:
ruff format --diff .This prints a unified diff of the changes, and it also exits with 1 if any file would change.
You can select the output format of the linter with --output-format. Common values are concise, full, json, github and grouped. The github format produces annotations that GitHub Actions shows on the pull request.
ruff check --output-format=concise .
ruff check --statistics .--statistics prints a count of violations per rule, which is a good way to see where to start when you first run Ruff on an existing codebase.
Common mistakes
- Expecting
ruff checkto format code. It only lints. Runruff formatfor layout. - Expecting
ruff formatto find unused imports. It only rewrites layout. It never removes imports or changes names. - Using a different version in each place. Pin one version and reuse it in the editor, pre-commit and CI.
- Running
--fixon a dirty working tree. Commit or stash first, so you can review exactly what Ruff changed withgit diff. - Reading
pyproject.tomlwithout a[tool.ruff]section as configuration. Ruff ignores apyproject.tomlthat has no such section and continues walking up the directory tree. - Forgetting that exclusions apply differently for explicit paths. Pass
--force-excludewhen a tool hands Ruff a list of files.
How the pieces fit together
The diagram shows what happens when you run ruff check on a directory.
Practice
Exercise
You run ruff format . in a CI step and it prints "2 files reformatted", but the step passes with exit code 0 and the unformatted code is never flagged. What should the step run instead, and what exit code will it return when files need changes?
Show answer
Run ruff format --check . (optionally with --diff to show the changes). It exits with code 1 when any file would be reformatted and 0 when all files are already formatted. Plain ruff format rewrites files inside the CI workspace and exits 0, so it never fails the build.
Exercise
A teammate says ruff check . is clean on their machine, but yours reports new violations. Both of you use the same pyproject.toml. Name two things you would compare first, and one command that helps rule out your configuration.
Show answer
Compare the installed Ruff versions with ruff --version, since newer versions add or change rules. Also compare which configuration file each of you is actually using, because a nearer ruff.toml or a --config flag can override pyproject.toml. Running ruff check --isolated . ignores all configuration files and uses the defaults, which shows whether the difference comes from configuration.
Exercise
Create example.py from this chapter. Which command lists how many violations of each rule it contains, and what are the three rule codes you should see?
Show answer
ruff check --statistics example.py prints a count per rule. The three codes are F401 (unused import os), F841 (unused local variable unused) and E711 (comparison to None with ==).