Sitemap.xml generation with Rails and Kamal
Rails doesn’t offer a default solution for generating sitemaps. By using Kamal and an external dependency, it is trivial to add support for sitemaps to your Rails 7.1+ project.

What are sitemaps?

A sitemap is a file where you provide information about the pages, videos, and other files on your site, and the relationships between them. Search engines like Google read this file to crawl your site more efficiently

What is Kamal?

Kamal is a tool for deploying Rails applications. It is based on Docker. It is the new way to deploy Rails 7.1+ apps.

Getting started

Installing sitemap_generator

First, we need to generate a sitemap file locally. We will use kjvarga/sitemap_generator to add support for sitemaps to your Rails app. Don’t mind the date of the latest commit. The project still works well for Rails 7.1+ apps.

Install the gem with Bundler:

bundle add sitemap_generator

Create the config/sitemap.rb file that sitemap_generator uses for its configuration:

bin/rails sitemap:install

Configuring sitemap_generator

Now, open the config/sitemap.rb file and change your default_host to your host. In my case, it’s

# config/sitemap.rb

# Set the host name for URL creation
SitemapGenerator::Sitemap.default_host = ""

If you don’t change it, then your sitemap will be pointing to, and we don’t want to confuse Google’s crawler.

Let’s add a few pages to the sitemap so that it won’t be empty (the root URL is added by default):

# config/sitemap.rb

add "/terms"
add "/privacy"

We just added the “Terms of Service” and the “Privacy Policy” pages. Feel free to add whatever page you want to be served on Google.

Note: You can use URL helpers instead of hardcoded paths.

Generating your sitemap.xml

You can generate sitemap.xml with a simple task provided by the sitemap_generator gem:

% bin/rails sitemap:refresh
In '/Users/kyrylo/Code/kyrylo/matcharoo/public/':
+ sitemap.xml.gz                                           1 links /  329 Bytes
Sitemap stats: 1 links / 1 sitemaps / 0m00s

Pinging with URL '':
Ping failed for Google: #<OpenURI::HTTPError: 404 Sitemaps ping is deprecated. See> (URL

You can safely ignore the warning about the failed ping. Track the related issue in the sitemap_generator’s issue tracker.

TL;DR: Google removed that endpoint, and pinging is no longer necessary.

The command generated a gzipped version of your sitemap at public/sitemap.xml.gz. Let’s unzip it and inspect the content:

gunzip -k public/sitemap.xml.gz
xmllint --format public/sitemap.xml
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns:xsi="" xmlns="" xmlns:image="" xmlns:video="" xmlns:news="" xmlns:mobile="" xmlns:pagemap="" xmlns:xhtml="" xsi:schemaLocation="">

Everything worked as expected. Our app now supports sitemaps.

Configuring sitemaps to work with Kamal

It would be tedious to always refresh sitemaps manually with bin/rails sitemap:refresh before you deploy your app. If you deploy your app with Kamal, there’s a simple solution to that problem.

Kamal supports hooks, and we can leverage them for this task. If you ran kamal init to set up Kamal, then the following sample hooks were created for you at the root of your project:

  • .kamal/hooks/post-deploy.sample
  • .kamal/hooks/post-traefik-reboot.sample
  • .kamal/hooks/pre-build.sample
  • .kamal/hooks/pre-connect.sample
  • .kamal/hooks/pre-deploy.sample
  • .kamal/hooks/pre-traefik-reboot.sample

We are interested in the .kamal/hooks/pre-build.sample hook.

Activate the hook by changing its filename:

mv .kamal/hooks/pre-build.sample .kamal/hooks/pre-build

If you don’t have .kamal/hooks/pre-build.sample, then simply create it with:

mkdir -p .kamal/hooks && touch .kamal/hooks/pre-build

Open the file and add the sitemap generation command there:


RAILS_MASTER_KEY=`cat config/master.key` bin/rails sitemap:refresh

Note: We need to provide RAILS_MASTER_KEY because otherwise, the run will fail with:


Now, when you deploy your app, this line will be run before the Docker image is built.

Important: Make sure your sitemap is not ignored in .dockerignore.

Deploying your sitemap to the server

We are done with all the configuration. Deploy your app as you normally do!

In my case, the command is the following:

kamal deploy -d production
Running the pre-build hook...
  INFO [11d0414b] Running /usr/bin/env .kamal/hooks/pre-build as kyrylo@localhost
 DEBUG [11d0414b] Command: ( export KAMAL_RECORDED_AT="2024-02-16T11:33:05Z" KAMAL_PERFORMER="kyrylo" KAMAL_DESTINATION="production" KAMAL_VERSION="5ca783a66ab25181e3a7554ace7a81ec6fe129d7_uncommitted_6df7cbededce5a20" KAMAL_SERVICE_VERSION="matcharoo@5ca783a66ab25181e3a7554ace7a81ec6fe129d7_uncommitted_6df7cbededce5a20" KAMAL_HOSTS="x.x.x.x,x.x.x.x" KAMAL_COMMAND="deploy" ; /usr/bin/env .kamal/hooks/pre-build )
 DEBUG [11d0414b]       In '/Users/kyrylo/Code/kyrylo/matcharoo/public/':
 DEBUG [11d0414b]       + sitemap.xml.gz                                        1964 links /    11.9 KB
 DEBUG [11d0414b]       Sitemap stats: 1,964 links / 1 sitemaps / 0m00s
 DEBUG [11d0414b]
 DEBUG [11d0414b]       Pinging with URL '':
 DEBUG [11d0414b]       Ping failed for Google: #<OpenURI::HTTPError: 404 Sitemaps ping is deprecated. See> (URL
  INFO [11d0414b] Finished in 2.379 seconds with exit status 0 (successful).

Now that our app is deployed, check the sitemap at

Download the file, unzip it, and make sure the content is what you expect it to be.

Submitting your sitemap to the Google Search Console

Navigate to the Search Console, find the “Indexing / Sitemaps” section, and add your gzipped sitemap there:

Submitted sitemap for


Now, whenever you deploy your app, or edit config/sitemap.rb to add new entries to the sitemap, your changes will automatically be published under /sitemap.xml.gz, and Google crawlers will be fetching them periodically.

Good job!

You can discuss this article on X/Twitter:

Share on: