Getting Ruby on Rails going on Mac OS

I'm currently using a Macbook Air (2020, i5, 8GB, Catalina) for Ruby and Rails development.

It's a great combination, but there are a couple of useful snippets of information that I've found helpful when setting up Ruby/Rails on it (I rebuild my dev machines, whether Mac, Linux, or Windows, on a pretty regular basis). As this would possibly help others too, I'm documenting the complete steps I use to get things in place.

By following these instructions, I go from a fresh Mac OS to a running demo site with Ruby, Bundler, Rails, and Yarn.

About doing this on a 2020 Macbook Air

Performance is fine. Be warned however that performing all these steps up to where I had a running demo app (no code changes by me) blew through around 25% of my battery life (mainly due to Xcode building native extensions I believe). Perhaps don't start this with less charge than that, though a trouble-free install could reduce the impact.

Running Rails commands may fail if you use the system Ruby as even though it is a suitable version there are often path access issues with Bundler. Whilst Bundler offers up commands to sidestep this, the easiest option is to use rbenv.

In fact I'd recommend using rbenv anyway, to help in supporting apps with different Ruby/Rails requirements.

You'll also need the XCode developer tools for native dependencies to build. To install these in advance, try and run git in your Terminal. You'll get prompted to install Xcode and at that point you can choose the developer tools only.

Setting up a Ruby install using rbenv

brew install rbenv
  • You'll have probably got a warning about your system OpenSSL going stale (and a command which will allow your OpenSSL to remain updated). It's up to you, and depends upon how often you rebuild your machine. The instructions basically say that in order to link Rubies to Homebrew's OpenSSL 1.1 (which is kept upgraded) add the following to your ~/.zshrc:
export RUBY_CONFIGURE_OPTS="--with-openssl-dir=$(brew --prefix openssl@1.1)"
  • Also add this to the same ~/.zshrc file to ensure rbenv is always available, then restart all your Terminal windows:
eval "$(rbenv init -)"
  • Run the following (after restarting your Terminal) to get the current state of play. If you have no rbenv version of Ruby (you won't at this point unless you've deviated from the above), it will give you a command to install one.
curl -fsSL | bash
  • Follow the instructions the last command emitted to install an rbenv version and make it the default. For example:
rbenv install 2.6.3
rbenv global 2.6.3
  • When it is installed, restart your Terminal again, then run the following to check that you are not still using the system Ruby from /usr/bin. If you are, then adjust your path to make sure the right one is listed first by updating your startup scripts in your home folder.
ruby --version
which ruby

Dealing with any permissions issues with Bundler

Your Bundler is probably still using the system Ruby, which can result in the message "Your user account isn't allowed to install to the system RubyGems". To fix it run the following commands. They may not all be necessary (sometimes only the first is needed), but doing all of them saves having to work through which one(s) is/are sufficient.

sudo chown -R $USER  ~/.rbenv
gem list bundler
gem uninstall bundler
rbenv rehash
gem install bundler
rbenv rehash

Installing and configuring Rails

  • Install Rails
gem install rails --version=6.0.1 --no-document
gem list --local rails
  • Create your new Rails app (example folders):
mkdir -p ~/Source/Ruby/Rails
cd ~/Source/Ruby/Rails
rails new demo
cd demo
  • Sit back and wait for the dependencies to be fetched and for Xcode to build the native ones (I throttle my Air CPU to keep the fans off, so for me this takes a fair while).
  • When it's done it will ask for Yarn. It shows instructions, and at that point you're in the Land of Node so I'll stop here, other than to suggest
    • using nvm to manage your Node versions in the same way as rbenv does for Ruby, then installing Yarn globally with npm install --global yarn
    • or using brew install yarn to do a quick install if you don't care about sharing one Yarn across your system
  • Do a web pack build for all the Node (front-end) stuff using rails webpacker:install

Checking and running your Rails app

bin/rails about
bin/rails server

Browse to localhost:3000 to see the demo site.

Going forward

Follow the many Rails tutorials out there, but it's worth remembering a couple of useful commands just covered above:

curl -fsSL | bash
gem list
gem list --local rails
bin/rails about
bin/rails server