Intro

rbenv is a utility for installing multiple ruby versions on a host machine. Using rbenv allows you to install ruby in a path you have ownership over so you can install gems without having to have sudo or root privileges. rbenv also allows you to target the exact ruby version in development that's in use in production deployments potentially avoiding nefarious bugs due to Ruby version mismatches.

In this post I will install rbenv on CentOS, install an alternate Ruby version to the system Ruby and cover some basic usage.

For reference the following software will be used in this post.

  • CentOS - 7
  • rbenv - 1.1.1-40-g483e7f9
  • ruby - 2.5.5

Dependencies

Install the required dependencies.

cmd
sudo yum install -y git-core zlib zlib-devel gcc-c++ patch readline readline-devel libyaml-devel libffi-devel openssl-devel make bzip2 autoconf automake libtool bison curl

Install rbenv

Clone the rbenv git repository.

cmd
git clone https://github.com/rbenv/rbenv.git ~/.rbenv

# output

Cloning into '/home/bradmin/.rbenv'...
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 2748 (delta 0), reused 1 (delta 0), pack-reused 2744
Receiving objects: 100% (2748/2748), 517.92 KiB | 344.00 KiB/s, done.
Resolving deltas: 100% (1720/1720), done.

Compile dynamic bash extension to speed up rbenv.

cmd
cd ~/.rbenv && src/configure && make -C src

# output

make: Entering directory `/home/bradmin/.rbenv/src'
gcc -fPIC     -c -o realpath.o realpath.c
gcc -shared -Wl,-soname,../libexec/rbenv-realpath.dylib  -o ../libexec/rbenv-realpath.dylib realpath.o
make: Leaving directory `/home/bradmin/.rbenv/src'

Update the ~/.bashrc to add the rbenv cli utility to your path.

cmd
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc

Reload your shell to load the rbenv parameters into the environment.

cmd
exec $SHELL -l

Install Plugins

Install the ruby-build plugin.

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

# output

Cloning into '/home/bradmin/.rbenv/plugins/ruby-build'...
remote: Enumerating objects: 35, done.
remote: Counting objects: 100% (35/35), done.
remote: Compressing objects: 100% (20/20), done.
remote: Total 9681 (delta 12), reused 33 (delta 10), pack-reused 9646
Receiving objects: 100% (9681/9681), 2.05 MiB | 1.18 MiB/s, done.
Resolving deltas: 100% (6327/6327), done.

Install Ruby

Install an alternate version of Ruby with the rbenv install <ruby-version> command.

cmd
rbenv install 2.5.5

# output

Downloading ruby-2.5.5.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.5.tar.bz2
Installing ruby-2.5.5...
Installed ruby-2.5.5 to /home/bradmin/.rbenv/versions/2.5.5

After installing new version of Ruby or a gem that provides new commands, use the rbenv rehash command to install shims for all executables known to rbenv.

cmd
rbenv rehash

Usage

Create a project folder and set a default ruby version with the rbenv local <ruby-version> command.

cmd
mkdir ~/ruby-project && cd ~/ruby-project
rbenv local 2.5.5
Note
This will create a .ruby-version file in the project directory overriding the global Ruby version.

Confirm the Ruby version installed and location of the Ruby binary is in the users home directory.

cmd
ruby --version

# output

ruby 2.5.5p157 (2019-03-15 revision 67260) [x86_64-linux]
cmd
which ruby

# output

~/.rbenv/shims/ruby
cmd
which gem

# output

~/.rbenv/shims/gem

Confirm gems can be successfully installed as a non root user.

cmd
gem install bundler

# output

Fetching: bundler-2.0.1.gem (100%)
Successfully installed bundler-2.0.1
Parsing documentation for bundler-2.0.1
Installing ri documentation for bundler-2.0.1
Done installing documentation for bundler after 3 seconds
1 gem installed
cmd
which bundle

# output

~/.rbenv/shims/bundle

Outro

rbenv is a useful utility to managing multiple Ruby installs on a host machine. This allows you to keep your system Ruby install clean and also use the same Ruby version in your development environment as you would use in a production deployment.

# ruby
# rbenv
# centos