direnv setup on Linux/Ubuntu

direnv setup on Linux/Ubuntu

direnv is an extension of your shell. It augments the existing shell with a new feature that can load and unload environment variables depending on the current directory.

Use cases

  • Load 12factor apps environment variables

  • Create per-project isolated development environments

  • Load secrets for deployment

Installation and how to use

  • Install direnv if not installed

      sudo apt-get install direnv
    
  • Change your directory where you want to set the environment variable

      cd devstack
    
  • Set the desired variable in .envrc file

      echo export OPENEDX_RELEASE=maple.master > .envrc
    
  • Allow direnv

      direnv allow .
    
  • Check direnv status, whether the .envrc file is loaded or not

      direnv status
    
  • Add the following line at the end of your ~/.bashrc file

      # Make sure it appears even after rvm, git-prompt, and other shell extensions that manipulate your prompt.
      eval "$(direnv hook bash)"
    
  • Check your variable

      # If this doesn't work then close the Terminal and open again and move to you directory
      echo ${OPENEDX_RELEASE-nope}
    

References: