Specify dependencies in Ruby

Cloud Run functions written in Ruby use bundler to access dependencies. Dependencies can be downloaded when your function is deployed, or packaged locally alongside your function.

Each function must provide a Gemfile that specifies the functions_framework gem, along with any additional gems needed by the function. Gemfile must be in the same directory as the app.rb file that contains your function code. In addition, your function must provide a lockfile that specifies all the transitive dependencies and their exact versions. This file, Gemfile.lock, is also located in the same directory alongside the Gemfile.

When you deploy your function, Cloud Run functions downloads and installs the dependencies declared in the Gemfile and Gemfile.lock using bundler.

The Gemfile lists the packages required by your function, along with any optional version constraints. For more details, see the Gemfile reference.

The following is an example Gemfile:

source "https://rubygems.org"

gem "functions_framework", "~> 0.7"
gem "google-cloud-storage", "~> 1.29"

You run the following command to install the functions_framework gem and other dependencies:

bundle install

The Gemfile.lock is generated by bundler when it analyzes your dependencies, and freezes the exact versions of each gem to be installed. This ensures, for example, that your function is deployed with the same dependency versions as those used in your local tests. The exact format of the Gemfile.lock is private to bundler and is not meant to be edited by hand.

If you have already used bundler to install your dependencies and run tests locally, you probably already have a Gemfile.lock file present. If not, you can generate one by running:

bundle lock

The Functions Framework is a required dependency for all functions. Although Cloud Run functions installs it on your behalf when the function is created, we recommend that you include it as an explicit dependency for clarity.

If your function relies on private dependencies, we recommend that you mirror functions-framework to your private registry. Include the mirrored functions-framework as a dependency to your function to avoid installing the package from the public internet.

Packaging local dependencies

You can also package and deploy dependencies alongside alongside your function. This approach is useful if your dependency is not available via the rubygems package manager.

To package a gem locally, include it in a directory in your function's directory structure, and provide the path in the dependency's Gemfile entry. The gem directory must include a valid gemspec file, and it must be located within the function's directory hierarchy so that its code is deployed along with your function. For example, you might use a directory structure such as the following:

myfunction/
├── Gemfile
├── Gemfile.lock
├── app.rb
└── my_private_gem/
    ├── lib/
    |   └── my_private_gem.rb
    └── my_private_gem.gemspec

The Gemfile entry might look like this:

source "https://rubygems.org"

gem "functions_framework", "~> 0.7"
gem "my_private_gem", path: "./my_private_gem"

See the Gemfile reference for more discussion about referencing local gem paths.