Skip to main content
← Back to workflows

How to Install Ruby in GitHub Actions

ruby-setup-ruby -
GitHub Action
Use downloadAndExtract() for truffleruby+graalvm too
789
Contributors
Contributor - eregonContributor - MSP-Greg
Categories
CICUBE ANALYTICS INSIGHTS
Engineering Velocity: 25% Team Time Lost to CI Issues
View Platform →
3.5h
Time Saved/Dev/Week
40%
Faster Releases
Click for next insight
Usage
name: 'Usage of setup-ruby GitHub Action'
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

setup-ruby logo

setup-ruby

An action to download a prebuilt Ruby and add it to the PATH in 5 seconds


What is Ruby Setup GitHub Action?

The Setup Ruby GitHub Action downloads a prebuilt Ruby, JRuby, or TruffleRuby and adds it to the PATH. This action is very efficient and takes about 5 seconds to download, extract, and add the given Ruby to the PATH. No extra packages need to be installed.

Important: Prefer ruby/setup-ruby@v1. If you pin to a commit or release, only the Ruby versions available at the time of the commit will be available, and you will need to update it to use newer Ruby versions.

Single Job Example

This example sets up a workflow to install Ruby and run a bundle exec rake command.

name: My workflow
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1
with:
ruby-version: '3.3' # Not needed with a .ruby-version file
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
- run: bundle exec rake

Matrix of Ruby Versions

This matrix tests all stable releases and head versions of MRI, JRuby, and TruffleRuby on Ubuntu and macOS.

name: My workflow
on: [push, pull_request]
jobs:
test:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest]
ruby: ['2.7', '3.0', '3.1', '3.2', '3.3', head, jruby, jruby-head, truffleruby, truffleruby-head]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
- run: bundle exec rake

Matrix of Gemfiles

This example sets up a matrix of Gemfiles to test with different versions of Ruby.

name: My workflow
on: [push, pull_request]
jobs:
test:
strategy:
fail-fast: false
matrix:
gemfile: [rails5, rails6]
runs-on: ubuntu-latest
env:
BUNDLE_GEMFILE: ${{ github.workspace }}/gemfiles/${{ matrix.gemfile }}.gemfile
steps:
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1
with:
ruby-version: '3.3'
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
- run: bundle exec rake

Supported Version Syntax

Various ways to specify Ruby versions.

with:
ruby-version: '2.6.5' # Specific version
ruby-version: '2.6' # Short version, latest 2.6.x
ruby-version: 'ruby' # Latest stable MRI
ruby-version: 'jruby' # Latest stable JRuby
ruby-version: 'truffleruby' # Latest stable TruffleRuby

Working Directory

Set the working directory if .ruby-version, .tool-versions, or Gemfile.lock are not at the root of the repository.

with:
working-directory: path/to/subdirectory

RubyGems Version

Optionally customize the RubyGems version.

with:
rubygems: '3.0.0'

Bundler Version

Bundler is installed based on the Gemfile.lock or the version bundled with Ruby. This can be customized.

with:
bundler: '2.1.4'

Caching bundle install Automatically

Automatically run bundle install and cache the result to speed up subsequent runs.

- uses: ruby/setup-ruby@v1
with:
bundler-cache: true

Dealing with a Corrupted Cache

In some scenarios, it may be necessary to ignore the cache and rebuild all gems.

- uses: ruby/setup-ruby@v1
with:
bundler-cache: true
cache-version: 1

Caching bundle install Manually

It is possible to cache gems manually, but using bundler-cache: true is recommended for simplicity and reliability.

- uses: actions/cache@v4
with:
path: vendor/bundle
key: ${{ runner.os }}-gem-${{ hashFiles('**/Gemfile.lock') }}
restore-keys: |
${{ runner.os }}-gem-