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 https://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 https://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.
Alternatively, you can install the full Xcode from the App Store or the smaller Command Line Tools for Xcode from https://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
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 and ruby-install
ruby-install is used to install different versions of Ruby
brew install chruby ruby-install
Next, you can install Ruby
ruby-install ruby 2.4
ruby-install will use Homebrew to install dependencies
Afte Ruby is installed, add these 2 lines
source /usr/local/opt/chruby/share/chruby/chruby.sh source /usr/local/opt/chruby/share/chruby/auto.sh
chruby will switch to that version automatically.
rbenv is used to select the Ruby version. It can also install different Ruby versions through
brew install rbenv
ruby-build is a dependency
Next, set up the shell integration. You can find the instructions by running
The output will tell you to
eval "$(rbenv init -)"
To install Ruby, run
rbenv install 2.4.3
ruby-build will use Homebrew for its dependencies similar
rbenv doesn't have a command to switch to a Ruby version. It selects a Ruby version for you automatically by looking at
.ruby-version on the application, or the
RVM was the first to be released among the projects mentioned on this post. It has even more features than
RVM is not available through Homebrew. You can install it with
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB \curl -sSL https://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 https://raw.githubusercontent.com/rvm/rvm/master/binscripts/rvm-installer \curl -O https://raw.githubusercontent.com/rvm/rvm/master/binscripts/rvm-installer.asc gpg --verify rvm-installer.asc && bash rvm-installer stable
The installer will only run
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
This line allows you to
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
Which one should I choose?
If you're looking for a tool for installing and switching Ruby versions, you can use any
After you install Ruby,
Most Ruby applications now use Bundler and it will be available to the standard Ruby installation soon. Install Bundler
gem install bundler
To read more about the tools mentioned here, check out