Ruby - rbenv notes

Manage Ruby installations with rbenv

Install rbenv

Install rbenv on macOS with Homebrew
# Install rbenv and ruby-build
brew install rbenv

# setup rbenv in your shell
rbenv init
Install rbenv on Ubuntu with rbenv-installer
# install rbenv and ruby-build
curl -fsSL https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-installer | bash

# setup rbenv in your shell
rbenv init
Windows 10 requires Windows Subsystem for Linux

Windows Subsystem for Linux

Setup rbenv and default gem path

Setup .bash_profile on macOS
# automatically load rbenv by appending the following to ~/.bash_profile
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile

# reload .bash_profile so that PATH changes take effect
source ~/.bash_profile

Note: your .bash_profile file should contain the following:

# rbenv
eval "$(rbenv init -)"
Setup .bashrc on Ubuntu and Windows 10 with Windows Subsystem for Linux
# add ~/.rbenv/bin to your $PATH for access to the 'rbenv' command-line utility
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc

# automatically load rbenv by appending the following to ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc

# change the default gem path to ~/gems
# (recommended to avoid file permissions problems and using `sudo`)
echo '# Install Ruby Gems to ~/gems' >> ~/.bashrc
echo 'export GEM_HOME="$HOME/gems"' >> ~/.bashrc
echo 'export PATH="$HOME/gems/bin:$PATH"' >> ~/.bashrc

# restart your shell so that PATH changes take effect
exec $SHELL

Note: your .bashrc file should contain the following:

# rbenv
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
# install Ruby Gems to ~/gems
export GEM_HOME=$HOME/gems
export PATH=$HOME/gems/bin:$PATH

Uninstall rbenv

First, disable rbenv by removing the following two lines from your shell startup config file (.bash_profile or .bashrc):

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

Then:

# remove rbenv root directory
rm -rf `rbenv root`

# if you installed rbenv using Homebrew
brew uninstall rbenv

Check rbenv installation and default gem path

# Check your install
curl -fsSL https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-doctor | bash

# check that default gem paths point to your home directory by default
gem env

Install ruby with rbenv

sudo apt install -y libssl-dev zlib1g-dev # ubuntu
sudo dnf install -y libssl-dev zlib1g-dev # fedora

# install Ruby 2.5.1
rbenv install 2.5.1

# install Ruby 2.5.3
rbenv install 2.5.3

# set Ruby 2.5.3 as the global Ruby version
rbenv global 2.5.3

# print ruby version
ruby -v

Install ruby gems

# install bundler and jekyll on ubuntu
gem install bundler jekyll

# install ruby gems locally on mac
gem install --user-install bundler jekyll

Common rbenv commands

# list all available rbenv commands
rbenv commands

# get global ruby version
rbenv global

# set global ruby version to 2.5.3
rbenv global 2.5.3

# set global ruby version to system default
rbenv global system

# get local ruby version
rbenv local

# set local ruby version to 2.5.3
rbenv local 2.5.3

# unset local ruby version
rbenv local --unset

# get shell ruby version
rbenv shell

# set shell ruby version to 2.5.3
rbenv shell 2.5.3

# unset shell ruby version
rbenv shell --unset

# list all ruby versions available for installation
rbenv install -l

# install ruby version 2.5.3
rbenv install 2.5.3

# uninstall ruby version 2.5.3
rbenv uninstall 2.5.3

# install shims for all Ruby executables known to rbenv
# run this command after you install a new version of Ruby or install a gem that provides commands
rbenv rehash

# list all installed ruby versions
rbenv versions

# get current ruby version
rbenv version
Source

How To Install Ruby on Rails with rbenv on Ubuntu 18.04


© 2020 | Paul Kim