Creating a new Ruby-based repo: a reminder

So, you need a new repo using Ruby language. Just remember to follow these simple rules and everything is going to be OK.


Keeping this as a note to myself. A cheatsheet, if you must.

This is a step-by-step guide which I’ve made for myself while trying to set up a Ruby installation on a fresh MacBook Pro M1 2020. It may or may not be relevant to your setup.

This is needed if you use CocoaPods in your iOS project, or if you use Jekyll as your static site generator of choice.

Short list collapsed
  1. install rvm (see https://rvm.io),
  2. add .ruby-version,
  3. install ruby via rvm,
  4. add .ruby-gemset,
  5. rvm use .,
  6. gem install bundler,
  7. add Gemfile,
  8. bundle install,
  9. bundle exec <command>.

Step 1: Install RVM/rbenv

Each Ruby-based app/repo needs a Ruby version manager. Period. I’ve been there and seen how things break with time if you don’t fix Ruby version used in your project.

I’m personally more familiar with RVM aka Ruby Version Manager, so to start I check whether

which rvm

returns anything in my terminal of choice and go to https://rvm.io and follow the installation instructions in case the output is empty.

Step 2: .ruby-version

In order to tell RVM which exact Ruby version you’re going to use, create a file at the root of your repo named

.ruby-version

with the version number as its contents.

echo "3.0.0" > .ruby-version

Step 3: Install the required Ruby version

If you’ve just installed RVM, there are no Ruby versions packaged with it. You have to manually install Ruby specified in your .ruby-version file. To list versions known to RVM, use

rvm list known

and then install the one you need:

rvm install ruby-3.0.0

After that, checking which ruby in terminal should yield a path in .rvm directory:

which ruby
/Users/username/.rvm/rubies/ruby-3.0.0/bin/ruby

Step 4: .ruby-gemset

I understand a Ruby gemset as sort of a namespace in which your dependencies are installed. So, if you need some specific version of a gem in one app and a different version of the same gem in another app, RVM can easily manage these for you if the gems are installed in different gemsets. It’s just good manners to declare a separate gemset for each app you’re working on.

echo "wanderwaltz.github.io" > .ruby-gemset

Step 5: rvm use .

Make sure you’re using the gemset you’re declaring. In the root of the repo call

rvm use .

to make sure RVM has read the .ruby-version and .ruby-gemset files and taken their contents into account.

Step 6. Install bundler

gem install bundler

Bundler is used to manage all your other Ruby dependencies. While all other stuff will be installed via

bundle install

command, you have to install bundler itself directly via gem install.

Step 7. Make a Gemfile

Create a Gemfile and list your dependencies there. See https://bundler.io/gemfile.html for info on the syntax. Remember that the Gemfile is basically a Ruby script so if you need to do some complex logic in there, you can (although this may not be that great of an idea).

Install dependencies via

bundle install

Bonus step: problems with installing gems

Sometimes unexpected problems arise when installing dependencies. Usually Stack Overflow has a solution for these. This time I was installing Jekyll and its transitive dependencies and encountered the following error:

compiling binder.cpp
In file included from binder.cpp:20:
./project.h:116:10: fatal error: 'openssl/ssl.h' file not found
#include <openssl/ssl.h>

A question on Stack Overflow contained enough info to solve the problem, although I needed to follow the steps from the comments, not the actual answer.

For future reference, the steps which helped me were the following:

brew install openssl
brew link openssl --force
gem install eventmachine -v 1.0.8 -- --with-cppflags=-I/usr/local/opt/openssl/include
bundle install

Step 8: Always bundle exec

To make sure you’re using the right gems, always use

bundle exec <command>

afterwards, not just <command>. This is for the Ruby-based commands obviously. For example, in case of wanderwaltz.github.io the commands I run are usually related to Jekyll and take the following form:

bundle exec jekyll serve

Project maintained by wanderwaltz Hosted on GitHub Pages — Based on theme by mattgraham