-1

I have tried to set my environment variables multiple ways on my Ubuntu server but nothing seems to work. I need to know how to properly set them on the digital ocean droplet I am using to host my website.

I have tried the following:

  1. By using a file called screts.sh and putting all of the variable exports in there on different lines: export ENVIRONMENT_VARIABLE=TheValue and then using source screts.sh in the terminal.
  2. By simply entering export ENVIRONMENT_VARIABLE=TheValue in the Ubuntu terminal as the user for my application (not as root)
  3. I've also tried to use set ENVIRONMENT_VARIABLE=TheValue but that doesn't work either.

After echoing the variable for the last 2, it gave me back the correct value. I did it by typing this in the terminal: echo $VARIABLE_NAME

Thanks for any help!

Jake
  • 1,086
  • 12
  • 38
  • 1
    Where does Rails come into this? Does it use the variable? – kristianp Sep 21 '18 at 01:42
  • @kristianp I think it would have to do with this, I will be using variables for my database password in the rails app, my production key, and for my sendgrid username and password for my mailer. – Jake Sep 21 '18 at 01:44
  • Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/) would be a better place to ask. – jww Sep 21 '18 at 02:06
  • @jww I think, bash is related with this website, right? – Berkhan Berkdemir Sep 21 '18 at 02:20
  • 1
    You're question should be something like _I have set the environment variables on my droplet server, they are not being read, or how do I read the values from Rails_. This example of a question belongs here. – Kedarnag Mukanahallipatna Sep 21 '18 at 02:57
  • btw why don't you use `.bash_profile` file to put all your `environment` variables ? Have you tried logging into rails console on the server and print those keys ? Does it return anything or empty ? Like using this command `ENV` or `ENV["your_key"]`. – Kedarnag Mukanahallipatna Sep 21 '18 at 03:21
  • 1
    How to set an environmental variable, which you seem to want explained to you, is off-topic. How to set an environmental variable has been asked and answered so many times you have to make an effort to avoid finding an answer. [How to set Ruby Environment in Linux?](https://stackoverflow.com/q/23821982/608639), [Environment variables in ruby script on Linux](https://stackoverflow.com/q/34264383/608639), etc. – jww Sep 21 '18 at 03:28
  • @jww The first question you linked to is on how to actually change the environment that ruby run in (development, test, or production). The second is about a script that changes the environment variable or something based on a script. I am having problems setting the variable of a value that won't change often at all. It also has nothing to do with setting the ruby environment the Ubuntu server is running. – Jake Sep 21 '18 at 03:41
  • @KedarnagMukanahallipatna I'm pretty new to setting env variables, I didn't know about bash_profile, nor have I tried the console. In my app I am using the code `ENV['SENDGRID_USERNAME']` to get the value. – Jake Sep 21 '18 at 03:44

1 Answers1

0

here is what you could do, open(create one if it does not exist) the .bash_profile file.

Add all the export lines into the file.

export SENDGRID_USERNAME=""
export SENDGRID_PASSWORD=""
export DATABASE_NAME=""

then save the file and run source ~/.bash_profile. Then open rails console and either run ENV or ENV["SENDGRID_USERNAME"], you should see the value you have assigned to the corresponding key.

Also as an alternative we have gem which you could consider once you know how .bash_profile setup works.