0
0
RubyHow-ToBeginner · 4 min read

How to Install Ruby Using rbenv: Step-by-Step Guide

To install Ruby using rbenv, first install rbenv and ruby-build plugins, then run rbenv install <version> to download and compile Ruby. Finally, set the installed Ruby version globally with rbenv global <version>.
📐

Syntax

The main commands to install Ruby using rbenv are:

  • rbenv install <version>: Downloads and compiles the specified Ruby version.
  • rbenv global <version>: Sets the default Ruby version for your system.
  • rbenv local <version>: Sets Ruby version for the current project directory.
  • rbenv versions: Lists all installed Ruby versions.
bash
rbenv install 3.2.2
rbenv global 3.2.2
rbenv versions
Output
Downloading ruby-3.2.2... Installing ruby-3.2.2... Installed ruby-3.2.2 to ~/.rbenv/versions/3.2.2 3.2.2 (set by user) * system (set by /home/user/.rbenv/version)
💻

Example

This example shows how to install Ruby version 3.2.2 using rbenv, set it as the global default, and verify the installation.

bash
curl -fsSL https://github.com/rbenv/rbenv-installer/raw/main/bin/rbenv-installer | bash
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
rbenv install 3.2.2
rbenv global 3.2.2
ruby -v
Output
Downloading ruby-3.2.2... Installing ruby-3.2.2... Installed ruby-3.2.2 to ~/.rbenv/versions/3.2.2 ruby 3.2.2p0 (2024-03-15 revision 12345) [x86_64-linux]
⚠️

Common Pitfalls

Common mistakes when installing Ruby with rbenv include:

  • Not installing ruby-build plugin, which is needed to compile Ruby versions.
  • Forgetting to add rbenv to your shell's PATH and initialize it, causing commands to fail.
  • Not running rbenv rehash after installing Ruby or gems, which can cause executables to be missing.
  • Trying to install Ruby without required build dependencies like gcc, make, or libraries.
bash
Wrong:
rbenv install 3.2.2
ruby -v

Right:
# Install ruby-build plugin first
# Add rbenv to PATH and initialize
rbenv install 3.2.2
rbenv global 3.2.2
rbenv rehash
ruby -v
📊

Quick Reference

CommandDescription
rbenv install Download and install specified Ruby version
rbenv global Set default Ruby version globally
rbenv local Set Ruby version for current directory
rbenv versionsList installed Ruby versions
rbenv rehashUpdate shims for Ruby executables

Key Takeaways

Always install ruby-build plugin before installing Ruby versions with rbenv.
Add rbenv to your shell PATH and initialize it to use rbenv commands.
Use rbenv install to download and compile Ruby versions.
Set the Ruby version globally with rbenv global for system-wide use.
Run rbenv rehash after installing Ruby or gems to update executable shims.