Siddhesh Acharekar

December 24, 2023

Installing Ruby on Rails on macOS with Zsh

I've been using Ruby on Rails since 3 years now. Got first introduced to it when I started working for Mindbody, and so far it's mostly just been that. Something I use at work. Now that I've begun to do my own projects in it I've noticed one pain point which tbh shouldn't even exist given how much Rails has grown and how fantastic the community is: how does one install it locally on a macOS machine?

Rbenv? RVM? asdf? qwerty?
(qwerty isnt really a ruby env manager its me trying to be funny following asdf which really is something usable)

I get why every Rails app at work is dockerized and why everybody swears by that being the best, preferred way to spin up Rails apps without much hassle and local configurations. Trust me I get it. But if somebody's starting their Rails journey, its terrible to subject them to this level of decision making and enforcing the prerequisite to know Docker. Maybe it somewhere in the world. The needle which is the most convenient, straightforward, and most importantly easily reproducible way to install Ruby on Rails in a million acre haystack farm called the internet. 

But till I find that, this is what I use. Lets get started.

Brew

We'll need brew, update it first.
> brew update

Rbenv

You need a ruby environment manager: something that allows you to manage multiple ruby versions across apps and change as needed. Now there's many out there that can do this for you like rvm, asdf, chruby to name a few but rbenv has just seemed the easiest to work with so far so I recommend this. Also there's VERY little you have to do to get it to a working state.

> brew install rbenv

Zsh

> vim ~/.zshrc

then paste this at the bottom of whatever's in your .zshrc file already:

# For rbenv
source $HOME/.zshenv
eval "$(rbenv init - zsh)"

then

> vim ~/.zshenv

paste this:

export PATH="$HOME/.rbenv/bin:$PATH"

finally get back to your terminal and run:

> source ~/.zshrc

verify rbenv has been installed by running type rbenv and you should see the following output:

> type rbenv
rbenv is a shell function from /Users/{your-home-path}/.zshrc

Ruby install

This is where the real magic starts. Rbenv helps you specify what specific ruby version you want to go with. We'll first check what latest stable versions are listed for each release, then go with the one we want.
> rbenv install -l
3.0.6
3.1.4
3.2.2
jruby-9.4.5.0
mruby-3.2.0
picoruby-3.0.0
truffleruby-23.1.1
truffleruby+graalvm-23.1.1
As of today (12/24/2023) 3.2.2 is the latest stable release so I'm gonna go ahead with that.

> rbenv install 3.2.2
Go grab some tea, have a bath, get married, have a kid and come back. This should be done by the time your kids 3.

At this point if you were to run ruby -v you'd still see the outdated stock ruby that comes installed on macOS so we'll set this freshly installed latest ruby release as our preference with:

> rbenv global 3.2.2

Kill your terminal to be sure, and restart it. Now run this and your output should be something similar to:
> ruby -v
ruby 3.2.2p20 (2023-03-30 revision 4491bb740a) [x86_64-darwin22]

Gems

Every successful language has shareable packages of code and one manager library to rule them all. Java has "libraries" managed by Maven. Python has "packages" managed by "pip". In Ruby we have "gems" managed by (another gem) Bundler. We install bundler now.

First run:
> echo "gem: --no-document" > ~/.gemrc

This line prevents gems from building local documentation as part of installation. Sure docs for a gem are helpful, but youre better of googling api contracts for gems than slowing down every bundle install.
Next, install bundler:
gem install bundler

Ensure your gems are installed and will be installed in the right location. The right location: because we use rbenv will be under the ruby version we defined as global above. So this is how we check it:
> gem env home
/Users/{your-home-path}/.rbenv/versions/3.2.2/lib/ruby/gems/3.1.0

Rails

And now, to commence your web development career, we install rails.
> gem install rails

Now go have your second kid.

If you're interested to know why we do the next command, google "shims rehash rbenv". I'm leaving it out intentionally to not overload you with information. But run this:
> rbenv rehash

Finally, verify its successful installation:
> rails -v
Rails 7.1.1

Congratulations! You're good to go now!

About Siddhesh Acharekar

I make web apps for a living. @SiddheshSA