Running Rails 5 on the Google App Engine flexible environment

It's easy to get started developing Ruby on Rails apps that run on the Google App Engine flexible environment, and because the apps you create will run on the same infrastructure that powers all of Google's products, you can be confident that they will scale to serve all of your users, whether there are a few or millions of them.

This tutorial assumes you are familiar with Rails web development. It walks you through deploying a new Rail application.

This tutorial requires Ruby 2.3.3 or newer.

Before you begin

Check off each step as you complete it.

  1. check_box_outline_blank check_box Create a project in the Google Cloud Platform Console.
    If you haven't already created a project, create one now. Projects enable you to manage all Google Cloud Platform resources for your app, including deployment, access control, billing, and services.
    1. Open the Cloud Platform Console.
    2. In the drop-down menu at the top, select Create a project.
    3. Click Show advanced options. Under App Engine location, select a United States location.
    4. Give your project a name.
    5. Make a note of the project ID, which might be different from the project name. The project ID is used in commands and in configurations.
  2. check_box_outline_blank check_box Enable billing for your project, and sign up for a free trial.

    If you haven't already enabled billing for your project, enable billing now, and sign up for a free trial. Enabling billing allows the application to consume billable resources such as running instances and storing data. During your free trial period, you won't be billed for any services.

  3. check_box_outline_blank check_box Install the Google Cloud SDK.

    If you haven't already installed the Google Cloud SDK, install and initialize the Google Cloud SDK now. The SDK contains tools and libraries that enable you to create and manage resources on Google Cloud Platform.

Set up your local environment for Rails


Before you begin, take the following steps:

  1. Install Ruby version 2.3.3 or greater.

  2. Install the Rails 5 gem.

  3. Install the Bundler gem.

Alternatively: You can use Google Cloud Shell, which comes with Ruby, Rails, and Cloud SDK already installed.

For additional information on installing Rails and its dependencies, see the official Getting Started with Rails guide.

After you've completed the prerequisites, you can create and deploy a Rails app. The following sections guide you through configuring, running, and deploying an application.

Create a new app

To begin, run the rails new command to scaffold a new Rails sample application:

rails new appengine_example

Go to the directory that contains the sample code:

cd appengine_example

Run the application locally

To run the new Rails application on your local computer:

  1. Install dependencies using Bundler:

     bundle install
  2. Start a local web server:

     bundle exec rails server
  3. In your web browser, browse to http://localhost:3000/.

You can see the "Yay! You’re on Rails!" message from the sample app displayed in the page.

Screenshot of new Rails app running

Add a simple page

Now add a Welcome page to the generated Rails application. You can generate the scaffolding for a new page by using rails generate. The following command creates a new Rails controller named WelcomeController with an index action:

bundle exec rails generate controller Welcome index

This command generates the following new directories and files by default for the new WelcomeController controller and index action.

create  app/controllers/welcome_controller.rb
 route  get 'welcome/index'
invoke  erb
create    app/views/welcome
create    app/views/welcome/index.html.erb
invoke  test_unit
create    test/controllers/welcome_controller_test.rb
invoke  helper
create    app/helpers/welcome_helper.rb
invoke    test_unit
invoke  assets
invoke    coffee
create      app/assets/javascripts/
invoke    scss
create      app/assets/stylesheets/welcome.scss

Next, open the file app/views/welcome/index.html.erb, and you should see the following boilerplate HTML:

<p>Find me in app/views/welcome/index.html.erb</p>

Modify the file however you like. For example, you can use the following content:

<p>This is a home page for a new Rails App on Google Cloud Platform!</p>

Finally, you will set the index controller action as the root action for Rails, and whenever a user visits the Rails app, they will see your Welcome page!

Next, open the file config/routes.rb to see the following generated content:

Rails.application.routes.draw do
  get 'welcome/index'

  # For details on the DSL available within this file, see

Modify this file by adding root 'welcome#index':

Rails.application.routes.draw do
  get 'welcome/index'

  # For details on the DSL available within this file, see
  root 'welcome#index'

Save the file and close it. Test the Rails app as you did before and make sure you like what you see.

Deploy the app to the App Engine flexible environment

App Engine flexible environment uses a file called app.yaml. to describe an application's deployment configuration. If this file is not present, the gcloud command line tool will try to guess the deployment configuration. However, it is a good idea to provide this file because Rails requires a secret key in a production environment.

To configure the sample application for deployment to App Engine, create a new file named app.yaml at the root of your sample application directory and add the following:

entrypoint: bundle exec rackup --port $PORT
env: flex
runtime: ruby

Configure Rails secret key

When a Rails app is deployed in the production environment, the environment variable SECRET_KEY_BASE is expected to be set to a secret key that is used to protect user session data. This environment variable is read in the config/secrets.yml file.

First, generate a new secret key:

bundle exec rails secret

Copy the generated secret key to your clipboard. You will use the secret key in the next step.

Next, open the file app.yaml that you created earlier, and add an env_variables section. The env_variables will set environment variables in the production environemnt in App Engine flexible environment. Your app.yaml should look like the example below, with [SECRET_KEY] replaced with the secret key in your clipboard.

entrypoint: bundle exec rackup --port $PORT
env: flex
runtime: ruby


Set up an App Engine flexible environment app

If this is the first time you deploy an app, you will need to create an App Engine flexible environment application. This will help you select the region in which you want to run the Rails app.

Enter this command to create an App Engine app:

gcloud app create

A prompt is displayed with a list of regions. Select a region that supports App Engine flexible environment for Ruby apps.

You can read more about Regions and Zone.

Deploy to App Engine flexible environment

Before you deploy, precompile your Rails assets by running the following command:

RAILS_ENV=production bundle exec rails assets:precompile

After the assets finish compiling, you can proceed to deploy the sample by using the following command:

gcloud app deploy

Wait for the message that notifies you that the update has completed. This can take several minutes.

Access the deployed Rails app

In your web browser, browse to:


or use the command:

gcloud app browse

You will see the following content.

Screenshot of new Rails app running

This time, your request is served by the Rails app running in the App Engine flexible environment.

This command deploys the application as described in app.yaml and sets the newly deployed version as the default version, causing it to serve all new traffic. As the app deploys, you might see several repeated messages while the platform checks whether the app is serving. This is normal. Wait for the message that notifies you that the update of the app is complete.

If you update your app, you can deploy the updated version by entering the same command you used to deploy the app the first time. The new deployment creates a new version of your app and promotes it to the default version. The older versions of your app will remain, as do their associated VM instances. Be aware that all of these app versions and VM instances are billable resources.

Read App Engine logs

Now that you have deployed your Rails app, you may want to read the logs. You can read the app logs by using the Logs Viewer located in the Cloud Console, or by using gcloud app logs read. You can learn more about reading logs using the Cloud SDK.

Clean up resources

After you've finished the Running Rails 5 in the App Engine tutorial, you can clean up the resources you created on Google Cloud Platform so you won't be billed for them in the future. The following sections describe how to delete or turn off these resources.

Delete project

The easiest way to eliminate billing is to delete the project you created for the tutorial.

To delete the project:

  1. In the Cloud Platform Console, go to the Projects page.

    Go to the Projects page

  2. In the project list, select the project you want to delete and click Delete project. After selecting the checkbox next to the project name, click
      Delete project
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

Delete an App Engine version

To delete an app version:

  1. In the Cloud Platform Console, go to the App Engine Versions page.

    Go to the Versions page

  2. Click the checkbox next to the non-default app version you want to delete.
  3. Click the Delete button at the top of the page to delete the app version.

What's next

Send feedback about...