Ruby

4 minute read

Versions and setup

CodeShip uses RVM to manage Ruby and JRuby versions. 2.7.8 is the default version.

The following Ruby versions are preinstalled:

jruby-9.2.21.0 [ x86_64 ] jruby-9.3.13.0 [ x86_64 ] jruby-9.4.5.0 [ x86_64 ] ruby-2.4.10 [ x86_64 ] ruby-2.5.9 [ x86_64 ] ruby-2.6.10 [ x86_64 ] ruby-2.7.0 [ x86_64 ] ruby-2.7.1 [ x86_64 ] ruby-2.7.2 [ x86_64 ] ruby-2.7.3 [ x86_64 ] ruby-2.7.4 [ x86_64 ] ruby-2.7.5 [ x86_64 ] ruby-2.7.6 [ x86_64 ] ruby-2.7.7 [ x86_64 ] =* ruby-2.7.8 [ x86_64 ] ruby-3.0.0 [ x86_64 ] ruby-3.0.1 [ x86_64 ] ruby-3.0.2 [ x86_64 ] ruby-3.0.3 [ x86_64 ] ruby-3.0.4 [ x86_64 ] ruby-3.0.5 [ x86_64 ] ruby-3.0.6 [ x86_64 ] ruby-3.1.0 [ x86_64 ] ruby-3.1.1 [ x86_64 ] ruby-3.1.2 [ x86_64 ] ruby-3.1.3 [ x86_64 ] ruby-3.1.4 [ x86_64 ] ruby-3.2.0 [ x86_64 ] ruby-3.2.1 [ x86_64 ] ruby-3.2.2 [ x86_64 ] ruby-3.2.3 [ x86_64 ] ruby-3.3.0 [ x86_64 ] # => - current # =* - current && default # * - default

Specifying version

You can change the version you want to use by running rvm use RUBY_VERSION --install in your Setup Commands. We do not automatically load the Ruby version from your Gemfile. For example:

rvm use 3.2.3 --install

Most Ruby versions are preinstalled, but if you need an older version the --install flag will automatically install the version for you at build time.

Using a .ruby-version file

You can also use your .ruby-version file on CodeShip. The .ruby-version file lives in the project root and its content is just your Ruby version, for example: 3.2.3. You can read the Ruby version to use from that file:

rvm use $(cat .ruby-version) --install

One use case is that you can change your Ruby version for different branches.

JRuby

If you need to install a version of JRuby that is not already installed by default you can do so with:

rvm use jruby-JRUBY_VERSION --install

YJIT support

YJIT support is compiled in versions 3.1, 3.2 and 3.3.

You can check that a version is compiled with YJIT with:

$ rvm use 3.3.0 $ ruby -v --yjit ruby 3.3.0 (2023-12-25 revision 5124f9ac75) +YJIT [x86_64-linux]

Note the +YJIT designation.

Learn more about YJIT and potential performance gains.

Dependencies

You can install dependencies using Bundler in your setup commands:

gem install bundler -v BUNDLER_VERSION bundle install

In some cases you may need to first update RubyGems to the latest version with:

gem update --system

Note, beginning in RubyGems 3.5.0, support for Ruby 2.6 and 2.7 was dropped so you can update to the last supported version with:

gem update --system 3.4.22

Dependency Cache

CodeShip automatically configures bundler to use the $HOME/cache/bundler directory, which we save between builds to optimize build performance. You can read this article to learn more about the dependency cache and how to clear it.

Frameworks And Testing

Our Ruby support includes Ruby itself, Rails, Sinatra and most other frameworks.

We also support all Ruby based test frameworks like RSpec, Cucumber and Minitest.

Capybara is also supported out of the box with the selenium-webdriver, capybara-webkit or the poltergeist driver for PhantomJS.

Parallel Testing

If you are running parallel test pipelines, you will want to separate your RSpec tests into groups and call a group specifically in each pipeline. For instance:

Pipeline 1:

rspec spec/spec_1

Pipeline 2:

rspec spec/spec_2

Parallelization Gems

In addition to parallelizing your tests explicitly with parallel pipelines, there are a couple Rails gems that are popular ways to parallelize within your codebase.

While we do not officially support or integrate with these modules, many CodeShip users find success speeding their tests up by using them. Note that it is possible for these gems to cause resource and build failure issues.

Notes And Known Issues

Nokogiri

Nokogiri might fail to compile with the bundled libxml and libxslt libraries. To install the gem you need to use the system libraries instead.

The error may look like this:

Fetching nokogiri 1.8.2
Installing nokogiri 1.8.2 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

Fix it by adding this command before bundle install:

bundle config build.nokogiri --use-system-libraries

Run With Bundle Exec

Make sure to run your commands with bundle exec (e.g. bundle exec rspec) so all commands you run are executed with the versions of the Ruby gems you configured in your Gemfile.lock.

Can Not Find Gem In Sources

Sometimes you might see errors like the following:

Could not find safe_yaml-0.9.2 in any of the sources

Please make sure the version of the gem you want to install wasn’t removed from RubyGems.

RVM Requires Curl

If you are manually installing a Ruby version with RVM you may encounter the following error:

RVM requires 'curl'. Install 'curl' first and try again.

This error typically occurs when the NPM package node-which is installed. You can check if it is present in the build with:

$ which which
node_modules/.bin/which

To workaround this, temporarily remove the NPM version of which, then run your rvm command. After that completes you can let the package reinstall with either npm install or yarn install. To remove it, add this command at the start of your Setup Steps:

rm -f node_modules/.bin/which

Custom Bundler Version

Each preinstalled Ruby version will include a recent version of Bundler by default. If you have a situation where you need to install a specific Bundler version you can do so by adding the following commands to your build after setting the Ruby version:

gem uninstall -x -a bundler
rvm @global do gem uninstall -x -a bundler
gem install bundler -v BUNDLER_VERSION

FreeTDS

FreeTDS 1.1.6 is installed by default. If your project requires a newer version you can install it with a script by adding this command to your setup steps:

\curl -sSL https://raw.githubusercontent.com/codeship/scripts/master/packages/freetds.sh | bash -s

Following the install add this command before running bundle install:

bundle config build.tiny_tds --with-freetds-lib=$HOME/cache/freetds/lib --with-freetds-include=$HOME/cache/freetds/include

Puma

The Puma gem will fail to install by default if the version is older than 3.7. Older Puma versions use OpenSSL 1.0, but Ubuntu Focal includes OpenSSL 1.1 by default. You can either upgrade Puma to at least 3.7 or revert to the older OpenSSL version.

The error may look like this:

Fetching puma 3.6.0
Installing puma 3.6.0 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

Revert to OpenSSL 1.0 by adding these commands before bundle install:

sudo apt-get update
sudo apt-get install -y libssl1.0-dev