Installing direnv

30 Jan 2019 17:01 direnv

From direnv.net:

direnv is an environment switcher for the shell.

Installing direnv

Ubuntu

direnv is available as an Ubuntu package, but unless you’re on a really recent Ubuntu, it’ll be outdated.

Download the appropriate binary from the releases page, mark it executable, and stick it somewhere in your PATH.

For example:

curl -qfsSL https://github.com/direnv/direnv/releases/download/v2.32.1/direnv.linux-amd64 -o direnv
chmod +x direnv
sudo mv direnv /usr/local/bin

macOS

brew install direnv

Hooking the shell

Then you need to hook it into the shell.

I only care about bash:

# at the end of ~/.bashrc
eval "$(direnv hook bash)"

This (for bash at least) uses PROMPT_COMMAND to ensure that the direnv hook gets run whenever the prompt is re-displayed.

Note: this has the interesting side effect that you can’t do (cd .. ; ruby -v) and expect it to work.

direnv appears to do the same (prompt hook) for fish, but it appears to use a pre-command hook in zsh.