Deploy a Rails 6 or 7 App on Render – Render Docs
This guide demonstrates how to set up a local Ruby on Rails environment, create an app with a simple view, and deploy that app to Render. It also shows how to connect the app to a Render PostgreSQL database. These same steps work for both Rails 6 and Rails 7.
Deploying Rails 8? See this quickstart.
1. Create a Rails project
If you have an existing Rails project that you want to deploy to Render, you can skip to Update your app for Render.
First, let's set up a local development environment with a basic project structure.
Install Rails
Use the gem install command to install Rails if you haven't yet. Make sure you have the required dependencies installed (Ruby, Node.js and Yarn 1.x).
$ gem install rails
We're using Rails 7.1 in this tutorial, so verify that you have the correct version installed:
$ rails --version
Rails 7.1.2
Create a new project
This tutorial creates a Rails project with the name mysite. You can replace this name with another name of your choosing.
- In your terminal, navigate to the directory where you'll create your project. Then, run the following command to generate a new project that uses Bootstrap for styling:
$ rails new mysite --database=postgresql -j esbuild --css bootstrap
You can provide additional arguments to customize the generated project. Run rails new -h for details.
- Create local PostgreSQL databases for your app:
$ rails db:create
Created database 'mysite_development'
Created database 'mysite_test'
If this command fails, make sure you've installed and started PostgreSQL locally, then check your project's config/database.yml file. You might need to specify your PostgreSQL username and/or password.
You should now have a functional foundation for your new Rails app! To verify, start your development server:
$ bin/dev
To see your app in action, go to localhost:3000 in your browser. You should see the Rails default landing page:
Commit all of your project changes and push them to a repository on a Git provider. You can deploy your app to Render from any supported provider.
2. Create the Hello World landing page
Next, let's add a simple static view to your app.
See the official Getting Started with Rails guide for more on creating Rails apps.
- To create a new Rails controller, you run the controller generator. Set the controller's name to
Renderand set up an action namedindexby running the following command:
$ rails g controller Render index
The generator creates several files in your project:
create app/controllers/render_controller.rb
route get 'render/index'
invoke erb
create app/views/render
create app/views/render/index.html.erb
invoke test_unit
create test/controllers/render_controller_test.rb
invoke helper
create app/helpers/render_helper.rb
invoke test_unit
- Open the
config/routes.rbfile and add the following line:
Rails.application.routes.draw do
get 'render/index'
root "render#index"
end
- Open the
app/views/render/index.html.erbfile and replace its contents with the following:
<main class="container">
<div class="row text-center justify-content-center">
<div class="col">
<h1 class="display-4">Hello World!</h1>
</div>
</div>
</main>
Verify your changes by returning to localhost:3000 in your browser. If you stopped your local server, restart it first:
$ bin/dev
3. Update your app for Render
Let's prepare your Rails project for production on Render. We'll create a build script for Render to run with each deploy, and we'll update your project to use a Render PostgreSQL database instead of SQLite (if necessary).
Create a build script
Create a file named render-build.sh in your repo's bin directory. Paste the following into it and save:
#!/usr/bin/env bash
# exit on error
set -o errexit
bundle install
bundle exec rails assets:precompile
bundle exec rails assets:clean
Make sure the script is executable before committing it to Git:
$ chmod a+x bin/render-build.sh
Swap SQLite for PostgreSQL (if necessary)
Skip this step if you created your app with the option --database=postgresql.
Update your project to use PostgreSQL with the following command:
$ rails db:system:change --to=postgresql
Alternatively, in your Gemfile, swap the gem for your database driver from sqlite3 to pg.
Before:
gem 'sqlite3'
After:
gem 'pg'
Run bundle install to update your Gemfile.lock, then commit your changes.
4. Deploy to Render
There are two ways to deploy your application on Render: by declaring your services within your repository in a render.yaml file, or by manually setting up each of your services in the Render Dashboard.
Use render.yaml to deploy
- Create a file named
render.yamlat the root of your directory and paste in the YAML content below:
databases:
- name: mysite
databaseName: mysite
user: mysite
plan: free
services:
- type: web
name: mysite
runtime: ruby
plan: free
buildCommand: './bin/render-build.sh'
startCommand: 'bundle exec rails server'
envVars:
- key: DATABASE_URL
fromDatabase:
name: mysite
property: connectionString
- key: RAILS_MASTER_KEY
sync: false
- key: WEB_CONCURRENCY
value: 2
In the Render Dashboard, go to the Blueprint page and click New Blueprint Instance. Select your repository (after giving Render the permission to access it, if you haven't already).
In the deploy window, set the value of the
RAILS_MASTER_KEYto the contents of yourconfig/master.keyfile. Then click Approve.
Deploy manually
Create a new PostgreSQL database on Render.
Create a new web service and point it to your application repository.
Select Ruby for the web service's runtime and set the following properties:
| Property | Value |
|---|---|
| Build Command | ./bin/render-build.sh |
| Start Command | ./bin/rails server |
- Set the following environment variables for the web service:
| Key | Value |
|---|---|
DATABASE_URL |
The internal database URL for the database you created above |
RAILS_MASTER_KEY |
Paste contents of the config/master.key file |
WEB_CONCURRENCY |
2 |
Additional actions
Accessing the Rails console
You can access the Rails console by either using the shell tab in the Render Dashboard and running rails c:
Adding caching
If you want to speed up your Rails app with a cache, see Rails caching with Redis.
Processing tasks in the background
If you want to process asynchronous tasks in the background, see Deploy Rails with Sidekiq on Render.
Setting a Ruby version
See Setting your Ruby Version if you need to customize the version of Ruby used for your app.