Sub topic 2
Setting up a DVC project
This chapter covers installing DVC, running dvc init inside a Git repository, and what the command creates. It also covers how DVC configuration is layered, and the errors you are most likely to hit.
You should already be comfortable with Git commits, branches, and tags, with the command line, and with reading YAML. The chapter uses Python and pip for installation only. No training code runs here.
What `dvc init` does
DVC does not replace Git. It sits next to it. Git keeps versioning your code and small text files. DVC adds a small amount of project metadata and a local cache directory. Later chapters use that cache to store large files outside Git.
dvc init creates the metadata and the cache location. It does not track any data yet. That happens in the next chapter with dvc add.
The usual order is:
- Create or enter a Git repository.
- Install DVC.
- Run
dvc init. - Commit the files DVC created.
Installing DVC
DVC is a Python package. Install it into a virtual environment so the version is pinned per project.
python -m venv .venv
source .venv/bin/activate
pip install dvc
dvc --versionRemote storage backends need extra packages. These are called extras. You can install the one you need now or later:
pip install "dvc[s3]" # Amazon S3
pip install "dvc[gs]" # Google Cloud Storage
pip install "dvc[azure]" # Azure Blob Storage
pip install "dvc[ssh]" # SSH/SFTP serversThe remote chapter uses S3, so dvc[s3] is the one you will want. Pin the DVC version in your requirements.txt or lock file. Teammates and CI should run the same major version, because newer versions can write metadata that older ones cannot read.
Running `dvc init`
Start from a Git repository:
mkdir churn-model
cd churn-model
git init
dvc initDVC prints a short message ending with a hint to commit the changes to Git. It has also staged the new files for you, so you can commit right away:
git status --short
git commit -m "Initialize DVC"git status --short should list the staged files described in the next section. If dvc init ran inside a Git repository, those files are already staged. You do not need to run git add yourself.
What gets created
dvc init creates a .dvc directory and one file in the project root.
| Path | Committed to Git? | Purpose |
|---|---|---|
| `.dvc/config` | Yes | Project-level configuration, such as remote storage settings |
| `.dvc/.gitignore` | Yes | Tells Git to ignore the local-only entries below |
| `.dvc/cache/` | No | The local cache where DVC stores file contents |
| `.dvc/config.local` | No | Machine-specific settings and secrets, created when you need it |
| `.dvc/tmp/` | No | Temporary working files that DVC manages |
| `.dvcignore` | Yes | Patterns DVC should skip, in the same syntax as `.gitignore` |
The split matters. Everything in Git is shared by the whole team. Everything under .dvc/cache, .dvc/tmp, and .dvc/config.local stays on your machine. The generated .dvc/.gitignore already excludes these, so you cannot commit them by accident.
A freshly initialized .dvc/config is empty. Run cat .dvc/config to confirm. Remote definitions and other project options will be added to it as you work, so it is the file teammates need in order to use the same storage.
The `.dvcignore` file
.dvcignore works like .gitignore, but it controls what DVC scans, not what Git tracks. It is useful when a tracked directory contains files you do not want DVC to hash, such as editor swap files or .ipynb_checkpoints. Add one pattern per line:
.ipynb_checkpoints/
*.tmp
__pycache__/Ignored files do not take part in change detection. This speeds up dvc status on large directories and avoids false "changed" reports from temporary files.
Configuration levels
DVC reads settings from several layers, similar to Git. The dvc config command writes to one layer, selected by a flag.
| Flag | File | Scope |
|---|---|---|
| (none) | `.dvc/config` | The project, shared through Git |
| `--local` | `.dvc/config.local` | This project on this machine, never committed |
| `--global` | A per-user file in your user config directory | All projects for your user |
| `--system` | A system-wide file | All users on the machine |
More specific layers override less specific ones. Local beats project, project beats global, and global beats system.
Use this rule when deciding where a setting goes:
- Settings the whole team needs, such as a remote URL, go in the project config.
- Credentials and personal paths go in
--localor--global. - Never put access keys in
.dvc/config. That file is committed, and anything in it is visible to everyone with repository access.
For example, to turn off anonymous usage analytics for every project you work on:
dvc config --global core.analytics falseTo store an S3 profile name for only this machine, you would later run a command like dvc remote modify --local storage profile my-profile. You will see the full remote setup in the remote storage chapter.
Checking the project state
Two commands tell you quickly whether the project is healthy:
dvc doctor
dvc statusdvc doctor prints the DVC version, the platform, the supported remote types, the cache type, and the repository location. Include its output when you file a bug or ask a teammate for help.
dvc status compares the workspace to what DVC tracks. Right after dvc init there is nothing tracked, so it reports that there is nothing to compare. This is expected. It becomes useful once you have added data.
Initializing in less common layouts
The default case is a Git repository with dvc init run at its root. There are two variations.
A subdirectory of a larger repository
In a monorepo, one Git repository holds several projects. Each project can have its own DVC setup if you run dvc init with --subdir inside the project folder:
cd projects/churn-model
dvc init --subdirDVC then creates the .dvc directory inside projects/churn-model, and that folder becomes an independent DVC project. Commands run inside the folder see only its own tracked files and its own cache settings. Without --subdir, DVC refuses to initialize anywhere other than the Git root.
Without Git
You can use DVC without any source control by passing --no-scm:
mkdir scratch
cd scratch
dvc init --no-scmDVC then works, but you lose most of the benefits covered in this tutorial. Pointer files cannot be committed, so you cannot switch between data versions by checking out a Git commit. Treat --no-scm as a way to experiment or test, and not as a way to run a real project.
Common errors
These are the errors you are most likely to see at this stage.
| Message (shortened) | Cause | Fix |
|---|---|---|
| `failed to initiate DVC - ... is not tracked by any supported SCM tool` | The folder is not inside a Git repository | Run `git init` first, or use `--no-scm` if you really want no Git |
| `failed to initiate DVC - '.dvc' exists` | The project was already initialized | Do nothing. Use `-f` only if you want to wipe and recreate the DVC metadata |
| The same SCM error inside a subfolder of a repository | You are in a subdirectory, not the Git root | Run `dvc init` at the root, or add `--subdir` |
| `dvc: command not found` | The virtual environment is not active, or DVC is not installed | Activate the environment and rerun `pip install dvc` |
The -f flag deserves care. Forcing re-initialization overwrites .dvc/config, so any remote definitions in it are lost. Check git diff .dvc/config before you accept the result, and restore the file with Git if needed.
A complete setup script
This script performs everything in the chapter in one pass. It assumes python and git are installed and writes a short .dvcignore as well.
set -e
mkdir churn-model
cd churn-model
git init
python -m venv .venv
source .venv/bin/activate
pip install dvc
dvc init
printf '.ipynb_checkpoints/\n__pycache__/\n' > .dvcignore
dvc config core.analytics false
git add .dvcignore .dvc/config
git commit -m "Initialize DVC"
dvc doctor
dvc statusTwo details are worth noting. The virtual environment directory .venv should be added to your own .gitignore before any broad git add. Here the script adds only named files, so .venv stays out of the commit. And dvc config core.analytics false modifies .dvc/config, so the git add .dvc/config line picks that change up. Without it, the setting would be left uncommitted.
Exercise
You run dvc init in a folder and get the error "is not tracked by any supported SCM tool". Name two ways to resolve it, and say what you give up with the second.
Show answer
Run git init in the folder (or move into an existing Git repository) and then run dvc init. The second way is dvc init --no-scm, which lets DVC work without Git. With it you give up the ability to commit pointer files, so you cannot switch between data versions by checking out Git commits.
Exercise
A teammate needs an S3 access key to push data. Which configuration layer should hold the key, and why not the default one?
Show answer
Use --local (.dvc/config.local) or --global. The default layer is .dvc/config, which is committed to Git, so a key stored there would be visible to everyone with repository access and would remain in the history.
Exercise
After dvc init, you run git status and see staged files. Which entries of the .dvc directory are not among them, and which file in .dvc makes sure they stay out of Git?
Show answer
The cache (.dvc/cache), the temporary directory (.dvc/tmp), and .dvc/config.local are not committed. The file .dvc/.gitignore lists them so Git ignores them.