Yet another step-by-step guide to setting up a Ruby developement environment. Setting up a proper Ruby development environment involves setting up a Ruby Version Manager and Gem dependencies isolation.
You should never rely on your system installed Ruby but use a Ruby Version Manager. There are 2 main tools for this, RVM and rbenv. This is a guide for rbenv which I prefer for its simple, minimalist, less intrusive approach and its rich plugins ecosystem.
Make sure you are logged in as a normal user not as root, rbenv will be installed in $HOME/.rbenv
.
- execute steps 1 to 4 of https://github.com/sstephenson/rbenv#basic-github-checkout
- create the
~/.rbenv/plugins
directory
mkdir ~/.rbenv/plugins
This plugin will enable the rbenv build ...
command to easily build any Ruby version.
git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
This plugin will enable the rbenv update
command to easily update code and list of buildable rubies.
git clone https://github.com/rkh/rbenv-update.git ~/.rbenv/plugins/rbenv-update
This plugin will automate the rbenv rehash
command after gem installation.
git clone https://github.com/sstephenson/rbenv-gem-rehash.git ~/.rbenv/plugins/rbenv-gem-rehash
This plugin allow using shorter names for Ruby versions. See rbenv-aliases repo for usage.
git clone git://github.com/tpope/rbenv-aliases.git ~/.rbenv/plugins/rbenv-aliases
- first update list of available rubies (which will incidentally also update all rbenv related code).
rbenv update
- view list of available rubies for installation
rbenv install --list
- install rubies, for example:
rbenv install jruby-1.7.17
rbenv rehash
rbenv install 1.9.3-p551
rbenv rehash
rbenv install 2.1.5
rbenv rehash
- create a root directory for your project and go to it
- tell rbenv which Ruby to use when in this directory and below
rbenv local jruby-1.7.17
- verify
ruby -v
jruby 1.7.17 (1.9.3p392) 2014-12-09 fafd1a7 on Java HotSpot(TM) 64-Bit Server VM 1.7.0_11-b21 +jit [darwin-x86_64]
- the first time you use a new Ruby, make sure the bundler gem is installed
gem install bundler --no-ri --no-rdoc
- list all installed rubies
rbenv versions
-
similar to
rbenv local
- set the global version of Ruby to be used in all shells
rbenv global 1.9.3-p551
- sets a shell-specific Ruby version by setting the
RBENV_VERSION
environment variable in your shell
rbenv shell 1.9.3-p551
To manage the gem dependencies of your project you will be using Bundler by typically creating a Gemfile
in the root of your project.
I suggest you use the following default options for Bundler by creating the ~/.bundler/config
file with:
---
BUNDLE_PATH: vendor
BUNDLE_DISABLE_SHARED_GEMS: '1'
This will make sure your project isolate its dependencies by installing all gems into the vendor/
directory and do not use any system provided gems.