How to use direnv to load and unload environment variables

I’ve discovered the direnv tool. It allows load and unload environment variables depending on the directory.

GitHub repo

Installation

As few other tools, the easiest way is using Homebrew:

brew install direnv

Configuration

Once it’s installed, we need to add the following into ourt ~/.zshrc file:

# load direnv
eval "$(direnv hook zsh)"

In the directory, we have to create the .envrc file, where we will store all the env variables.

eg .envrc:

 export kittens="awesome"
 export BASE_API_URL="https://xxxx.xxx"

Note: Remember include the .envrc in the .gitignore

Commands

  • To allow (load) the new env variables:
direnv allow
  • In case you want to unload/deny these env variables:
direnv deny