Install Ruby with rbenv on macOS

This is a note on how to install rbenv on macOS and how to use it.

1. Install rbenv

Install rbenv.

Before install rbenv, you need to install xcode-select.

1
$ xcode-select --install

Install rbenv.

1
2
$ git clone https://github.com/rbenv/rbenv.git ~/.rbenv
$ cd ~/.rbenv && src/configure && make -C src

Add the following to .bash_profile

1
2
3
4
# rbenv
export RBENV_ROOT="$HOME/.rbenv"
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"

Load .bash_profile in shell

1
$ source .bash_profile

Install ruby-build to build ruby.

1
2
$ mkdir -p "$(rbenv root)"/plugins
$ git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build

2. How to Use rbenv ( Install and manage Ruby )

How to check which version you can use.

1
$ rbenv install -l

How to check which version you insatlled.

1
$ rbenv versions

How to install the specified version of PHP.

1
$ rbenv install 2.4.3

Run this command after you install a new version of Ruby

1
$ rbenv rehash

How to use the specified version of Ruby at global.

1
$ rbenv global 2.4.3

How to use the specified version of Ruby at local ( directory ).

1
$ rbenv local 2.4.3

This command will create .ruby-version file at current directory.

3. How to use gem

You can use gem.
When you install gem packages global ( gem install *** ), the gem packages will installed at current version of ruby directory)( example: .rbenv/versions/2.4.3/lib/ruby/gems/ ).
So, you shuld use rbenv rehash command after install gem packages at global.
I think this is safe because the gem packages installed global is related Ruby version.

example.

1
2
$ gem install bundler
$ rbenv rehash

4. How to Update rbenv

1
2
$ cd $(rbenv root)
$ git pull

5. How to Update ruby-build

When a new version of Ruby is released, you need to update ruby-build. Otherwise you can not install the new version of Ruby.

1
2
$ cd $(rbenv root)/plugins/ruby-build
$ git pull