Deploy a Rails App to Heroku

Deploy a Rails app with an encrypted .env.vault file to Heroku.

Initial setup

Install Rails.

gem install rails

Create a new Rails project.

rails new yourapp

Edit config/routes.rb and set root to www#index.


Rails.application.routes.draw do
  get "up" => "rails/health#show", as: :rails_health_check
  root "www#index"

Create app/controllers/www_controller.rb.


class WwwController < ApplicationController
  def index

Create app/views/www/index.html.erb.


Hello <%= ENV["HELLO"] %>.

Commit that to code and push it to Heroku.

heroku create
git push heroku

Once deployed, your app will say 'Hello .' as it doesn't have a way to access the environment variable yet. Let's do that next.

Install dotenv-vault-rails

Add dotenv-vault-rails to the top of your Gemfile.


source ""
ruby "3.1.3"
gem "dotenv-vault-rails", require: "dotenv-vault/rails-now"
gem "rails", "~> 7.1.1"
bundle install

Create a .env file in the root of your project.


# .env

Try running it locally.

bin/rails server
=> Booting Puma
=> Rails 7.1.1 application starting in development
* Listening on
* Listening on http://[::1]:3000

It should say Hello World.

Great! ENV now has the keys and values you defined in your .env file. That covers local development. Let's solve for production next.

Build .env.vault

Push your latest .env file changes and edit your production secrets. Learn more about syncing

npx dotenv-vault@latest push
npx dotenv-vault@latest open production

Use the UI to configure those secrets per environment.

Then build your encrypted .env.vault file.

npx dotenv-vault@latest build

Its contents should look something like this.


#/         cloud-agnostic vaulting standard         /
#/   [how it works](   /

# development

# production


Fetch your production DOTENV_KEY.

npx dotenv-vault@latest keys production
# outputs: dotenv://:[email protected]/vault/.env.vault?environment=production

Set DOTENV_KEY on Heroku using the CLI.

heroku config:set DOTENV_KEY='dotenv://:key_1234…'

Or use Heroku's UI.


Commit those changes safely to code and deploy.

That's it! On deploy, your .env.vault file will be decrypted and its production secrets injected as environment variables‚ just in time.

You'll know things worked correctly when you see Loading env from encrypted .env.vault in your logs. If a DOTENV_KEY is not set (for example when developing on your local machine) it will fall back to standard dotenv functionality.

heroku logs --tail