K Cartlidge

C#/DotNet. Go. Node. Python/Flask. Elixir.

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
export RUBY_CONFIGURE_OPTS="--with-openssl-dir=$(brew --prefix openssl@1.1)"
eval "$(rbenv init -)"
curl -fsSL https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-doctor | bash
rbenv install 2.6.3
rbenv global 2.6.3
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

gem install rails --version=6.0.1 --no-document
gem list --local rails
mkdir -p ~/Source/Ruby/Rails
cd ~/Source/Ruby/Rails
rails new demo
cd demo

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 https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-doctor | bash
gem list
gem list --local rails
bin/rails about
bin/rails server