I use direnv
to manage per-directory environment
variables, which comes in handy when a project needs its own Python
or Node (or whatever) environment.
Installation #
Install direnv
from apt
, brew
, or whatever. Then install into your .bashrc
like so:
if which direnv &> /dev/null; then
eval "$(direnv hook bash)"
fi
Node #
One time, install node
like the following. I use nodeenv
to pull in whatever version I want, but the important thing
is the target directory and naming scheme.
nodeenv -n 22.3.0 $HOME/node_versions/node-v22.3.0 --pre-built
Add the following globally (e.g., to .bashrc
):
export NODE_VERSIONS=$HOME/node_versions
And then, here's the per-project Node incantation of .envrc
:
use node 22.3.0
layout node
See the direnv-stdlib(1) for more details.
Python #
Python is a bit different. Here's the .envrc
:
layout python3
It creates the virtualenv for you!
$ which python
[...]/.direnv/python-3.12/bin/python