How to install direnv: .envrc basics, direnv allow, and auto-load env vars
Install direnv, author .envrc, allow once, auto-load per directory.
Every repo wants its own API keys, database URLs, and PATH tweaks, but manually sourceing .env files gets old. direnv reads .envrc in a project folder: cd in to load environment variables, cd out to unload. This guide covers install on macOS/Linux, shell hooks, .envrc basics, running direnv allow in the project directory, and automatic load/unload for hereink readers who juggle many repos in the terminal.
The query is how to install direnv and write .envrc to load project env vars automatically. You should finish with direnv installed, a first .envrc, direnv allow at the repo root, and variables appearing when you enter the directory and disappearing when you leave.
What direnv fixes
Per-directory environments
Global export pollutes every shell. Variables that belong to one repo should mount with that directory. direnv injects on cd and rolls back on exit, which cuts down wrong-script mistakes from stale PATH.
Works with .env files
.envrc can dotenv an existing .env or use plain export. Keep secrets in .gitignore; commit a safe .envrc template without live tokens.
Upstream:
github.com/direnv/direnv, docs atdirenv.net. Rundirenv versionafter install.
Install direnv
- 1macOS (Homebrew)
brew install direnv. Verify withwhich direnvanddirenv version. - 2Linux (distro packages)
Debian/Ubuntu:
sudo apt update && sudo apt install direnv. Fedora:sudo dnf install direnv. Arch:sudo pacman -S direnv. - 3Shell hook (required)
Add to
~/.zshrcor~/.bashrc:eval "$(direnv hook zsh)"(usebashfor bash). Open a new terminal orsourcethe file. - 4fish
In
~/.config/fish/config.fish:direnv hook fish | source. See official docs for other shells.
.envrc basics
.envrc is a shell snippet at the project root (or a subdirectory). Common patterns:
export API_BASE=https://api.staging.example.com
export NODE_ENV=development
PATH_add ./bin
dotenv_if_existsDo not commit real tokens. Teams can commit non-secret defaults in .envrc and keep secrets in local .env loaded via dotenv_if_exists.
Allow in the project directory
The first time you enter a folder with .envrc, direnv blocks execution until you explicitly trust the file:
cd ~/projects/my-app
# direnv: error .envrc is blocked. Run `direnv allow` to approve its content.
direnv allow
# direnv: loading ~/projects/my-app/.envrc
echo $API_BASEdirenv allow stores a hash whitelist under .direnv/. After you edit .envrc, run direnv allow again. That is intentional: a cloned repo cannot silently change your environment.
Automatic load and unload
With the hook installed, you do not need manual source each time:
cd ~/projects/my-app # loads .envrc
env | grep API_BASE
cd ~ # unloads project vars
env | grep API_BASE # should be emptyNested directories: a child .envrc applies when you enter that path. Monorepos often use a root .envrc with PATH_add for shared tooling—see source_up in direnv docs.
vs manual source .env
| Approach | When it loads | On leave |
|---|---|---|
source .env | Manual every time | Vars stay in shell |
direnv + .envrc | On cd | Auto unload |
| IDE injection | Open project | Depends on IDE |
- Fits
- Switching repos, per-project
PATH, staging vs prod endpoints tied to folders. - Does not fit
- One global export forever, or CI images—set env in Dockerfile or workflow instead.
.envrc changed but nothing updates?
Run direnv allow or direnv reload. direnv compares hashes so silent tampering cannot pass.
Conflict with asdf or mise?
They can coexist: version managers live in the shell; direnv adds per-directory PATH and vars. Many teams call tool-specific direnv integrations from .envrc—check each tool’s docs.
Windows support?
Official focus is Unix shells. Use WSL and follow Linux steps. Native PowerShell support is limited—see current direnv documentation.
After env is aligned, sketch the system
Once staging endpoints make sense in the terminal, service boundaries still belong on a shared canvas. See how to create a space.