> ## Documentation Index
> Fetch the complete documentation index at: https://developer.upsun.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Ruby

> Upsun Cloud supports deploying any Ruby application. Your application can use any Ruby application server such as Puma or Unicorn and deploying a Rails or a Sinatra app is very straight forward.


> [!TIP]
>
> You can now use composable image to install runtimes and tools in your application container. To find out more, see the [Composable image](https://developer.upsun.com/docs/configure-apps/app-reference/composable-image.md) topic.

## Supported versions

You can select the major and minor version.

Patch versions are applied periodically for bug fixes and other maintenance updates. By default, you get the latest available patches each time you deploy. If your project has a [maintenance window](https://developer.upsun.com/docs/core-concepts/build-deploy.md#maintenance-window) configured, Upsun Cloud applies patches only during that window. Upsun Cloud may still apply urgent or critical fixes outside of the maintenance window.

### Ruby

* 4.0
* 3.4
* 3.3

> [!NOTE]
>
> These versions are not yet available but are expected to be released soon.
>
> * 4.1

### Specify the language

To use Ruby, specify `ruby` as your [app's `type`](https://developer.upsun.com/docs/configure-apps/app-reference/single-runtime-image#type):


&#x20;   applications:
&#x20;     \# The app's name, which must be unique within the project.
&#x20;     \<APP\_NAME>:
&#x20;       type: 'ruby:\<VERSION\_NUMBER>'
&#x20;&#x20;

For example:


&#x20;   applications:
&#x20;     \# The app's name, which must be unique within the project.
&#x20;     myapp:
&#x20;       type: 'ruby:\{\{version:ruby:latest}}'

## Deprecated versions

The following versions are still available in your projects,
but they're at their end of life and are no longer receiving security updates from upstream.

*No deprecated versions.*

To ensure your project remains stable in the future, switch to a [supported version](#supported-versions).

## Retired versions

The following versions have been retired and are no longer available.
If your project uses a retired version, you must update to a [supported version](#supported-versions).

* 3.2
* 3.1
* 3.0
* 2.7
* 2.6
* 2.5
* 2.4
* 2.3

## Puma based Rails configuration

This example uses Puma to run a Ruby application.
You could use any Ruby application server such as Unicorn.

Configure the `.upsun/config.yaml` file with a few key settings as listed below.
A complete example is included at the end of this section.

1. Specify the language of your application (available versions are listed above):


&#x20;   applications:
&#x20;     \# The app's name, which must be unique within the project.
&#x20;     myapp:
&#x20;       type: 'ruby:\{\{version:ruby:latest}}'

2. Set up environment variables.

   Rails runs by default on a preview environment.
   You can change the Rails/Bundler via those environment variables,
   some of which are defaults on Upsun Cloud.


&#x20;   applications:
&#x20;     \# The app's name, which must be unique within the project.
&#x20;     myapp:
&#x20;       type: 'ruby:\{\{version:ruby:latest}}'
&#x20;       variables:
&#x20;         env:
&#x20;           PIDFILE: "tmp/server.pid" # Allow to start puma directly even if \`tmp/pids\` directory is not created
&#x20;           RAILS\_ENV: "production"
&#x20;           BUNDLE\_WITHOUT: 'development:test'
&#x20;           TARGET\_RUBY\_VERSION: '\~>\{\{version:ruby:latest}}' # this will allow to not fail on PATCH update of the image

The `SECRET_KEY_BASE` variable is generated automatically based on the
[`PLATFORM_PROJECT_ENTROPY`
variable](https://developer.upsun.com/docs/development/variables/use-variables#use-provided-variables) but you can change it.

Based on TARGET\_RUBY\_VERSION, we recommend to set on your Gemfile so next
PATCH release of ruby doesn't fail the build:

```ruby theme={null}
ruby ENV["TARGET_RUBY_VERSION"] || File.read(File.join(File.dirname(__FILE__), ".ruby-version")).strip
```

3. Build your application with the build hook.

   Assuming you have your dependencies stored in the `Gemfile` at [your app root](https://developer.upsun.com/docs/configure-apps/app-reference/single-runtime-image.md#root-directory),
   create a hook like the following:


&#x20;   applications:
&#x20;     \# The app's name, which must be unique within the project.
&#x20;     myapp:
&#x20;       type: 'ruby:\{\{version:ruby:latest}}'
&#x20;       ...
&#x20;       hooks:
&#x20;         build: |
&#x20;           set -e
&#x20;           bundle install
&#x20;           bundle exec rails assets:precompile
&#x20;         deploy: bundle exec rake db:migrate

These are installed as your project dependencies in your environment.
You can also use the `dependencies` key to install global dependencies.
These can be Ruby, Python, NodeJS, or PHP libraries.

If you have assets, it's likely that you need NodeJS/yarn.


&#x20;   applications:
&#x20;     \# The app's name, which must be unique within the project.
&#x20;     myapp:
&#x20;       type: 'ruby:\{\{version:ruby:latest}}'
&#x20;       ...
&#x20;       dependencies:
&#x20;         nodejs:
&#x20;           yarn: "\*"

4. Configure the command to start serving your application (this must be a foreground-running process) under the `web` section:


&#x20;   applications:
&#x20;     \# The app's name, which must be unique within the project.
&#x20;     myapp:
&#x20;       type: 'ruby:\{\{version:ruby:latest}}'
&#x20;       ...
&#x20;       web:
&#x20;         upstream:
&#x20;           socket\_family: unix
&#x20;         commands:
&#x20;           \# for puma
&#x20;           start: "bundle exec puma -b unix://\$SOCKET"
&#x20;           \# for unicorn
&#x20;           \# start: "bundle exec unicorn -l \$SOCKET"

This assumes you have Puma as a dependency in your Gemfile:

```ruby theme={null}
gem "puma", ">= 5.0"
```

5. Define the web locations your application is using:


&#x20;   applications:
&#x20;     \# The app's name, which must be unique within the project.
&#x20;     myapp:
&#x20;       type: 'ruby:\{\{version:ruby:latest}}'
&#x20;       ...
&#x20;       web:
&#x20;         locations:
&#x20;           "/":
&#x20;             root: "public"
&#x20;             passthru: true
&#x20;             expires: 1h
&#x20;             allow: true

This configuration sets the web server to handle HTTP requests at `/static`
to serve static files stored in `/app/static/` folder.
Everything else is forwarded to your application server.

6. Create any Read/Write mounts.

   The root file system is read only.
   You must explicitly describe writable mounts.


&#x20;   applications:
&#x20;     \# The app's name, which must be unique within the project.
&#x20;     myapp:
&#x20;       type: 'ruby:\{\{version:ruby:latest}}'
&#x20;       ...
&#x20;       mounts:
&#x20;         "/log":
&#x20;           source: tmp
&#x20;           source\_path: log
&#x20;         "/storage":
&#x20;           source: storage
&#x20;           source\_path: storage
&#x20;         "/tmp":
&#x20;           source: tmp
&#x20;           source\_path: tmp

This setting allows your application writing temporary files to `/app/tmp`,
logs stored in `/app/log`, and active storage in `/app/storage`.

You can define other read/write mounts (your application code itself being deployed to a read-only file system).
Note that the file system is persistent and when you backup your cluster these mounts are also backed up.

7. Then, setup the routes to your application in `.upsun/config.yaml`.

   ```yaml .upsun/config.yaml theme={null}
   applications:
     ...

   routes:
     "https://{default}/":
       type: upstream
       upstream: "myapp:http"
   ```

### Complete app configuration

Here is a complete `.upsun/config.yaml` file:


&#x20;   \# The name of the app, which must be unique within a project.
&#x20;   applications:
&#x20;     myapp:
&#x20;       type: 'ruby:\{\{version:ruby:latest}}'

&#x20;       dependencies:
&#x20;         nodejs:
&#x20;           yarn: "\*"

&#x20;       relationships:
&#x20;         mysql:

&#x20;       variables:
&#x20;         env:
&#x20;           BUNDLE\_CACHE\_ALL: '1' # Default, Cache all gems, including path and git gems.
&#x20;           BUNDLE\_CLEAN: '1' # /!\ if you are working with Ruby\<2.7 this doesn't work well, but should be safe on modern Rubies.
&#x20;           BUNDLE\_DEPLOYMENT: '1' # Default, Disallow changes to the Gemfile.
&#x20;           BUNDLE\_ERROR\_ON\_STDERR: '1' # Default.
&#x20;           BUNDLE\_WITHOUT: 'development:test'
&#x20;           PIDFILE: "tmp/server.pid" # Allow to start puma directly even if \`tmp/pids\` directory is not created
&#x20;           DEFAULT\_BUNDLER\_VERSION: "2.5.14" # In case none is mentioned in Gemfile.lock
&#x20;           EXECJS\_RUNTIME: 'Node' # If you need one on your assets https\://github.com/rails/execjs#readme
&#x20;           NODE\_ENV: 'production'
&#x20;           NODE\_VERSION: v14.17.6
&#x20;           NVM\_VERSION: v0.38.0
&#x20;           RACK\_ENV: 'production'
&#x20;           RAILS\_ENV: 'production'
&#x20;           RAILS\_LOG\_TO\_STDOUT: '1' # Default
&#x20;           RAILS\_TMP: '/tmp' # Default

&#x20;       hooks:
&#x20;         build: |
&#x20;           set -e

&#x20;           echo "Installing NVM \$NVM\_VERSION"
&#x20;           unset NPM\_CONFIG\_PREFIX
&#x20;           export NVM\_DIR="\$PLATFORM\_APP\_DIR/.nvm"
&#x20;           \# install.sh will automatically install NodeJS based on the presence of \$NODE\_VERSION
&#x20;           curl -o- https\://raw\.githubusercontent.com/nvm-sh/nvm/\$NVM\_VERSION/install.sh | bash
&#x20;           \[ -s "\$NVM\_DIR/nvm.sh" ] && \\. "\$NVM\_DIR/nvm.sh"

&#x20;           \# we install the bundled bundler version and fallback to a default (in env vars above)
&#x20;           export BUNDLER\_VERSION="\$(grep -A 1 "BUNDLED WITH" Gemfile.lock | tail -n 1)" || "\$DEFAULT\_BUNDLER\_VERSION"
&#x20;           echo "Install bundler \$BUNDLER\_VERSION"
&#x20;           gem install --no-document bundler -v \$BUNDLER\_VERSION

&#x20;           echo "Installing gems"
&#x20;           \# We copy the bundle directory to the Upsun Cloud cache directory for
&#x20;           \# safe keeping, then restore from there on the next build. That allows
&#x20;           \# bundler to skip downloading code it doesn't need to.
&#x20;           \[ -d "\$PLATFORM\_CACHE\_DIR/bundle" ] && \\
&#x20;               rsync -az --delete "\$PLATFORM\_CACHE\_DIR/bundle/" vendor/bundle/
&#x20;           mkdir -p "\$PLATFORM\_CACHE\_DIR/bundle"
&#x20;           bundle install
&#x20;           \# synchronize updated cache for next build
&#x20;           \[ -d "vendor/bundle" ] && \\
&#x20;               rsync -az --delete vendor/bundle/ "\$PLATFORM\_CACHE\_DIR/bundle/"

&#x20;           \# precompile assets
&#x20;           echo "Precompiling assets"
&#x20;           \# We copy the webpacker directory to the Upsun Cloud cache directory for
&#x20;           \# safe keeping, then restore from there on the next build. That allows
&#x20;           \# bundler to skip downloading code it doesn't need to.
&#x20;           \# https\://guides.rubyonrails.org/asset\_pipeline.html
&#x20;           mkdir -p "\$PLATFORM\_CACHE\_DIR/webpacker"
&#x20;           mkdir -p "\$RAILS\_TMP/cache/webpacker"
&#x20;           \[ -d "\$PLATFORM\_CACHE\_DIR/webpacker" ] && \\
&#x20;               rsync -az --delete "\$PLATFORM\_CACHE\_DIR/webpacker/" \$RAILS\_TMP/cache/webpacker/
&#x20;           \# We dont need secret here https\://github.com/rails/rails/issues/32947
&#x20;           SECRET\_KEY\_BASE=1 bundle exec rails assets:precompile
&#x20;           rsync -az --delete \$RAILS\_TMP/cache/webpacker/ "\$PLATFORM\_CACHE\_DIR/webpacker/"
&#x20;         deploy: bundle exec rake db:migrate

&#x20;       mounts:
&#x20;         "/log":
&#x20;           source: tmp
&#x20;           source\_path: log
&#x20;         "/storage":
&#x20;           source: storage
&#x20;           source\_path: storage
&#x20;         "/tmp":
&#x20;           source: tmp
&#x20;           source\_path: tmp

&#x20;       web:
&#x20;         upstream:
&#x20;           socket\_family: unix
&#x20;         commands:
&#x20;           \# for puma
&#x20;           start: "bundle exec puma -b unix://\$SOCKET"
&#x20;           \# for unicorn
&#x20;           \# start: "bundle exec unicorn -l \$SOCKET"

&#x20;         locations:
&#x20;           "/":
&#x20;             root: "public"
&#x20;             passthru: true
&#x20;             expires: 1h
&#x20;             allow: true

&#x20;   routes:
&#x20;     "https\://\{default}/":
&#x20;       type: upstream
&#x20;       upstream: "myapp:http"

&#x20;   services:
&#x20;     ...

## Configuring services

This example assumes there is a MySQL instance.
To configure it, [create a service](https://developer.upsun.com/docs/add-services) such as the following:


&#x20;   applications:
&#x20;     ...

&#x20;   routes:
&#x20;     ...

&#x20;   services:
&#x20;     mysql:
&#x20;       type: mysql:\{\{version:mariadb:latest}}

## Connecting to services

Once you have a service, link to it in your [app configuration](https://developer.upsun.com/docs/configure-apps):


&#x20;   applications:
&#x20;     myapp:
&#x20;       type: 'ruby:\{\{version:ruby:latest}}'
&#x20;       relationships:
&#x20;         mysql:
&#x20;       \[...]

&#x20;   routes:
&#x20;     \[...]

&#x20;   services:
&#x20;     mysql:
&#x20;       type: mysql:\{\{version:mariadb:latest}}

By using the following Ruby function calls, you can obtain the database details.

```ruby theme={null}
require "base64"
require "json"
relationships= JSON.parse(Base64.decode64(ENV['PLATFORM_RELATIONSHIPS']))
```

This should give you something like the following:

```json theme={null}
{
  "mysql" : [
    {
      "path" : "main",
      "query" : {
        "is_master" : true
      },
      "port" : 3306,
      "username" : "user",
      "password" : "",
      "host" : "mysql.internal",
      "ip" : "246.0.241.50",
      "scheme" : "mysql"
    }
  ]
}
```

For Rails, you can use the standard Rails `config/database.yml` with the values found with the snippet provided before.

## Other tips

* To speed up boot you can use the [Bootsnap gem](https://github.com/Shopify/bootsnap)
  and configure it with the local `/tmp`:

  ```ruby config/boot.rb theme={null}
  Bootsnap.setup(cache_dir: "/tmp/cache")
  ```

* For garbage collection tuning, you can read [this article](https://shopify.engineering/17489064-tuning-rubys-global-method-cache)
  and look for [discourse configurations](https://github.com/discourse/discourse_docker/blob/b259c8d38e0f42288fd279c9f9efd3cefbc2c1cb/templates/web.template.yml#L8)

* New images are released on a regular basis to apply security patches. While the minor version will not change (as you are specifying it in the `type` property), the patch version will be updated. You may encounter this kind of error:

  ```
  bundler: failed to load command: puma (/app/vendor/bundle/ruby/3.2.0/bin/puma)
  /app/.global/gems/bundler-2.4.22/lib/bundler/definition.rb:447:in `validate_ruby!': Your Ruby version is 3.2.9, but your Gemfile specified 3.2.8 (Bundler::RubyVersionMismatch)
  ```

  To avoid issues when such updates are performed, use

  ```ruby theme={null}
  ruby ENV["TARGET_RUBY_VERSION"] || File.read(File.join(File.dirname(__FILE__), ".ruby-version")).strip
  ```

  in your `Gemfile`, where `TARGET_RUBY_VERSION` has been defined as above.

> [!NOTE]
>
> To deploy a Ruby project, create a new project from the
> [Upsun Console](https://console.upsun.com/projects/create-project)
> and select a template, or push your existing code.

## Troubleshooting

By default, deployments have `BUNDLE_DEPLOYMENT=1` to ensure projects have a `Gemfile.lock` file.
This is safer for version yank issues and other version upgrade breakages.

You may encounter an error like the following during a build:

```txt {no-copy="true"} theme={null}
W: bundler: failed to load command: rake (/app/.global/bin/rake)
W: /app/.global/gems/bundler-2.3.5/lib/bundler/resolver.rb:268:in `block in verify_gemfile_dependencies_are_found!': Could not find gem 'rails (= 5.2.6)' in locally installed gems. (Bundler::GemNotFound)
```

To resolve this error:

1. Run `bundle install` with the same `ruby` and `bundler` versions defined in your `.upsun/config.yaml` file.
2. Push the `Gemfile.lock` to your repository.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.