Mac OS X, Ruby, ruby-install, chruby and You09 Jun 2015
Last updated: July 13th, 2016
This beginner's guide will set up with Ruby 2.4.1, chruby, ruby-install and Rails 5.1.2 and is specifically written for a development environment on Mac OS X, but will probably work on many other operating systems with slight modifications.
This guide is almost a copy of my Ubuntu, Ruby, ruby-install, chruby, Rails and You guide, but this one has instructions for Macs.
This guide will cover installing a couple of things:
- ruby-install: a very lightweight way to install multiple Rubies on the same box.
- chruby: a way to easily switch between those Ruby installs
- Ruby 2.4.1: at the time of writing the newest current stable release of Ruby.
- Bundler: a package dependency manager used in the Ruby community
- Rails 5.1.2: at the time of writing the newest current stable release of Rails.
By the end of this guide, you will have these things installed and have some very, very easy ways to manage gem dependencies for your different applications / libraries, as well as having multiple Ruby versions installed and usable all at once.
We assume you have
sudo access to your machine, and that you have an understanding of the basic concepts of Ruby, such as "What is RubyGems?" and more importantly "How do I turn this computer-thing on?". This knowledge can be garnered by reading the first chapter of any Ruby book.
If you're looking for a good Rails book, I wrote one called Rails 4 in Action.
The first thing we're going to need to install is XCode which you can get from the Mac App Store. We'll use XCode to install the Command Line Tools which install some libraries that Ruby will use to compile itself.
First of all, we're going to need to install some package management script so that we can install packages such as Git, MySQL and other things exceptionally easy. The best package management system on Mac OS X for this is homebrew. We can install this by using this command:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Next, we'll install
brew install chruby ruby-install
First we fetch the ruby-install file, extract it into a directory, then make it. You can verify that these steps have worked by running the following command:
If you see this, then you've successfully installed ruby-install:
Our next step is to install Ruby itself, which we can do with this command:
ruby-install --latest ruby
This command will take a couple of minutes, so grab your $DRINKOFCHOICE and go outside or something. Once it's done, we'll have Ruby 2.4.1 installed.
Now we'll need to load chruby automatically, which we can do by adding these lines to
~/.zshrc if you're using ZSH):
source /usr/local/opt/chruby/share/chruby/chruby.sh source /usr/local/opt/chruby/share/chruby/auto.sh
In order for this to take effect, we'll need to source that file:
. ~/.bash_profile # or (if you're using ZSH) . ~/.zshrc
Alternatively, opening a new terminal tab/window will do the same thing.
To verify that chruby is installed and has detected our Ruby installation, run
chruby. If you see this, then it's working:
Now we need to make that Ruby the default Ruby for our system, which we can do by creating a new file called
~/.ruby-version with this content:
This file tells
chruby which Ruby we want to use by default. To change the ruby version that we're using, we can run
chruby ruby-2.4.1 for example -- assuming that we have Ruby 2.4.1 installed first!
Did this work? Let's find out by running
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-darwin16]
Now that we have a version of Ruby installed, we can install Rails. Because our Ruby is installed to our home directory, we don't need to use that nasty
sudo to install things; we've got write-access! To install the Rails gem we'll run this command:
gem install rails -v 5.1.2 --no-document
This will install the
rails gem and the multitude of gems that it and its dependencies depend on, including Bundler.
Before you can use MySQL, you'll need to install it with Homebrew:
brew install mysql
gem install mysql should succeed.
Before you can use PostgreSQL, you'll need to install it with Homebrew:
brew install postgresql
gem install pg should succeed.
And that's it! Now you've got a Ruby environment you can use to write your (first?) Rails application in with such minimal effort. A good read after this would be the official guides for Ruby on Rails.
The combination of chruby and ruby-install is such a powerful tool and comes in handy for day-to-day Ruby development. Use it, and not the packages from apt to live a life of development luxury.