Skip to content

Instantly share code, notes, and snippets.

@rwarbelow
Created November 11, 2015 18:26
Show Gist options
  • Star 65 You must be signed in to star a gist
  • Fork 21 You must be signed in to fork a gist
  • Save rwarbelow/40bd72b2aee8888d6d91 to your computer and use it in GitHub Desktop.
Save rwarbelow/40bd72b2aee8888d6d91 to your computer and use it in GitHub Desktop.
How to Run a Rails App in Production Locally
  1. Add gem 'rails_12factor' to your Gemfile. This will add error logging and the ability for your app to serve static assets.
  2. bundle
  3. Run RAILS_ENV=production rake db:create db:migrate db:seed
  4. Run rake secret and copy the output
  5. From the command line: export SECRET_KEY_BASE=output-of-rake-secret
  6. To precompile your assets, run rake assets:precompile. This will create a folder public/assets that contains all of your assets.
  7. Run RAILS_ENV=production rails s and you should see your app.

Remember to clobber your assets (rake assets:clobber) and re-precompile (rake assets:precompile) if you make changes.

@axlfire1
Copy link

Very helpfull thanks :)

@yinjinit
Copy link

good guidance. thanks

@dirceu-jr
Copy link

To compile assets in production mode (with css_compressor, :uglifier or other js_compressor):
rake assets:precompile RAILS_ENV=production

@catmando
Copy link

in Rails 5, you don't need the rails_12factor instead in step 7 add RAILS_SERVE_STATIC_FILES=true like this:

RAILS_SERVE_STATIC_FILES=true RAILS_ENV=production bundle exec rails s

Do steps 4 and 5 BEFORE step 3 (i.e. add your secret key base environment variable otherwise you cant run the rake commands.

finally its important that in step 6 you prefix with RAILS_ENV=production

@marisnb
Copy link

marisnb commented Feb 2, 2019

  1. Run **_

RAILS_ENV=production rake db:create db:migrate db:seed

**
2. Run **

rake secret

** and copy the output
3. From the command line : **

export SECRET_KEY_BASE=output-of-rake-secret

**
4. Change production.rb file config.assets.compile = false to **

config.assets.compile = true

**
5.To precompile your assets, run **

RAILS_ENV=production bundle exec rake assets:precompile

**. This will create a folder public/assets that contains all of your assets.
6.Run **

RAILS_ENV=production rails s

_** and you should see your app.

@gowritumma
Copy link

Thank you so much. The steps are very clear.
Do I need to upload the compiled assets files(in public folder) to GITHUB if I want to deploy in Heroku?

@rwarbelow
Copy link
Author

@gowritumma Nope. Heroku should compile the assets automatically.

@BGMP
Copy link

BGMP commented Apr 23, 2020

thank you ❤️

@anoop-chaursiya
Copy link

Thank you so much. I am following all steps resolved the bugs.

@donavallikrishnamohan
Copy link

Thank you very much

@TylerSustare
Copy link

RAILS_SERVE_STATIC_FILES=true

Lifesaver with React Rails https://github.com/reactjs/react-rails

@crazyoptimist
Copy link

I use this command for puma on prod env

rails s -e production

@Faq
Copy link

Faq commented Sep 27, 2022

Also needed to set
config.force_ssl = false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment