This guide demonstrates how to set up a local [Ruby on Rails](https://rubyonrails.org/) 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](/content/docs/postgresql/index.html). These same steps work for both Rails 6 and Rails 7.

**Deploying Rails 8?** See [this quickstart](/content/docs/deploy-rails-8/index.html).

## 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](/content/docs/deploy-rails-6-7#3-update-your-app-for-render/index.html).

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](https://guides.rubyonrails.org/getting_started.html#creating-a-new-rails-project-installing-rails) installed (Ruby, Node.js and Yarn 1.x).

```shell
$ gem install rails
```

We're using Rails 7.1 in this tutorial, so verify that you have the correct version installed:

```shell
$ 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.

1. 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:

```shell
$ 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.

2. Create local PostgreSQL databases for your app:

```shell
$ 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:

```shell
$ 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](https://guides.rubyonrails.org/getting_started.html) guide for more on creating Rails apps.

1. To create a new Rails controller, you run the _controller generator_. Set the controller's name to `Render` and set up an action named `index` by running the following command:

```shell
$ 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
```

2. Open the `config/routes.rb` file and add the following line:

```ruby
Rails.application.routes.draw do
      get 'render/index'
      root "render#index"
end
```

3. Open the `app/views/render/index.html.erb` file and replace its contents with the following:

```erb
<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:

```shell
$ 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](/content/docs/postgresql/index.html) 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:

```bash
#!/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:

```shell
$ 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:

```shell
$ rails db:system:change --to=postgresql
```

Alternatively, in your `Gemfile`, swap the gem for your database driver from `sqlite3` to `pg`.

**Before:**

```ruby
gem 'sqlite3'
```

**After:**

```ruby
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](/content/docs/infrastructure-as-code/index.html) in a `render.yaml` file, or by manually setting up each of your services in the [Render Dashboard](https://dashboard.render.com/).

### Use `render.yaml` to deploy

1. Create a file named `render.yaml` at the root of your directory and paste in the YAML content below:

```yaml
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
```

2. In the Render Dashboard, go to the [Blueprint](https://dashboard.render.com/blueprints) page and click **New Blueprint Instance**. Select your repository (after giving Render the permission to access it, if you haven't already).

3. In the deploy window, set the value of the `RAILS_MASTER_KEY` to the contents of your `config/master.key` file. Then click **Approve**.

### Deploy manually

1. Create a new [PostgreSQL database](/content/docs/postgresql/index.html) on Render.

2. Create a new [web service](/content/docs/web-services/index.html) and point it to your application repository.

3. 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` |

4. 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](https://dashboard.render.com/) and running `rails c`:

### Adding caching

If you want to speed up your Rails app with a cache, see [Rails caching with Redis](/content/docs/rails-caching-redis/index.html).

### Processing tasks in the background

If you want to process asynchronous tasks in the background, see [Deploy Rails with Sidekiq on Render](/content/docs/deploy-rails-sidekiq/index.html).

### Setting a Ruby version

See [Setting your Ruby Version](/content/docs/ruby-version/index.html) if you need to customize the version of Ruby used for your app.
