How to Automatically Load Project‑Specific Environment Variables with direnv .envrc

User avatar placeholder
Written by Tamzid Ahmed

September 12, 2026

Tired of manually exporting API keys, database URLs, or feature flags each time you switch projects? By using direnv .envrc to automatically load project‑specific environment variables, you keep secrets out of your shell history and let your dev environment ready itself the moment you cd into a folder.

Why project‑specific environment variables matter

Environment variables are the de‑facto way to configure applications without hard‑coding values. When each project needs its own set of vars—different database credentials, third‑party API keys, or feature flags—manually exporting them leads to mistakes, duplicated effort, and potential leaks. direnv solves this by loading the right set automatically whenever you enter a project directory.

How direnv .envrc automatically loads environment variables

Direnv is a shell extension that watches for a file named .envrc in the current directory. When you change directories, direnv evaluates the .envrc file, exports any variables it defines, and removes them when you leave. This creates a clean, per‑project environment without cluttering your global shell.

Understanding the shell hook

After installation, direnv adds a hook to your shell’s PROMPT_COMMAND (bash) or precmd (zsh/fish). The hook runs direnv export each time the prompt is displayed, checking for a new or modified .envrc. If the file exists and is allowed, direnv loads its contents into the environment.

Installing direnv

  1. On macOS with Homebrew: brew install direnv
  2. On Ubuntu/Debian: sudo apt-get install direnv
  3. On Fedora: sudo dnf install direnv
  4. Verify installation: direnv version
  5. Hook direnv into your shell:
    • Bash: add eval "$(direnv hook bash)" to ~/.bashrc
    • Zsh: add eval "$(direnv hook zsh)" to ~/.zshrc
    • Fish: add direnv hook fish | source to ~/.config/fish/config.fish
  6. Reload your shell or open a new terminal.

Creating and using a .envrc file

  1. Navigate to your project root: cd my-project
  2. Create the file: touch .envrc
  3. Add variable exports, for example:
    export DATABASE_URL='postgres://localhost/mydb'
    export API_KEY='s3cr3t'
    export NODE_ENV='development'
    
  4. Save the file.
  5. The first time you enter the directory, direnv will warn that the .envrc is blocked. Allow it with:
    direnv allow
  6. Direnv now automatically loads the variables on each cd into the folder and unloads them when you leave.

Security considerations and best practices

  • Never commit .envrc to version control; add it to .gitignore.
  • Use direnv’s built‑in dotenv stdlib to load a separate .env file that you do keep ignored.
  • Restrict direnv allow to trusted projects; review any .envrc before allowing.
  • Consider using direnv edit to open the file in your editor safely.
  • Rotate secrets regularly and audit which projects have active envrcs.

Integrating direnv with your shell (bash, zsh, fish)

Once the hook is added (see installation step), direnv works transparently. However, a few tips improve the experience:

  • Enable lazy loading for large projects by adding export DIRENV_LOG_FORMAT= to suppress verbose logs.
  • Use direnv status to see which variables are currently loaded.
  • If you use a version manager like asdf or nvm, place the direnv hook after those initializations so PATH modifications apply correctly.
  • For CI environments, you can source $(direnv export bash) in your CI script to replicate the local environment.

Advanced tips: loading .env files, using stdlib, and more

  • Load a .env file with the stdlib: add source_up or dotenv to your .envrc:

    # .envrc
    dotenv
    export DATABASE_URL='postgres://localhost/mydb'
  • Use source_up to inherit variables from a parent directory’s .envrc.
  • Combine with layout functions (e.g., layout node) to automatically run npm install or pip install -r requirements.txt when entering a project.
  • Create project‑specific aliases or functions inside .envrc to streamline repetitive tasks.
  • Direnv also supports export and unset for temporary overrides.

Conclusion

By leveraging direnv .envrc to automatically load project‑specific environment variables, you eliminate manual export steps, reduce the risk of leaking secrets, and keep each project’s configuration isolated and reproducible. Start today: install direnv, add a simple .envrc, run direnv allow, and watch your workflow become seamless.

Leave a Comment