Guide to deploy a Rails app (in less than 10 minutes)

Below you’ll find a step-by-step process on how to create a brand new Rails app and deploy it to Fly.io. This guide if for you, if you’re looking for to spin up an app because:

  • You want to learn how to deploy an application from scratch.
  • You’re building your portfolio or a small app.
  • You’re looking for a decently abstracted solution.
  • You’ve spent hours non-stop battling with documentation or trying to setup things by yourself.

Check the section at the end for more information about why Fly is the recommended option.

Pre-work

  • Close window and go back to your terminal.

Create a Rails app from scratch

  • Run the following command to create a new app (rv will automatically install ruby and rails gem for you): rvx rails new demo-app

  • After the command finish setting up the app, go to the newly created app folder: cd demo-app

  • Install the project dependencies by running: rv ci

Update Dockerfile

  • The Dockerfile provided by default needs some adjustments to work with Fly, run the following 2 commands to update it:

    • bundle add dockerfile-rails --optimistic --group development
    • rv run rails generate dockerfile --force

Launch!

  • Run the flyctl command to create a new app and deploy your code: fly launch

  • Process will stop and show the default settings:

We're about to launch your Rails app on Fly.io. Here's what you're getting:

Organization: Javier Cervantes              (fly launch defaults to the personal org)
Name:         demo-app-lingering-cloud-1789 (generated)
Region:       San Jose, California (US)     (this is the fastest region for you)
App Machines: shared-cpu-1x, 1GB RAM        (most apps need about 1GB of RAM)
Postgres:     <none>                        (not requested)
Redis:        <none>                        (not requested)
Tigris:       <none>                        (not requested)

════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════

? Do you want to tweak these settings before proceeding?
  • Type N and return to use the default values.

  • Once the deployment is finish you’ll see URL where the app was created and deployed to a unique URL, for example: Visit: https://demo-app-lingering-cloud-1789.fly.dev/

Verify

Deploy with a simple database

  • Run this command to create a products table and the related scaffolded files: bin/rails generate scaffold Product name:string

  • Since the app already created, use the deploy command to push the changes: fly deploy

  • Now you can test the changes in [URL]/products

That’s it! You can use the same process to deploy an existing application or follow the Getting Started with Rails tutorial for a walk through of how to build a simple e-commerce app.

Why choose PAAS and Fly?

I recommend using a PAAS provider if you’re looking to get something working quickly or if you’re not well versed in “Devops stuff”. For example, self-hosting in a VPS adds additional complexity like provisioning a server, setting up a domain, ssh configuration and you need to take care of security and updates.

More specifically, I selected Fly.io for this guide because it has a free trial that allows you to spin something up, and decide if you want to continue or look for something else. If you only need to deploy a simple application, the price is also very competitive:

Finally, it also has the following benefits:

  • No additional Postgres database required (unless you want to).
  • Pay as go you plans (you can suspend/stop the machines and you won’t be charged)
  • You can use the same Dockerfile to switch to a different provider or self-host later.
  • Comprehensive Rails documentation and a community forum for troubleshooting.

You can review and compare with other similar options:

Before you go

Thanks for reaching this far! If you find this guide helpful, please share it with others.

I would love to know if you’re able to deploy your apps or if you have any issues, please send your feedback.

Happy coding!

5 Likes

This is a great writeup! Another area where Fly excels is in setting up live, streaming backups of your production sqlite database, using litestream. Just a fly storage create sets up an S3-compatible Tigris bucket, and adds the env vars Litestream will need to your app. Then you can use the litestream-ruby gem to wrap your rails s command and get live backups while your app runs!

3 Likes

Thanks for your comment. I didn’t know about that feature… I’ll take a look!

Fly.io reminds me of my old Heroku days. It’s so fun :slight_smile:

btw you can use set a custom domain and its SSL certificate with

fly certs add foobar.com There is a good blog post about it Use your own domain name with a Fly app - DEV Community

I didn’t know that you could set the domain with a command! Thanks for sharing the article, seems to be very straightforward.

For this guide I wanted to keep things simple, but maybe I could work on a follow-up to dig into this type of settings… :thinking:

1 Like