The Ruby Runtime

Overview

The Ruby runtime is the software stack responsible for installing your application code and its dependencies and running your application. The standard runtime is declared in app.yaml as runtime: ruby:

runtime: ruby
env: flex

Runtimes in the flexible environment are built using Docker. The Ruby runtime is based on Debian Jessie. The source code for the Ruby runtime is publicly available on GitHub.

Interpreter

The default interpreter is currently Ruby 2.3.1. You can specify a different Ruby version by writing the version name to a .ruby-version file in your application directory. For example:

2.2.5

When a .ruby-version file is present, the runtime attempts to download and install the specified version of Ruby. If the version cannot be satisfied, you will receive an error message when deploying your application.

Dependencies

The runtime looks for a Gemfile file in your application's source directory and uses Bundler to install any dependencies before starting your application. For more information on declaring and managing packages, see Using Ruby Libraries.

Using C libraries with Ruby

To enable the use of Ruby libraries that require C extensions, the headers for the current Ruby version and the following Debian packages are pre-installed on the system.

These packages allow the installation of most popular Ruby libraries. If your application requires additional operating-system level dependencies, you will need to use a custom runtime based on this runtime to install the appropriate packages.

Application startup

The runtime starts your application using the entrypoint defined in app.yaml. The entrypoint should start a process that responds to HTTP requests on the port defined by the environment variable PORT. For example:

entrypoint: rails server -p $PORT

Most web applications use a Rack-supported web server such as Puma, Unicorn or Thin.

Before you can use one of these servers, you must add them as a dependency in your application's Gemfile. The runtime ensures that all dependences are installed before your entrypoint is called.

source "https://rubygems.org"

gem "rack"
gem "puma"

An example entrypoint using puma for a Rails application:

entrypoint: rails server Puma -p $PORT

An example entrypoint using puma for any Rack application:

entrypoint: bundle exec rackup -s Puma -p $PORT

For applications that can handle requests without a WSGI server, you can just execute a ruby script:

entrypoint: bundle exec ruby app.rb

Extending the runtime

The standard Ruby runtime can be used to create a custom runtime. Custom runtimes are configured via Dockerfiles. You can generate a Dockerfile based on the standard Ruby runtime using gen-config:

gcloud beta app gen-config --custom

You can then customize the Dockerfile and .dockerignore as desired. Finally, you will need to specify runtime: custom instead of runtime: ruby in app.yaml.

Environment variables

The following environment variables are set by the runtime environment:

Environment Variable Description
GAE_INSTANCE The name of the current instance.
GAE_MEMORY_MB The amount of memory available to the application process.
GAE_SERVICE The service name specified in your application's app.yaml file, or if no service name is specified, it is set to default.
GAE_VERSION The version label of the current application.
GCLOUD_PROJECT The Project ID associated with your application, which is visible in the Google Cloud Platform Console
PORT The port that will receive HTTP requests.
RACK_ENV Set to production.
RAILS_ENV Set to production.
RAILS_SERVE_STATIC_FILES Set to true.

You can set additional environment variables with app.yaml.

Metadata server

Each instance of your application can use the Compute Engine metadata server to query information about the instance, including the its host name, external IP address, instance ID, custom metadata, and service account information. App Engine does not allow you to set custom metadata for each instance, but you can set project-wide custom metadata and read it from your App Engine and Compute Engine instances.

This example function uses the metadata server to get the external IP address of the instance:

require "sinatra"
require "net/http"

get "/" do
  uri = URI.parse(
    "http://metadata.google.internal/computeMetadata/v1" +
    "/instance/network-interfaces/0/access-configs/0/external-ip"
  )

  request = Net::HTTP::Get.new uri.path
  request.add_field "Metadata-Flavor", "Google"

  http = Net::HTTP.new(uri.host, uri.port)

  response = http.request request

  "External IP: #{response.body}"
end

Send feedback about...

App Engine flexible environment for Ruby docs