Post

How to Install rbenv on macOS

Set up rbenv on macOS to handle multiple Ruby versions for each project.

How to Install rbenv on macOS

Managing multiple Ruby versions can be challenging, especially when different projects require different Ruby setups. rbenv makes this process seamless, allowing you to switch Ruby versions effortlessly. Whether you’re working with tools like Fastlane or Jekyll for automation and blogging, rbenv ensures each project uses the correct version of Ruby.

In this guide, I’ll walk you through how to install rbenv using Homebrew.

Why rbenv?

rbenv is a lightweight Ruby version manager that helps you:

  • Manage multiple Ruby versions on the same machine.
  • Automatically use the correct Ruby version for each project.
  • Avoid conflicts between system and project-specific Ruby versions.

1. Installing rbenv with Homebrew

To install rbenv via Homebrew on macOS, follow these steps:

Step 1: Install Homebrew (if not already installed)

Check out my How to Install Homebrew on macOS post for detailed instructions on how to install Homebrew.

Step 2: Install rbenv

With Homebrew installed, you can now install rbenv by running:

1
brew install rbenv

Step 3: Initialize rbenv

Once rbenv is installed, you need to initialize it so it can manage Ruby versions. Add the following line to your shell configuration file:

For bash or zsh, add this to your .bash_profile or .zshrc:

1
2
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
source ~/.bash_profile   # For bash
1
2
echo 'eval "$(rbenv init -)"' >> ~/.zshrc
source ~/.zshrc          # For zsh

This step ensures rbenv is loaded each time you open a new terminal window.

Step 4: Install Ruby Versions

With rbenv set up, you can now install Ruby versions using:

1
rbenv install 3.1.2  # Example: Install Ruby 3.1.2

Step 5: Set Global or Local Ruby Versions

  • Global version: Set the default Ruby version for all projects on your system:

    1
    
    rbenv global 3.1.2
    
  • Local version: Set a Ruby version for a specific project. Run this command in your project directory:

    1
    
    rbenv local 3.1.2
    

This will create a .ruby-version file in the project directory, ensuring that rbenv switches to this version whenever you’re working in that project.

Step 6: Verify the Installation

To confirm that rbenv is working correctly, run:

1
2
rbenv versions   # Lists all installed Ruby versions
rbenv version    # Shows the current active Ruby version

2. Installing Gems

After installing and setting your desired Ruby version, you can install Ruby gems without needing sudo. For example, to install Bundler:

1
gem install bundler

3. Managing Ruby Versions

  • List installed Ruby versions:

    1
    
    rbenv versions
    
  • Switch between Ruby versions:

    1
    2
    
    rbenv global <version>  # Globally
    rbenv local <version>   # For a project
    
  • Remove a Ruby version:

    1
    
    rbenv uninstall <version>
    

By following these steps, you’ll have rbenv installed and ready to manage multiple Ruby versions seamlessly. Whether you’re working on different projects or switching between development tools like Jekyll or Fastlane, rbenv keeps your Ruby environment organized and hassle-free.

This post is licensed under CC BY 4.0 by the author.