direnvis an environment switcher for the shell.
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.
curl -qfsSL https://github.com/direnv/direnv/releases/download/v2.19.0/direnv.linux-amd64 -o direnv chmod +x direnv sudo mv direnv /usr/local/bin
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.