Sub topic 2
Installing and invoking Ruff
This chapter covers how to install Ruff and how to run its two main commands. It assumes you can use a terminal, can create and activate a Python virtual environment, and can install packages with pip. Rules, configuration, and automatic fixes come in later chapters.
Installing Ruff
Ruff is a single executable that ships as a Python package, so pip can install it. Install it into the virtual environment of the project you want to check:
python -m venv .venv
source .venv/bin/activate
pip install ruff
ruff --versionOn Windows, activate the environment with .venv\Scripts\activate instead. The ruff --version command prints the installed version. If the shell says the command is not found, the environment is probably not active.
Other installers work too, for example pipx install ruff or uv tool install ruff. A tool installed this way is available everywhere and is not pinned to one project. For team projects, prefer installing Ruff as a development dependency so that everyone, including CI, uses the same version. Rule sets and formatting can change between releases, so mismatched versions produce different results.
The two commands
Ruff has two commands that do different jobs.
| Command | Purpose | Changes files? |
|---|---|---|
| `ruff check` | Lints: reports rule violations | Only with `--fix` |
| `ruff format` | Formats: rewrites code layout | Yes, unless `--check` is given |
The commands are separate. ruff check does not reformat code, and ruff format does not report lint violations.
Invoking Ruff
Each command takes zero or more paths. With no path, Ruff uses the current directory. Paths can be files or directories:
ruff check .
ruff check src/app.py tests/
ruff check --fix .
ruff format .
ruff format --check .
ruff rule F401ruff check .lints every Python file under the current directory and prints violations.ruff check --fix .also applies fixes that Ruff considers safe.ruff format .rewrites files in place.ruff format --check .changes nothing and reports which files would be reformatted. This is the form to use in CI.ruff rule F401prints the documentation for one rule.
Ruff skips directories such as .git, .venv, node_modules, and __pycache__ by default, so running it on . does not lint your installed packages.
Exit codes
Scripts and CI depend on exit codes, so it helps to know them.
ruff checkexits with0when it finds no violations, and with1when it finds any.ruff format --checkexits with1when at least one file would change.- An exit code of
2means Ruff itself failed, for instance because of an invalid configuration file.
You can see this by running Ruff on a file with problems. Save this as sample.py:
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)Then run:
ruff check sample.py
echo $?With Ruff's default rules, the output lists three violations: F401 for the unused os import, F841 for the unused local variable unused, and E711 for comparing to None with ==. The echoed exit code is 1. Each report shows the file, line, column, rule code, and a short message.
Other ways to run it
If you prefer not to rely on the shell finding the executable, you can run it through Python with python -m ruff check .. This always uses the Ruff installed in that interpreter's environment. With uv, uvx ruff check . runs Ruff without a permanent install, which is handy for a one-off check.
Exercise
You want a CI step that fails when any file is not formatted, without modifying any files. Which command do you use, and what exit code signals failure?
Show answer
Use ruff format --check .. It exits with code 1 when at least one file would be reformatted, and it leaves all files unchanged.
Exercise
You run ruff check . and it exits with code 2. What does that usually mean, as opposed to code 1?
Show answer
Code 2 means Ruff itself failed, for example because of an invalid configuration file or a bad argument. Code 1 means Ruff ran normally and found lint violations.