Skip to content
Advertisement

how to add environment variable in linux for rails app?

i need to set an environment variable for the rails app to use

SECRET_KEY_BASE=9941144eb255ff0ffecasdlkjqweqwelkjasdlkjasd

the config settings for production is as shown below

# Do not keep production secrets in the repository,
# instead read values from the environment.
production:
  secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>

how can i set the environment variable using the linux command

export VARNAME="my value" 

I tried to set the variable but looks like it needs to be for the right user. Sorry i am not an expert in linux. I appreciate any help! Thanks!

Advertisement

Answer

export VARNAME="my value"

Well the above works for your current terminal session. After this command, all the subsequent commands can access this variable. Try running this:

echo $VARNAME

It will print the value my value in the console. If you want this behaviour to be persisted, you need to place the export command in your OS’ config file (~/.bashrc in case of Ubuntu).

After editing this file, either restart your terminal, or run this:

source ~/.bashrc

This will reload the file in your current terminal session. Alternatively, you can try running your Rails server (or a rake command) as follows:

VARNAME="my value" rails s
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement