Install direnv on Ubuntu 18.04 in 1 min

2020-02-18

Splash

At Will & Skill we care deeply about using the right tools to optimize our workflow. If we can shave off a few seconds here and there we can save a lot of time in the long run.

direnv is a small but nifty tool that optimizes the task of switching to the right virtualenv

Gif of terminal commands

1. Install direnv

sudo apt-get update
sudo apt-get install direnv

2. Add the snippet below in Your .bashrc/.zshrc

show_virtual_env() {
  if [ -n "$VIRTUAL_ENV" ]; then
    echo "($(basename $VIRTUAL_ENV))"
  fi
}

PS1='$(show_virtual_env)'$PS1

# direnv
eval "$(direnv hook bash)"

3. Add .direnvrc to your home folder

Copy this snippet to the .direnv that You just created

Javascriptlayout_virtualenv() {
  local venv_path="$1"
  source ${venv_path}/bin/activate
}

4. Add .envrc in your project folder for instance in /home/faisal/myproject and copy the snippet below into the file

Javascriptlayout virtualenv ~/myproject/myproject-env

That's it! Happy coding.