Ruby can be notoriously challenging to get setup initially. You might run
into permissions issues when running gem install
or you might go to
install gems and it tells you that you’re using the wrong version of ruby!
These installation woes are the biggest blocker for most folks wanting to
use Ruby packages like jekyll
where Ruby is an implementation detail and
unrelated to what they’re actually trying to do.
How do Rubyists fix this? They use a Ruby version manager.
There are many Ruby version managers. My preference is rbenv
, but rvm
,
chruby
, and asdf
are all good options.
Getting setup with rbenv
is easy on a Mac (full installation instructions):
# 1. Install rbenv & ruby-build
$ brew install rbenv ruby-build
# 2. Load rbenv into your shell environment persistently (.bashrc for bash, .zshrc for zsh)
$ echo 'eval $(rbenv init -)' >> ~/.bashrc && . ~/.bashrc # for bash
$ echo 'eval $(rbenv init -)' >> ~/.zshrc && . ~/.zshrc # for zsh
# 3. List versions available to install! Choose one.
$ rbenv install --list
# 4. Install that version!
$ rbenv install 2.7.3
# 5. Set that version as the new default on your machine.
$ rbenv global 2.7.3
# Override for a specific directory by running `rbenv local <new_version>`
This installs this version of ruby into $HOME/.rbenv/versions
. Since this
is in your home directory, there are no permissions issues. Gems are
installed inside each individual version (see gem env GEM_PATH
for the
exact paths) so it’s much easier to install.
Running rbenv global <version>
means that your shell will refer to that
version by default. Running gem install
in any directory will install
gems using that version, and running ruby <file>
or a gem executable will
use that Ruby version.
Different projects may have different Ruby versions. Rbenv makes specifying
which Ruby version you want easy: inside your project’s root directory, run
rbenv local <version>
(it will prompt you to install the version if it’s
not present). This is really helpful when you need to be switching between
versions but don’t want to remember. It will write out a .ruby-version
file so be sure to commit that to your source control!
Want to delete a version you’re not using anymore? Easy: rm -r ~/.rbenv/versions/<version>
.
Using rbenv
makes your life easier! I have been happily using rbenv for
many years now and can easily install and use gems and manage ruby versions
with ease.