How to use direnv to create environment variables in ubuntu
February 10, 2018 · 5 mins read
Environment variables are very important for setting up important variables that you don’t want to share in code. In my project, I have to set up some important variable that I require all over my project for the development. For example database names, email passwords, private keys and all other such things. But the problem with the environment variables is that you need to set them again and again. Everytime you come back to your shell, you have to export them again. First of all, Let’s discuss the way in which we can set environment variables without using direnv. The easiest way of creating these variables is as follows:
And correspondingly you can call these variables in the python file as follows:
$ export DB_USER=root $ export DB_PASSWORD=ranvir
import os DB_USER = os.environ['DB_USER'] DB_PASSWORD = os.environ['DB_PASSWORD']
This way you can save yourself from sending very important secrets into your repository like I did some time ago and had go through a very long process for deleting that code from GitHub. In the meantime someone used the password and logged into my email account. Read the post about how I was able to delete the password from GitHub code without deleting the repository. This is good solution, Right? There is no doubt that once you start using it, you will fall in love with such technology. But in few days you will want to get out of typing in all the environment variables again and again after a shutdown or shifting of the bash shell. The solution of this problem is direnv. Direnv is an open-source project used for proper setting of environment variables. The idea behind this is that you set up all your variables in a single file called
.envrcand the environment will be created as soon as you enter the directory in which
.envrcfile is present. For installing
direnvin ubuntu apply the following command:
or in MAC, use
$ sudo apt-get install direnv
After this, you have to add the following line at the end of the
brew install direnv
Now if you are using
eval "$(direnv hook bash)"
zshas I do, you will have to add the following to
Now go to a directory where you want to launch the environment variables. As long as you stay in that directory the value of environment variables will not change. Use the following commands to create a file
eval "$(direnv hook zsh)"
Open the file in your favourite text editor and add the following content. I myself prefer vim
$ touch .envrc
Save and close the file. The following error will be reported when you close the file.
export DB_NAME=abc export DB_USER=root
Do as the error say. Apply the following command and start using the environment variables:
direnv: error .envrc is blocked. Run `direnv allow` to approve its content
Applying the command will give the following result:
$ direnv allow
When you leave the directory the following result will be shown:
direnv: loading .envrc direnv: export +DB_NAME ...
As long as you stay somewhere in the directory, environment variable will always be available. This now starts working as the virtual environment. Read more about virtual environments Do share your views about the post.
Please share your Feedback:
Did you enjoy reading or think it can be improved? Don’t forget to leave your thoughts in the comments section below! If you liked this article, please share it with your friends, and read a few more!