0
0
Rubyprogramming~5 mins

Ruby version management (rbenv, rvm)

Choose your learning style9 modes available
Introduction

Ruby version management helps you switch between different Ruby versions easily. This is useful because different projects may need different Ruby versions.

You work on multiple Ruby projects that require different Ruby versions.
You want to try a new Ruby version without affecting your current projects.
You need to test your code on several Ruby versions to ensure compatibility.
You want to keep your system Ruby clean and avoid conflicts.
You want to install Ruby versions without admin rights on your computer.
Syntax
Ruby
rbenv install <version>
rbenv global <version>
rvm install <version>
rvm use <version>

rbenv commands help you install and set Ruby versions globally or per project.

rvm commands also install and switch Ruby versions, with some extra features like gemsets.

Examples
Install Ruby 3.2.2 and set it as the default Ruby version for your system using rbenv.
Ruby
rbenv install 3.2.2
rbenv global 3.2.2
Install Ruby 3.1.0 and switch to it immediately using rvm.
Ruby
rvm install 3.1.0
rvm use 3.1.0
Set Ruby 2.7.6 only for the current project folder using rbenv.
Ruby
rbenv local 2.7.6
Create a gemset named 'myproject' and use Ruby 3.0.0 with that gemset using rvm.
Ruby
rvm gemset create myproject
rvm use 3.0.0@myproject
Sample Program

This script installs Ruby 3.2.2 using rbenv, sets it as the default Ruby version, updates shims, and then prints the Ruby version to confirm.

Ruby
# This is a shell script example showing rbenv usage

# Install Ruby 3.2.2
rbenv install 3.2.2

# Set Ruby 3.2.2 as global default
rbenv global 3.2.2

# Run rbenv rehash to update shims
rbenv rehash

# Check Ruby version
ruby -v
OutputSuccess
Important Notes

After installing a Ruby version with rbenv, run rbenv rehash to update shims.

rvm can manage gemsets, which are separate spaces for gems per Ruby version.

Always check your current Ruby version with ruby -v after switching.

Summary

Ruby version managers like rbenv and rvm help you switch Ruby versions easily.

Use them to keep projects isolated and avoid conflicts.

They make it simple to try new Ruby versions without breaking your system setup.