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 Ubuntu, 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.

  • Ubuntu - 2004
  • rbenv - 1.1.2-36-g60c9339
  • ruby - 2.7.1

Dependencies

Install the required dependencies.

cmd
sudo apt install git curl autoconf bison build-essential \
libssl-dev libyaml-dev libreadline6-dev zlib1g-dev \
libncurses5-dev libffi-dev libgdbm6 libgdbm-dev libdb-dev

Install rbenv

The following installer script will install rbenv and the ruby-build plugin

cmd
curl -fsSL https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-installer | bash

Update Shell Environment

Update your ~/.zshrc file with the following content.

file
# ~/.zshrc
  
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"

Install rbenv-doctor

rbenv-doctor is used to detect common issues.

cmd
curl -fsSL https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-doctor | bash

Install Ruby

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

cmd
rbenv install 2.7.1

# output
Downloading ruby-2.7.1.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.7/ruby-2.7.1.tar.bz2
Installing ruby-2.7.1...
Installed ruby-2.7.1 to /home/bradmin/.rbenv/versions/2.7.1

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.7.1

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.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-linux]
cmd
which ruby
  
# output
/home/bradmin/.rbenv/shims/ruby
cmd
which gem
  
# output
/home/bradmin/.rbenv/shims/gem

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

cmd
gem install bundler
  
# output
Fetching bundler-2.1.4.gem
Successfully installed bundler-2.1.4
Parsing documentation for bundler-2.1.4
Installing ri documentation for bundler-2.1.4
Done installing documentation for bundler after 1 seconds
1 gem installed 
cmd
which bundle
  
# output
/home/bradmin/.rbenv/shims/bundle

Success!!!

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.

Tags

ruby rbenv ubuntu linux