Carles Andres' avatarHomeBlogReference
Back to blog

Load environment variables automatically

Updated on 8/10/2025

Dir env

I have recently discovered direnv, a tool that allows you to load environment variables automatically based on the current directory.

This is the TL;DR of how to set it up.

Installation

bash
brew install direnv

Add this to your ~/.zshrc:

bash

... all your other content

eval "$(direnv hook zsh)"

Configure your project

In your project, create a .envrc file and make sure it's ignored by git. The file needs to follow the right format:

export MY_FOO_VAR="something"export MY_OTHER_VAR=123

You could use other .env formats but it will probably require other tools to make their content available to direnv.

Give permissions

In your project's folder run:

bash
direnv allow

Load the vars

In your project's folder run:

bash
direnv reload