How to Read an Environment Variable in Ruby
Settings that change between computers, such as a database address or an API key, belong in environment variables. Ruby reads them from ENV, and ENV.fetch('NAME', default) gives you the value, or the default when the variable isn't set.
Syntax
<value> = ENV.fetch('<NAME>', <default>)
<value> = ENV['<NAME>']
| Part | What it does |
|---|---|
ENV.fetch('<NAME>', <default>) | The variable's value, or <default> when it isn't set. |
ENV.fetch('<NAME>') | Without a default, raises KeyError when the variable isn't set. |
ENV['<NAME>'] | The value, or nil when it isn't set. |
The Ruby documentation for ENV.fetch: "If name is the name of an environment variable, returns its value". Otherwise, "if a default value is given (but not a block), returns the default value".
The Example
The example reads GREETING with an empty-string default and prints it with inspect, which shows the quotes, first with the variable set and then without it.
Prerequisites
- Ruby. This guide uses Ruby 4.0.10.
Step 1: Write the Code
Create a file named main.rb:

greeting = ENV.fetch('GREETING', '')
puts "GREETING is: #{greeting.inspect}"
Step 2: Set the Variable and Run It
Set GREETING to Hello and run the script. Then remove the variable and run it again:
- macOS / Linux
- Windows (PowerShell)
export GREETING=Hello
ruby main.rb
unset GREETING
ruby main.rb

$env:GREETING = 'Hello'
ruby main.rb
Remove-Item Env:GREETING
ruby main.rb

GREETING is: "Hello"
GREETING is: ""
With the variable set, fetch returned "Hello"; without it, the default "".
fetch, fetch with a Default, or []?
ENV.fetch('NAME')for settings the program can't run without: a missing variable raisedkey not found: "GREETING_NOT_SET" (KeyError), which stops the script at once.ENV.fetch('NAME', default)when a fallback makes sense.ENV['NAME']whennilis a fine answer: it returnednilfor a missing variable.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Using ENV['NAME'] for a required setting | nil flows on and fails later, far from the cause | Use ENV.fetch('NAME') |
| Expecting a number | Every value is a String, such as "3000" | Convert with Integer(...) |
| Setting the variable in one terminal and running Ruby in another | The default, or nil: each terminal has its own variables | Set it in the terminal that runs the script |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
key not found: "…" (KeyError) | fetch without a default met a missing variable | Set the variable, or pass a default |
| The value is always the default | The variable isn't set in this terminal | Set it as in Step 2 |
FAQ
Are variable names case-sensitive?
On macOS and Linux, yes. Windows treats GREETING and greeting as the same variable.
Can I change a variable from Ruby?
Yes: ENV['NAME'] = 'value' sets it for this script and the programs it starts.
Conclusion
ENV.fetch('NAME', default) reads an environment variable with a fallback; without the default it raises KeyError, which suits required settings. ENV['NAME'] returns nil when the variable is missing.
Additional Resources
- ENV, in the Ruby documentation.
