Install and configure direnv
for environment management
Why we need direnv
When working on multiple projects,
each one often needs its own set of environment variables.
Managing these manually is error-prone and tedious.
direnv
1 automatically loads and unloads environment variables
as you move between different project directories,
making it much easier to manage project-specific configurations.
How to install direnv
brew install direnv
# Ubuntu/Debian
sudo apt-get install direnv
# Fedora
sudo dnf install direnv
Verify that direnv
is installed and available in your PATH:
which direnv
# Should show something like /usr/local/bin/direnv
How to configure direnv
After installation, you need to hook direnv
into your shell.
Add the line to your ~/.bashrc
file:
eval "$(direnv hook bash)"
Then reload your configuration:
source ~/.bashrc
Add the line to your ~/.zshrc
file:
eval "$(direnv hook zsh)"
Then reload your configuration:
source ~/.zshrc
Now direnv
is ready to use.
To verify it's working:
# Create a test directory
mkdir ~/direnv-test
cd ~/direnv-test
# Create a basic .envrc file
echo 'export TEST_VAR="Hello, direnv!"' > .envrc
# Allow the .envrc file
direnv allow .
# Verify the variable is set
echo $TEST_VAR
# Should output: Hello, direnv!
When you leave the directory, direnv
will automatically unload these variables,
keeping your environment clean and organized.
Loading .env
files automatically (direnv >= 2.31.0)
If you want direnv to automatically recognize and load .env
files (in addition to .envrc
),
you can enable this feature in your direnv.toml
configuration file:
- Create or edit the file at
~/.config/direnv/direnv.toml
. - Add the following:
[global]
load_dotenv = true
- With this setting, direnv will look for and load
.env
files on top of.envrc
files. - If both
.envrc
and.env
exist,.envrc
takes precedence. - Requires direnv version 2.31.0 or newer.
- See the official documentation for more details.
Troubleshooting
direnv not loading environment variables
-
Check if direnv is hooked into your shell:
- For bash, your
~/.bashrc
should have:eval "$(direnv hook bash)"
- For zsh, your
~/.zshrc
should have:eval "$(direnv hook zsh)"
- After editing, restart your shell.
- For bash, your
-
.envrc or .env is not allowed:
- When you create or modify a
.envrc
or.env
, direnv will block it for security. - Run:
to allow it.
direnv allow .
- When you create or modify a
-
.env file not loaded:
- Make sure you have
load_dotenv = true
in yourdirenv.toml
(see above). - Or, in older direnv versions, add
dotenv
to your.envrc
:dotenv
- Make sure you have
-
Changes to .envrc or .env not taking effect:
- Run:
direnv reload
- Or simply
cd
out and back into the directory.
- Run:
-
Environment variables not showing up:
- Make sure you are in the directory (or a subdirectory) containing the
.envrc
or.env
and that it is allowed.
- Make sure you are in the directory (or a subdirectory) containing the
-
direnv not installed or not in PATH:
- Run:
which direnv
- If not found, install direnv and ensure it’s in your PATH.
- Run:
-
Shell not supported:
- Check direnv’s documentation for your shell’s setup instructions.
Common error messages
- ".envrc is not allowed":
- Run
direnv allow .
in the directory.
- Run
- "direnv: command not found":
- Install direnv and ensure it’s in your PATH.
- "Environment variable not set":
- Check your
.envrc
or.env
for typos and ensure the file is allowed.
- Check your
-
direnv
is an extension for your shell that loads and unloads environment variables depending on your current directory. Learn more about it at direnv.net. ↩