Ruby Installation

Set up Ruby on your computer

⚙️ Installing Ruby

Installing Ruby is straightforward on all major operating systems. You can use official installers, version managers, or package managers. Once installed, you can run Ruby programs from your terminal or command prompt.


# Check if Ruby is installed
ruby --version

# Run a Ruby program
ruby myprogram.rb
                                    

Installation Methods

🪟

Windows

Use RubyInstaller for easy setup

RubyInstaller WSL
🍎

macOS

Pre-installed or use Homebrew

Homebrew rbenv
🐧

Linux

Use package manager or rbenv

apt yum rbenv
🌐

Online

Try Ruby without installing

Replit TryRuby

🔹 Windows Installation

RubyInstaller is the easiest way to install Ruby on Windows. It includes Ruby, essential tools, and a development kit.

Installation Steps:

  1. Visit rubyinstaller.org
  2. Download the latest Ruby+Devkit installer
  3. Run the installer and follow the wizard
  4. Select "Add Ruby to PATH" option
  5. Complete the MSYS2 installation when prompted
# Verify installation in Command Prompt
ruby --version
# Output: ruby 3.2.0 (or your version)

# Test with a simple program
ruby -e "puts 'Ruby is installed!'"

Output:

Ruby is installed!

🔹 macOS Installation

macOS comes with Ruby pre-installed, but you can install a newer version using Homebrew for better control and updates.

🔸 Using Homebrew (Recommended)

# Install Homebrew first (if not installed)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

# Install Ruby
brew install ruby

# Add to PATH (add to ~/.zshrc or ~/.bash_profile)
echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >> ~/.zshrc

# Verify installation
ruby --version

🔸 Check Pre-installed Ruby

# Check current Ruby version
ruby --version

# Test Ruby
ruby -e "puts 'Hello from macOS!'"

Output:

Hello from macOS!

🔹 Linux Installation

Linux users can install Ruby using their distribution's package manager. Different distributions use different commands.

🔸 Ubuntu/Debian

# Update package list
sudo apt update

# Install Ruby
sudo apt install ruby-full

# Verify installation
ruby --version

🔸 Fedora/CentOS

# Install Ruby
sudo yum install ruby

# Or on newer Fedora
sudo dnf install ruby

# Verify installation
ruby --version

🔸 Test Installation

# Create a test file
echo 'puts "Ruby on Linux!"' > test.rb

# Run the file
ruby test.rb

Output:

Ruby on Linux!

🔹 Using Version Managers

Version managers like rbenv or RVM allow you to install and switch between multiple Ruby versions easily. This is useful for developers working on different projects.

🔸 rbenv (Recommended)

# Install rbenv (macOS/Linux)
curl -fsSL https://github.com/rbenv/rbenv-installer/raw/main/bin/rbenv-installer | bash

# Install a Ruby version
rbenv install 3.2.0

# Set global Ruby version
rbenv global 3.2.0

# Verify
ruby --version

Why use a version manager?

  • Install multiple Ruby versions
  • Switch between versions per project
  • Avoid system Ruby conflicts
  • Easy updates and management

🔹 Online Ruby Environments

Try Ruby without installing anything on your computer using online platforms:

🔸 Replit Example

# Write and run Ruby code online
name = "Developer"
puts "Welcome, #{name}!"
puts "Start coding Ruby now!"

Output:

Welcome, Developer!
Start coding Ruby now!

Popular Online Platforms:

  • Replit: Full IDE in browser
  • TryRuby: Interactive Ruby tutorial
  • Ruby Fiddle: Quick code testing

🔹 Verify Your Installation

After installation, verify everything works correctly:

# Check Ruby version
ruby --version

# Check gem (Ruby package manager)
gem --version

# Run interactive Ruby
irb

🔸 Create Your First Ruby File

# Save as hello.rb
puts "Ruby installation successful!"
puts "Current time: #{Time.now}"
puts "Ready to code!"
# Run the file
ruby hello.rb

Output:

Ruby installation successful!
Current time: 2024-01-15 10:30:45 +0000
Ready to code!

🧠 Test Your Knowledge

What command checks your Ruby version?