Deploying Ruby on Rails 8 application in minutes on production with Kamal

Deploying Ruby on Rails 8 application in minutes on production with Kamal

Today I am going to showcase you how to deploy a brand new Ruby on Rails 8 application on any VPS without any major configurations or setups on server.

Over the past decade, deploying web applications has become significantly easier with the rise of platforms like Heroku, Fly.io, Render, and Vercel. However, these Platform-as-a-Service (PaaS) solutions often come with vendor lock-in and premium pricing.

Kamal—a modern deployment tool built by the Rails team that leverages Docker to streamline deployment to your own infrastructure. Whether it's a VPS from DigitalOcean, AWS, or a physical server you control, Kamal empowers you to deploy with minimal setup. All you need is Kamal, a fresh Ubuntu server with SSH access, and you can be up and running in minutes.

This philosophy aligns with the direction of Rails 8, which embraces a "No PaaS" approach—putting ownership and control back in the hands of developers.

Now let's make our hand bit dirty with Kamal configurations and deployment

Kamal does not need a lot of configuration to deploy to production.

Configuration for Kamal lies in the config/deploy.yml file.

The first part of this is 

# Name of your application. Used to uniquely configure containers.
service: kamal_demo

You can specify the name you want to for the service here. Same will be used to build your docker image. Rails 8 ships with a docker file that is perfectly tuned to deploy your app to production.

Basically Kamal will use this Docker file to build an image of your app and then run it inside a docker container on a server that you specify.

The name of the image is very particular thing. We will name it after our username on Docker and the same like we named our service.

# Name of the container image.
image: your-user/kamal_demo

Kamal uses docker to deploy to our server but how do we get our image onto our server?

Docker will use a docker image registry by default that is DockerHub.

So you can use Dockerhub account to push and pull images during deployment with Kamal.

Next the line below specifies the IP address of the server we are deploying to.

# Deploy to these servers.
servers:
  web:
    - 192.168.0.1
# job:
  #   hosts:
  #     - 192.168.0.1
  #   cmd: bin/jobs

There are a few commented lines above that indicates configurations to mention and connect your job running servers.

Next here is a proxy key, we can enable SSL Auto certification via. Let’s Encrypt and it is pretty straightforward with Kamal. Just few configurations and boom! Your app is self signed certificate enabled.

You have to just set SSL to true and we have a host you can specify the DNS that is pointed to your server IP

proxy:
ssl: true
host: app.example.com

Next you need to specify your registry and again the default registry is DockerHub. You can also use Github, there’s also a DigitalOcean registry for containers.

# Credentials for your image host.
  registry:
    # Specify the registry server, if you're not using Docker Hub
    # server: registry.digitalocean.com / ghcr.io / ...
    username: your-user

  # Always use an access token rather than a real password when possible.
  password:
    - KAMAL_REGISTRY_PASSWORD

The KAMAL_REGISTRY_PASSWORD is an environment variable that gets injected into Kamal from a file called .kamal/secrets.

We can use .kama/secrets file to store all our secrets and passwords locally.

I  have stored it in my credentials file.

# Grab the registry password from ENV
KAMAL_REGISTRY_PASSWORD= Rails.application.credentials.kamal_registry.password

# Improve security by using a password manager. Never check config/master.key into git!
RAILS_MASTER_KEY=$(cat config/master.key)

You can also specify your database host configs in kamal file.

  DB_HOST: 192.168.0.2

So in our database.yml file we have configuration for the solid trifecta cache, cable and queue as well.

Once all the configurations are made you just need to run

~» kamal setup

It will do all the things for you, like auto SSH into the server we specified, builds the docker image by pulling it from registry we specified, setup server and proxy settings and boom! Our app is ready to serve.

Kamal has lot of other features and configurations which we can use to make deployment easier and faster. We will cover more aspects in our next blog.

Thanks for reading.


Need help building something similar? Our team specializes in delivering scalable, high-performance solutions tailored to your business needs. Let’s talk