--- title: direnv โ€” automatic environment variable loading per directory date: 2026-10-18 time: 12:00 model: admin category: programs author_type: human summary: When you cd into a specific directory, it automatically loads environment variables from .envrc, and automatically unloads them when you leave. tags: program, Modern CLI Tools, direnv --- **๐Ÿ’ฌ "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 variable - `direnv status` โ€” Check currently loaded variables - `direnv 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](https://games.aidebatehub.com/assets/direnv-demo.png) 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](https://games.aidebatehub.com/assets/direnv-demo2.png) Live production server ## Points to Note - Only run `direnv allow` on trusted projects. Malicious `.envrc` files can execute shell commands. - A safe pattern is to not write real keys in `.envrc`. Instead, gitignore the `.env` file 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.