Deploying Rails on Fly.io

Deploying Rails on Fly.io

Recently, Robby Russel posted opens a new window posted on Twitter asking for Heroku alternatives for small Rails apps. Since it got quite a bit of traction, I decided to explore some options and post my experiences as blog posts.

For my first post, I’ll go for one that I’ve already used in the past and liked it very much: Fly.io

I like them because they’re dead simple and have great docs. The dashboard is pretty good too, but since I’m not one to have too many requirements around my apps, the simplicity of use is what I like the most.

1. Install flyctl and log into Fly

The way we deploy things in Fly.io is using flyctl, a command line app that is used to manage the applications.

If you’re going to do anything on Fly.io, you’ll need it. So the first thing to do is to install it.

The Install flyctl page opens a new window has instructions for the main OSes around (MacOS, Linux and Windows). For this article, because I use MacOS, I’ll be following those instructions.

If you have Homebrew, you can type:

brew install flyctl

If not, there’s an installation script:

curl -L https://fly.io/install.sh | sh

If you used the script, you’ll need to add the flyctl directory to your shell’s rc file. The output of the installation will give you the entries you need to add.

Whatever the method, if you finished the previous step successfully, you should be able to run the flyctl command.

This is the expected output:

$ flyctl
This is flyctl, the Fly.io command line interface.

Usage:
  flyctl [flags]
  flyctl [command]

It doesn't look like you're logged in. Try "fly auth signup" to create an
account, or "fly auth login" to log in to an existing account.

Here's a few commands to get you started:
  flyctl launch    Create and configure a new app from source code or a Docker image
  flyctl status    Show app status
  flyctl deploy    Deploy Fly applications
  flyctl logs      View app logs
  flyctl apps      Manage apps
  flyctl machine   Commands that manage machines

If you need help along the way:
  Use `fly docs` to open the Fly.io documentation, or visit https://fly.io/docs.
  Use `fly <command> --help` for more information about a command.
  Visit https://community.fly.io to get help from the Fly.io community.

For a full list of commands, run `fly help`.

Once you confirm flyctl was correctly installed, log in to your Fly.io account. Also, assuming this is your first time around doing this, you’ll need to run:

flyctl auth login

This will open your browser where you’ll log in, after which you can go back to the terminal:

$ flyctl auth login
Opening https://fly.io/app/auth/cli/181f51e0...

Waiting for session... Done
successfully logged in as mateusdeap@gmail.com

2. Create your Rails app

I’m going to assume you have one already. If not, Fly.io’s guide runs you through creating one.

In my case, I’ll be using an app I created as an exercise to learn Hotwire: a character sheet application for FFRPG 3e rules opens a new window .

3. Deploy

Run fly launch from your app directory and follow the wizard. It’s that simple. In my case, all I need is a PostgreSQL database, so my flow look like this:

$ fly launch
Creating app in /Users/mateus/Projects/ruby/character_sheet
Scanning source code
Detected a Rails app
? Choose an app name (leave blank to generate one): ffrpg
? App ffrpg already exists, do you want to launch into that app? Yes
Some regions require a paid plan (bom, fra, maa).

...

Creating a second machine to increase service availability
Finished launching new machines
-------
NOTE: The machines for [app] have services with 'auto_stop_machines = true' that will be stopped when idling

-------

Visit your newly deployed app at https://ffrpg.fly.dev/

And if you click that link at the end you’ll see your application:

FFRPG app login page

Once you sign up, you can create your characters (using Hotwire Turbo!):

New character form

After filling out some details…

Character list

4. Final observations

This app uses PostgreSQL as the database. If you happen to want to run a standard Rails application that uses SQLite, some things aren’t fully supported on Fly.io, like deploying to multiple regions. Make sure to read through their docs opens a new window on this, but if you require multiple regions in production, it might be best to pick a different service to deploy the application, unless you don’t mind the risk.

Also, despite being functional, this application is quite simple, so if you use more services (e.g. Sidekiq, Redis, etc.) your experience may vary. The aim of this tutorial was merely to showcase the simple path for those considering to migrate from Heroku; and for that we believe that Fly.io’s strong selling point is its ease of use. Its command line tool is easy to understand and thoroughly documented. They also have great community support in the form of a forum opens a new window and, of course, direct support opens a new window for those signed up on their Scale, Launch or Enterprise plans

Incidentally, if you need help migrating your app to Fly.io or any other hosting service, we can do that for you! Just send us a message opens a new window and get in touch with our team!

Get the book