This is a post on installing Ruby on a Mac. This should work on recent MacOS versions – El Capitan, Sierra, and High Sierra. First of all, Ruby is already pre-installed on your Mac. However, the pre-installed version is a few versions behind so we’ll look into the other ways to install Ruby.
Using a package management system makes your life easier when installing any software. On a Mac, you can use Homebrew to install newer versions of Ruby. You can also use it to install a tool that will install Ruby. More on this below.
To install Homebrew, run
/usr/bin/ruby -e '$[curl -fsSL //raw.githubusercontent.com/Homebrew/install/master/install]'
If you don’t like to run a script from a remote site, you can run this instead
cd /usr/local
mkdir homebrew && curl -L //github.com/Homebrew/brew/tarball/master | tar xz --strip 1 -C homebrew
Most Homebrew packages require a compiler. You can install the Command Line Tools from the command line.
xcode-select --install
Alternatively, you can install the full Xcode from the App Store or the smaller Command Line Tools for Xcode from //developer.apple.com/download/more/. You’ll need to register for an account.
Once Homebrew is installed, you can use it to install Ruby.
brew install ruby
This will install the latest Ruby version on Homebrew [which is 2.5.0 at the time of this writing] on /usr/local/bin/ruby
.Add /usr/local/bin
to the start of your PATH
so it becomes the default Ruby.
Add this to ~/.profile
or~/.bash_profile
.
export PATH='/usr/local/bin:/usr/local/sbin:$PATH'
If you’re using Ruby to run a few scripts then using the Homebrew version is enough. If you’re developing multiple Ruby applications, you should consider one of the tools below. These are installers and version managers that make it easier to install and run multiple Ruby versions. You can use Ruby 2.3 for one application and Ruby 2.4 for another.
It is important to upgrade the Ruby version you use. You get security fixes and you stay on a supported version. The Ruby maintainers drop support for older Ruby versions. The Rails framework also require a relatively newer version of Ruby.
To run different Ruby versions on your Mac, consider one of the following: chruby, rbenv, and RVM. Choose only one of these three. Using more than one at the same time may cause problems.
Scale performance. Not price. Try Engine Yard today and enjoy our great support and huge scaling potential for 14 days.
Deploy your app for free with Engine Yard.
chruby and ruby-install
ruby-install
is used to install different versions of Ruby and chruby
is used to switch to a specific version.
You can install
chruby
andruby-install
using Homebrew.
brew install chruby ruby-install
Next, you can install Ruby using ruby-install
. For example, to install the latest Ruby 2.4.x,
ruby-install ruby 2.4
ruby-install
will use Homebrew to install dependencies like
openssl
,readline
,automake
,and libyaml
. Ruby will be compiled from source so this may take some time.
Afte Ruby is installed, add these 2 lines on ~/.bash_profile
. If
you’re using ZSH as your shell, add these to ~/.zshrc
.
source /usr/local/opt/chruby/share/chruby/chruby.sh
source /usr/local/opt/chruby/share/chruby/auto.sh
chruby.sh
enables chruby
. To switch to a Ruby version, run
chruby 2.4.3
auto.sh
automatically chooses the Ruby version depending on the version specified on .ruby-version
.
When you cd
to the root of your application where .ruby-version
exists, chruby
will switch to that version automatically.
rbenv
rbenv
is used to select the Ruby version. It can also install different Ruby versions through its ruby-build
plugin
. It has more features than the combination of ruby-install
andchruby
but for installing and switching Ruby versions, they’re similar.
You can install rbenv
using Homebrew.
brew install rbenv
ruby-build
is a dependency of rbenv
so it will also be installed by Homebrew.
Next, set up the shell integration. You can find the instructions by running
rbenv init
The output will tell you to add eval '$[rbenv init -]'
to~/.bash_profile
.
To install Ruby, run
rbenv install 2.4.3
ruby-build
will use Homebrew for its dependencies similar to ruby-install
. Ruby will be compiled from source so this may take some time.
rbenv
doesn’t have a command to switch to a Ruby version. It selects a Ruby version for you automatically by looking at the RBENV_VERSION
env variable, .ruby-version
on the application, or the global ~/.rbenv/version
file.
RVM
RVM was the first to be released among the projects mentioned on this post. It has even more features than rbenv beyond installing and switch Ruby versions.
RVM is not available through Homebrew. You can install it with
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
\curl -sSL //get.rvm.io | bash -s stable
You can skip the first line if you’re not using GPG but I recommend that you set it up. The second line makes installing convenient but you are running a script from a remote location on your shell. For a more secure installation process, verify the installer before running it.
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
\curl -O //raw.githubusercontent.com/rvm/rvm/master/binscripts/rvm-installer
\curl -O //raw.githubusercontent.com/rvm/rvm/master/binscripts/rvm-installer.asc
gpg --verify rvm-installer.asc && bash rvm-installer stable
The installer will only run if gpg --verify
succeeds. The installer will modify the PATH to include $HOME/.rvm/bin
. You’ll need to add one more line to ~/.profile
[[ -s '$HOME/.rvm/scripts/rvm' ]] && source '$HOME/.rvm/scripts/rvm'
This line allows you to run rvm use
to switch Ruby version.
To install Ruby, run
rvm install 2.4.3
To switch to a Ruby version, run
rvm use 2.4.3
RVM will switch to a new version automatically if you have .ruby-version
on your application. It can also use its own configuration file .rvmrc
which is used for more advanced features in addition to switching to a Ruby version. These features won’t be discussed on this post.
Which one should I choose?
If you’re looking for a tool for installing and switching Ruby versions, you can use any of chruby
,rbenv
, and RVM
. rbenv
and RVM
have more features on top of the 2 basic
functionalities mentioned.
Consider chruby
first as it is the smallest of the 3 while still doing its job. If you need any of the more advanced features, try the other 2. Note that it’s best to use one of these only. You can ‘disable’ the tools by commenting out the lines you added on ~/.profile
,~/.bash_profile
,or ~/.bashrc
.
Next steps
After you install Ruby, add .ruby-version
on your application and commit it to your repository. The 3 tools mentioned here all support .ruby-version
. All developers working on your application will be using the same Ruby version even if
they don’t use the same version manager.
Most Ruby applications now use Bundler and it will be available to the standard Ruby installation soon. Install Bundler with gem install bundler
and let it install and manage gems for you.
Sources
To read more about the tools mentioned here, check out
- chruby
- rbenv
- RVM
- Homebrew