Skip to content

Instantly share code, notes, and snippets.

@jramnai
Last active September 23, 2022 21:47
Show Gist options
  • Save jramnai/0d4a2cc2dcd1484bfde90aad6b6a00bc to your computer and use it in GitHub Desktop.
Save jramnai/0d4a2cc2dcd1484bfde90aad6b6a00bc to your computer and use it in GitHub Desktop.
Setup direnv on Ubuntu system to set your environment variable

direnv setup on Ubuntu

direnv is an extension for your shell. It augments existing shells 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:

https://direnv.net/

http://manpages.ubuntu.com/manpages/focal/man1/direnv.1.html

https://forum.opencraft.com/t/devstack-tips-and-tricks-general-discussion/65/32

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment