Skip to main content
We haven’t yet updated this page for Fly Machines, which are the VMs now at the core of all Fly Apps. You might notice that some features don’t work the same way anymore. Visit our community forum if you need help with your app.
Getting an application running on Fly.io is essentially working out how to package it as a deployable image. Once packaged, it can be deployed to the Fly.io platform. In this guide we’ll learn how to deploy a Crystal application on Fly.io.

Creating a Lucky App

Our example will be using a basic Lucky app with PostgreSQL. We’ll assume you already have lucky+crystal installed. Once lucky is installed, we can create a new project by running the following command:
This will walk you through project options and create a new project.

Running The Application

To run the application, first run script/setup to install dependencies and create the database. Then run lucky dev to start the application. Connect to the address displayed in the console. This should bring you to the “Hello Lucky” page.

Install Flyctl and Login

It’s time to install flyctl, the CLI app for managing apps on Fly.io. If you’ve already installed it, carry on. If not, hop over to our installation guide. Once that’s installed you’ll want to log in to Fly.io.

Launch the app on Fly

When you run fly launch from the newly-created project directory, the launcher provides some defaults for your new app, and gives you the option to tweak the settings. Run:
You’ll get a summary of the defaults chosen for your app:
Type y at the prompt to open the Fly Launch page, and make the following changes to your app config:
  • Change the default app name and region, if needed.
  • For Databases, select Fly Postgres, give the Postgres database app a name (for example, your app name with -db appended) and choose a configuration.
Once you confirm your settings, you can return to the terminal, where the launcher will:
  • Run the deployment setup task
  • Build the image
  • Set secrets required by (SECRET_KEY_BASE, for example)
  • Deploy the application in your selected region
Make sure to note your Postgres credentials from the output.
That’s it! Run fly apps open to see your deployed app in action. Try a few other commands:
  • fly logs - Tail your application logs
  • fly status - App deployment details
  • fly status -a postgres-database-app-name - Database deployment details
  • fly deploy - Deploy the application after making changes

Inside fly.toml

The fly.toml file now contains a default configuration for deploying your app. In the process of creating that file, flyctl has also created a Fly-side application slot of the same name, “hello-lucky”. If we look at the fly.toml file we can see the name in there:
The flyctl command will always refer to this file in the current directory if it exists, specifically for the app name/value at the start. That name will be used to identify the application to the Fly platform. The rest of the file contains settings to be applied to the application when it deploys. We’ll have more details about these properties as we progress, but for now, it’s enough to say that they mostly configure which ports the application will be visible on.

Deploying to Fly

To deploy changes to your app, just run just run:
This will lookup our fly.toml file, and get the app name hello-lucky from there. Then flyctl will start the process of deploying our application to the Fly platform. Flyctl will return you to the command line when it’s done. Alternatively, you can use GitHub Actions to deploy your app to Fly

Viewing the Deployed App

Now the application has been deployed, let’s find out more about its deployment. The command fly status will give you all the essential details.
If you want to know what IP addresses the app is using, try flyctl ips list:

Connecting to the App

The quickest way to browse your newly deployed application is with the flyctl apps open command.
Your browser will be sent to the displayed URL.

Multi-Region Deployment with Postgres (Optional)

Fly also supports multi-region deployments. To deploy to multiple regions, first Create a PostgreSQL Cluster, then follow these steps:
  1. Configure your primary region by setting the PRIMARY_REGION in your fly.toml.
  1. Add the superfly/fly.cr shard to your project
  1. Require fly/pg/error_handler and fly/avram after avram
  1. Add the Fly::PG::ErrorHandler middleware to your list of middlewares, after the Lucky::ErrorHandler
  1. Deploy with fly deploy
  2. You can see replays in the fly log

Arrived at Destination

You have successfully built, deployed, and connected to your first Crystal application on Fly.