Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add .env file or otherwise set environment variables in a Heroku app?

Tags:

heroku

I've tried many different solutions on the web for this problem, but all have been unsuccessful.

Here's the problem: My app needs to know whether it is being run on Heroku (production mode) or locally (development mode). For this purpose, we want to use environment variables. I've understood that environment variables on Heroku can be set in a .env file. So my attempt was to run heroku run bash -a <app-name> and then to install vim by doing this:

mkdir ~/vim
cd ~/vim

# Staically linked vim version compiled from https://github.com/ericpruitt/static-vim
# Compiled on Jul 20 2017
curl 'https://s3.amazonaws.com/bengoa/vim-static.tar.gz' | tar -xz

export VIMRUNTIME="$HOME/vim/runtime"
export PATH="$HOME/vim:$PATH"
cd -

Apart from crashing repeatedly, vim didn't work anymore when I logged in and out of the shell:

~ $ vim // in the heroku shell
vim: error while loading shared libraries: libXt.so.6: cannot open shared object file: No such file or directory

I also tried heroku plugins:install heroku-vim but running heroku vim after that only resulted in a long delay followed by the normal heroku shell opening, no vim.

I don't really care if I get vim to work. I just want to be able to write in a file named .env on Heroku so I can set environment variables in it.

How can I achieve this?

like image 290
Sahand Avatar asked Apr 18 '18 16:04

Sahand


2 Answers

There is no need for an .env file on Heroku. In fact, such a file won't work very well since

  • Heroku gets all of its files from your Git repository,
  • has an ephemeral filesystem, meaning that changes to files like .env will be quickly lost, and
  • the .env file won't be available on other dynos if you scale your app

As such, creating an .env file on Heroku isn't a good approach.

Instead, you can use its built-in support for environment variables, using heroku config:set <var> <value> or its web UI. Either way, you'll get a regular environment variable.

like image 91
Chris Avatar answered Sep 23 '22 08:09

Chris


Simply define the key and the value on reveal config. The same way you have it on .env. Having to commmit your .env file to github isn't secure. for instance Paystack_payment_url as the key https://api.paystack.co as the value.

like image 20
Alemoh Rapheal Baja Avatar answered Sep 25 '22 08:09

Alemoh Rapheal Baja