Richard North’s Blog

More prac­ti­cal direnv

Some con­sid­er­able time ago I wrote my praise for direnv along with some us­age tips. That blog post con­tin­ues to re­ceive plenty of traf­fic from peo­ple seek­ing direnv con­fig­u­ra­tion guid­ance.

Since then, I have con­tin­ued to use direnv and (quietly) ad­vo­cate for it among any­one who will lis­ten. However, what I have not done is share how my us­age of the tool has evolved and im­proved.

For any­one need­ing a brief in­tro­duc­tion, direnv is a tool that runs hook scripts (.envrc) as you cd around a file sys­tem. For ex­am­ple, you can cd into a di­rec­tory hold­ing a Python code­base and have its vir­tualenv au­to­mat­i­cally ac­ti­vated. If you leave the di­rec­tory, the vir­tualenv is au­to­mat­i­cally de­ac­ti­vated. direnvs power is its flex­i­bil­ity: it can run any script, up­date the PATH or any other en­vi­ron­ment vari­ables, with­out cou­pling to a spe­cific shell.

With a cus­tom ~/.direnvrc script direnv gives me au­to­matic setup of lan­guage tool­ing for:

Examples #

All ex­am­ples as­sume that my cus­tom ~/.direnvrc script is be­ing used. My script is only set up for use with Mac OS and re­quires Homebrew.

You’ll also need to have direnv in­stalled and hooked in to your shell, of course!

Java #

If you want to use Java 11 in a spe­cific di­rec­tory, you just cre­ate an .envrc file con­tain­ing:

use java adopt@1.11.0-6

After mod­i­fy­ing an .envrc file you must al­low direnv to run it, for se­cu­rity rea­sons. This is sim­ply done by en­ter­ing direnv allow at the shell. For ex­am­ple:

$ echo "use java [email protected]" >> .envrc
direnv: error /Users/rnorth/somedir/.envrc is blocked. Run `direnv allow` to approve its content

$ direnv allow
direnv: loading ~/somedir/.envrc
direnv: using java [email protected]
JAVA_HOME=/Users/rnorth/.jabba/jdk/[email protected]/Contents/Home

$ java -version
openjdk version "11.0.6" 2020-01-14
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.6+10)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.6+10, mixed mode)

From now on, any time you cd into this di­rec­tory, direnv will:

.jabbarc files are also re­spected, and will be au­to­mat­i­cally ap­plied from the cur­rent or par­ent di­rec­tory. All that’s needed is a blank .envrc file to trig­ger direnv into ac­tion.

Python #

Similarly, cre­ate an .envrc file con­tain­ing:

use python 3.7.6

direnv will:

NodeJS and Ruby #

Unsurprisingly, the pat­tern con­tin­ues for NodeJS and Ruby! For ex­am­ple:

use node 12.16.3


use ruby 2.3.3

in­stall and set up NodeJS and Ruby au­to­mat­i­cally, as above.

Summary #

direnv is a pow­er­ful tool, and one that I’m still very happy to use more than five years af­ter first dis­cov­er­ing it.

With a small amount of set­ting up, with a cus­tom ~/.direnvrc file, it’s ex­tremely pow­er­ful, and I hope you find my ex­am­ple a use­ful start­ing point!

I’d strongly rec­om­mend direnv for:

Enjoy a more pro­duc­tive, less has­sle, shell ex­pe­ri­ence!

← Home