rbenv

version-managementRubydevelopment-environmentCLIUNIX-tools

GitHub Overview

rbenv/rbenv

Manage your app's Ruby environment

Stars16,455
Watchers297
Forks1,423
Created:August 1, 2011
Language:Shell
License:MIT License

Topics

bashrubyruby-installationruby-versionsshell

Star History

rbenv/rbenv Star History
Data as of: 7/20/2025, 02:56 AM

Language Version Management Tool

rbenv

Overview

rbenv is a simple and lightweight tool for managing multiple Ruby versions. It allows using different Ruby versions for each project without affecting the global Ruby environment. rbenv is simpler than rvm and developed based on UNIX design philosophy.

Details

Key Features

  • Minimalist Design: Simple structure focused on core functionality
  • Transparent Shim System: Intercepts Ruby commands by adding shims to PATH
  • Plugin Architecture: Extensible with plugins like ruby-build
  • .ruby-version File: Automatic version switching per project
  • No Environment Variables: No complex environment variable setup like RVM
  • Fast Operation: Lightweight implementation for quick startup

How It Works

rbenv places the ~/.rbenv/shims directory at the beginning of PATH and intercepts Ruby command execution. It determines the appropriate Ruby version at runtime and executes the corresponding version's command.

Version Priority

  1. RBENV_VERSION environment variable
  2. .ruby-version file in current directory
  3. .ruby-version file in parent directories (recursive search)
  4. Global setting (~/.rbenv/version)
  5. System Ruby

Advantages and Disadvantages

Advantages

  • Simplicity: Simpler mechanism compared to RVM, easier to understand
  • Lightweight: Minimal features for fast operation
  • Project Isolation: Independent Ruby environment for each project
  • Bundler Compatibility: Easy to use with Bundler
  • Shell Independent: Minimal impact on bashrc/zshrc
  • Transparency: Predictable operation, easy troubleshooting

Disadvantages

  • Limited Features: No advanced features like gemsets
  • Manual Installation: Requires separate ruby-build plugin
  • No Windows Support: Doesn't work on native Windows
  • Initial Setup: Requires editing shell configuration files
  • Build Time: Time-consuming as it builds Ruby from source

Reference Pages

Usage Examples

Installation (Linux/macOS)

# Install via Git clone
git clone https://github.com/rbenv/rbenv.git ~/.rbenv

# On macOS, Homebrew is also available
brew install rbenv

# Install ruby-build plugin (required for Ruby installation)
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build

# Install ruby-build on macOS
brew install ruby-build

Shell Configuration (Add to .bashrc or .zshrc)

# Set rbenv path
export PATH="$HOME/.rbenv/bin:$PATH"

# Initialize rbenv
eval "$(rbenv init -)"

# Apply configuration
source ~/.bashrc  # or source ~/.zshrc

Installing Build Dependencies

# Ubuntu/Debian
sudo apt-get update
sudo apt-get install -y build-essential libssl-dev libreadline-dev \
zlib1g-dev libffi-dev libyaml-dev libgdbm-dev libncurses5-dev

# CentOS/RHEL/Fedora
sudo yum install -y gcc openssl-devel readline-devel zlib-devel \
libffi-devel libyaml-devel gdbm-devel ncurses-devel

# macOS (Xcode Command Line Tools required)
xcode-select --install

Basic Usage

# List available Ruby versions
rbenv install --list

# Install specific version
rbenv install 3.3.0
rbenv install 3.2.3
rbenv install jruby-9.4.5.0

# Check installed versions
rbenv versions

# Set global version
rbenv global 3.3.0

# Check current version
rbenv version
ruby --version

Per-Project Management

# Navigate to project directory
cd /path/to/my-rails-app

# Set Ruby version for this project
rbenv local 3.2.3

# .ruby-version file is created
cat .ruby-version
# 3.2.3

# Use different version in another project
cd /path/to/another-project
rbenv local 3.1.4
ruby --version  # ruby 3.1.4

Temporary Version Switching

# Change version only for current shell session
rbenv shell 3.0.6

# Can also specify via environment variable
export RBENV_VERSION=3.0.6

# Revert back
rbenv shell --unset
# or
unset RBENV_VERSION

Integration with Bundler

# Install Bundler
gem install bundler

# Use Bundler in project
cd /path/to/my-project
bundle init

# Add dependencies to Gemfile
echo 'gem "rails", "~> 7.1.0"' >> Gemfile

# Install dependencies
bundle install

# Run rbenv rehash (to recognize new gem commands)
rbenv rehash

Useful Commands

# Show all installable Ruby versions
rbenv install --list-all

# Show path of current Ruby
rbenv which ruby

# Check which version executes a specific command
rbenv which irb

# Regenerate shims (after installing new gems)
rbenv rehash

# Update rbenv itself
cd ~/.rbenv
git pull

# Update ruby-build
cd ~/.rbenv/plugins/ruby-build
git pull