Skip to content

Instantly share code, notes, and snippets.

@troyharvey
Created June 28, 2022 23:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save troyharvey/cc77ea6af25d94e2c1fd293046c8262b to your computer and use it in GitHub Desktop.
Save troyharvey/cc77ea6af25d94e2c1fd293046c8262b to your computer and use it in GitHub Desktop.
Automatically load .env files

Automatically load .env files

I use .env environment variable files in every project. Docker Compose supports .env files. And now direnv supports them too. direnv is a cli tool that detects a .envrc or .env file and runs source .env automatically for you when you cd into a project directory.

  1. Install Homebrew.

  2. Install direnv.

     brew install direnv
    
  3. Follow these instructions to add the direnv hook to your shell.

  4. Create a direnv config directory.

     mkdir -p ~/.config/direnv
    
  5. Copy this snippet telling direnv to load .env files in addition to the default .envrc files.

     [global]
    
     load_dotenv=true
    
  6. Create a direnv config file.

     pbpaste > ~/.config/direnv/direnv.toml
    
  7. Restart your Terminal, and .env files will load automatically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment