The following is a thorough ‘setup guide’ for installing rbenv, ruby-build, Ruby, Bundler, Rails, RubyGems and other packages.
Most developers like to spend a bit of time setting up their development workspace. I’m no different, after a number of years tweaking and experimenting the following will detail how I setup my environment for OS X.
There has always been a consistent criteria my development environment needed to meet:
- Unobtrusive, no modifying core files
- Flexibility with Ruby versions
- Minimal configuration
- Easy to setup new/existing projects
So if you’re a ruby developer or a web developer with the same ideals this should help you get started quickly.
This ‘setup guide’ assumes a clean install of OS X, and the following tools are already installed: Command Line Tools, git and Hombrew.
OS X comes with Ruby installed (Mavericks/Yosemite even gets version 2.0.0, previously it was only 1.8.7), as we don’t want to be messing with core files we’re going to use the brilliant rbenv and ruby-build to manage and install our Ruby versions for our development environment.
Lets get brewing! We can install both of the required packages using Homebrew, once done we add a line to our ~/.bash_profile and reload our terminal profile.
Let’s install rbenv and ruby-build first:
brew install rbenv ruby-build rbenv-gem-rehash rbenv-default-gems
Note: Add rbenv to ~/.bash_profile so that it loads every time you open a terminal:
echo 'if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi' >> ~/.bash_profile source ~/.bash_profile
Now let’s install Ruby:
rbenv install 2.1.1
After Ruby is installed:
And let’s set it as the global version:
rbenv global 2.1.1
Check if it was set as the global version:
The package we just installed allow us to install different versions of Ruby and specify which version to use on a per project basis and globally. This is very useful to keep a consistent development environment if you need to work in a particular Ruby version.
Note: Now close terminal and open it again, this ensures everything has been reloaded in your shell.
Bundler manages an application’s dependencies, kind of like a shopping list of other libraries the application needs to work.
gem install bundler
echo 'bundler' >> "$(brew --prefix rbenv)/default-gems"
Skip r-doc generation. If you use Google for finding your Gem documentation like I do you might consider saving a bit of time when installing gems by skipping the documentation.
echo 'gem: --no-rdoc --no-ri' >> ~/.gemrc
Install Ruby on Rails
So far you’ve installed Ruby, if you’re not going to be working with Rails you can pat yourself on the back and start working with Ruby! If you intend to work with Rails then you’ve just got a couple more things to do.
SQLite is lightweight SQL service and handy to have installed since Rails defaults to using it with new projects. You may find OS X already provides an (older) version of SQLite3, but in the interests of being thorough we’ll install it anyway as Homebrew will set it to ‘keg-only’ and not interfere with the system version if that is the case.
Installation is simple with Homebrew:
brew install sqlite3
With Ruby installed and the minimum dependencies ready to go Rails can be installed as a Ruby Gem.
gem install rails
If you would like Rails to be a default gem in the future when you install a new version of Ruby you can add it to the default-gems file.
echo "rails\n" >> ~/.rbenv/default-gems
Rails has quite a number of other gem dependencies so don’t be surprised if you see loads of other gems being installed at the same time.
RubyGems, the Ruby package manager, was also installed:
Update to its latest version with:
gem update --system
To install a “gem” (Ruby package), run:
To install without generating the documentation for each gem (faster):
gem install --no-document
To see what gems you have installed:
To check if any installed gems are outdated:
To update all gems or a particular gem:
gem update [ ]
RubyGems keeps old versions of gems, so feel free to do come cleaning after updating:
We mainly use Ruby for the CSS pre-processor Compass, which is built on top of Sass:
gem install compass --no-document