direnv — automatic environment variable loading per directory
💬 "direnv command"
direnv provides automatic loading of directory-specific environment variables. When you cd into a specific directory, it automatically loads the environment variables from .envrc. When you leave, it automatically unloads them.
It supports automatic loading on directory entry and automatic unloading on exit. This enables project-specific environment variable isolation.
- Concept: Supports automatic loading on directory entry and automatic unloading on exit.
- Key Features: Enables project-specific environment variable isolation.
- Advantages: Supports most shells including bash, zsh, and fish.
- Usage: Deeper shell integration compared to dotenv.
Installation & Download
direnv is installed via a package manager or the official script.
| Environment | Command |
|---|---|
| Ubuntu / Debian | sudo apt install direnv |
| macOS (brew) | brew install direnv |
| Rust cargo | cargo install direnv |
| Official Binary | github.com/direnv/direnv/releases |
| Verify | direnv --version |
Verify the installation with direnv --version.
Frequently Used Options
| Option | Description |
|---|---|
| direnv allow | Adds the current .envrc to the trust list |
| direnv deny | Blocks the current .envrc |
| direnv reload | Reloads the .envrc in the current shell |
| direnv status | Checks the current status and loaded variables |
| direnv edit | Opens .envrc with the EDITOR |
3 Essential Command Combinations
echo 'export API_KEY=abc' > .envrc && direnv allow— Register a new environment variabledirenv status— Check currently loaded variablesdirenv deny— Confirm automatic unloading upon project exit
💬 "Show me an example of using direnv on a real server"
Here is a step-by-step breakdown of a common situation encountered on a live production server.
Step 1 · Shell Hook Setup
Add the direnv hook to your shell.
echo 'eval "$(direnv hook bash)"' >> ~/.bashrc && source ~/.bashrc
Step 2 · Create .envrc
Create the environment variable file.
cd /var/www/api && echo 'export API_KEY=abc' > .envrc && direnv allow
Step 3 · Verify Operation
Confirm the automatic loading and unloading when moving directories.
cd /var/www/api && echo $API_KEY
Actual Execution Screen
This shows the process of loading environment variables with direnv and unloading them upon exit.
!direnv command execution result
Live production server
How It's Used in Practice
1. Project-Specific Key Isolation
Situation — You use different DATABASE_URLs for each project.
echo 'export DATABASE_URL=postgres://localhost/mydb' > .envrc
direnv allow
Result — DATABASE_URL is automatically loaded upon cd.
Key Point — Keys do not leak into shell history or other projects.
2. Team Onboarding
Situation — Enabling team members to use a fresh clone immediately.
cp .envrc.example .envrc && direnv allow
Result — Onboarding is complete with a single line.
Key Point — Commit .envrc.example to git.
Actual Execution Screen
This shows the result of checking the current status with direnv status.
!direnv command execution result 2
Live production server
Points to Note
- Only run
direnv allowon trusted projects. Malicious.envrcfiles can execute shell commands. - A safe pattern is to not write real keys in
.envrc. Instead, gitignore the.envfile and source it from.envrc. - Some IDEs might not recognize the direnv shell hook.
Source (HTML): https://games.aidebatehub.com/programs/linux/modern/direnv.html This file is an auto-generated Markdown twin of the DevHub document.
AI Knowledge Hub
Visitor Comments