Ruby Version Management
Master Ruby version management with rbenv and rvm. Learn how to install, switch between, and manage multiple Ruby versions for different projects.
Why Version Management?
Version Management: Essential for working with different Ruby projects that may require different Ruby versions for compatibility and stability.
Benefits Why Use Version Management?
- Project isolation: Different projects, different versions
- Easy switching: Change versions instantly
- No conflicts: Avoid system Ruby issues
- Team consistency: Everyone uses the same version
- Testing compatibility: Test on multiple versions
Scenarios Common Use Cases
- Legacy projects: Old Rails apps need older Ruby
- New projects: Latest features require newer Ruby
- Gem compatibility: Some gems need specific versions
- CI/CD: Test against multiple Ruby versions
- Learning: Experiment with different versions
rbenv - Simple Ruby Version Management
rbenv: A lightweight Ruby version management tool that focuses on simplicity and follows the Unix philosophy of doing one thing well.
Installation
# macOS (using Homebrew)
brew install rbenv
# Ubuntu/Debian
sudo apt install rbenv
# Manual installation
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
Basic Commands
# List available versions
rbenv install --list
# Install Ruby version
rbenv install 3.2.0
# Set global version
rbenv global 3.2.0
# Set local version
rbenv local 3.1.0
rbenv Workflow
# Check current version
rbenv version
ruby --version
# List installed versions
rbenv versions
# Set version for project
cd my_project
rbenv local 3.2.0
# Creates .ruby-version file
# Rehash after gem installs
rbenv rehash
RVM - Ruby Version Manager
RVM: A comprehensive Ruby environment management tool that provides gemsets, automatic switching, and extensive configuration options.
Installation
# Install RVM
curl -sSL https://get.rvm.io | bash -s stable
# Reload shell
source ~/.rvm/scripts/rvm
# Install dependencies
rvm requirements
Basic Commands
# List available versions
rvm list known
# Install Ruby version
rvm install 3.2.0
# Use Ruby version
rvm use 3.2.0
# Set default version
rvm --default use 3.2.0
RVM Gemsets
# Create gemset
rvm gemset create myapp
# Use gemset
rvm use 3.2.0@myapp
# List gemsets
rvm gemset list
# Create .rvmrc file
echo "rvm use 3.2.0@myapp" > .rvmrc
# Auto-switch on directory change
cd my_project # Automatically switches to 3.2.0@myapp
rbenv vs RVM
rbenv Pros & Cons
Pros:
- Lightweight and fast
- Simple and predictable
- Doesn't modify shell
- Easy to understand
Cons:
- No gemsets
- Manual switching
- Less features
RVM Pros & Cons
Pros:
- Gemsets for isolation
- Auto-switching
- Rich feature set
- Easy project setup
Cons:
- Modifies shell
- More complex
- Can be slower
Best Practices
Version Management Best Practices
- Use .ruby-version files: Specify Ruby version per project
- Keep versions updated: Use latest stable versions
- Document requirements: Include Ruby version in README
- Use CI/CD: Test against multiple Ruby versions
- Team consistency: Everyone uses the same version manager
Common Mistakes to Avoid
- Not using version managers: System Ruby can cause conflicts
- Mixing rbenv and RVM: Choose one and stick with it
- Forgetting .ruby-version: Team members get different versions
- Not updating regularly: Miss security updates and features
- Ignoring compatibility: Test with target Ruby versions
What's Next?
Continue Your Ruby Journey
- Practice with Projects: Set up different Ruby versions for different projects
- Learn CI/CD: Configure automated testing with multiple Ruby versions
- Explore Advanced Features: Learn about gemsets, auto-switching, and configuration
- Team Workflows: Establish version management practices for your team
- Advanced Topics: Explore Docker, containers, and deployment strategies