direnv installieren: .envrc-Grundlagen, allow und Env-Variablen automatisch laden

direnv, .envrc, allow — Env pro Verzeichnis.

Jedes Repo braucht eigene Keys und PATH-Anpassungen — statt jedes .env manuell zu sourcen, liest direnv .envrc. Dieser Leitfaden: 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. Installation macOS/Linux, Shell-Hook, .envrc-Grundlagen, direnv allow, automatisches Laden/Entladen für hereink.

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.

installbrew · apt · nix
config.envrc · allow
behaviorcd in · cd out

Was direnv löst

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 at direnv.net. Run direnv version after install.

direnv installieren

  1. 1
    macOS (Homebrew)

    brew install direnv. Verify with which direnv and direnv version.

  2. 2
    Linux (distro packages)

    Debian/Ubuntu: sudo apt update && sudo apt install direnv. Fedora: sudo dnf install direnv. Arch: sudo pacman -S direnv.

  3. 3
    Shell hook (required)

    Add to ~/.zshrc or ~/.bashrc: eval "$(direnv hook zsh)" (use bash for bash). Open a new terminal or source the file.

  4. 4
    fish

    In ~/.config/fish/config.fish: direnv hook fish | source. See official docs for other shells.

.envrc-Grundlagen

.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_exists

Do not commit real tokens. Teams can commit non-secret defaults in .envrc and keep secrets in local .env loaded via dotenv_if_exists.

Im Projektverzeichnis allow

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_BASE

direnv 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.

Automatisch laden und entladen

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 empty

Nested 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 manuelles source .env

ApproachWhen it loadsOn leave
source .envManual every timeVars stay in shell
direnv + .envrcOn cdAuto unload
IDE injectionOpen projectDepends on IDE
Passt
Switching repos, per-project PATH, staging vs prod endpoints tied to folders.
Passt nicht
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.

Nach dem Env-Setup das System skizzieren

Once staging endpoints make sense in the terminal, service boundaries still belong on a shared canvas. See Raum erstellen.

Raum starten