Skip to content

Ruby Installation and Environment Setup

Before starting to learn Ruby programming, we need to install the Ruby runtime environment. This chapter will detail how to install Ruby on different operating systems and configure the development environment.

🖥️ Installing Ruby on Different Operating Systems

Windows System

  1. Visit RubyInstaller Official Website
  2. Download the latest version of Ruby+Devkit installer
  3. Run the installer and complete the installation following the prompts
  4. Make sure to check "Add Ruby to PATH environment variable" option
bash
# Verify installation
ruby -v
# Output example: ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x64-mingw32]

gem -v
# Output example: 3.2.0

Method Two: Using Chocolatey Package Manager

bash
# After installing Chocolatey, execute
choco install ruby

macOS System

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

# Install Ruby
brew install ruby

Method Two: Using rbenv Version Manager

bash
# Install rbenv
brew install rbenv

# Initialize rbenv
rbenv init

# Install the latest version of Ruby
rbenv install 3.0.0
rbenv global 3.0.0

Linux System (Ubuntu/Debian)

Using Package Manager to Install

bash
# Update package list
sudo apt update

# Install Ruby
sudo apt install ruby-full

# Verify installation
ruby -v
gem -v
bash
# Install dependencies
sudo apt install git curl libssl-dev libreadline-dev zlib1g-dev autoconf bison build-essential libyaml-dev libreadline-dev libncurses5-dev libffi-dev libgdbm-dev

# Clone rbenv
git clone https://github.com/rbenv/rbenv.git ~/.rbenv

# Add to PATH
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc

# Reload shell configuration
source ~/.bashrc

# Install ruby-build plugin
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build

# Install Ruby
rbenv install 3.0.0
rbenv global 3.0.0

🛠️ Development Tool Configuration

Visual Studio Code

  1. Install VS Code
  2. Install Ruby extension pack:
    • Ruby
    • Ruby Solargraph
    • Endwise

Sublime Text

  1. Install Package Control
  2. Install Ruby-related packages

Atom

  1. Install ruby-block extension
  2. Install language-ruby extension

RubyMine

Professional Ruby IDE from JetBrains with powerful features:

  • Intelligent code completion
  • Code refactoring
  • Debugger
  • Version control integration

VS Code with Ruby Extensions

Lightweight but full-featured development environment.

📦 Ruby Package Manager

RubyGems

Ruby's default package manager for installing and managing Ruby libraries (called gems).

bash
# View installed gems
gem list

# Install gem
gem install rails

# Uninstall gem
gem uninstall rails

# Update gem
gem update rails

# View gem information
gem info rails

Bundler

A tool for managing project dependencies.

bash
# Install Bundler
gem install bundler

# Initialize Gemfile
bundle init

# Install dependencies specified in Gemfile
bundle install

# Update dependencies
bundle update

📁 Project Structure Setup

Basic Project Structure

my_ruby_project/
├── Gemfile          # Dependency declaration file
├── Gemfile.lock     # Dependency version lock file
├── README.md        # Project documentation
├── lib/             # Library files directory
│   └── my_project.rb
├── bin/             # Executable files directory
│   └── my_script
├── spec/            # Test files directory
│   └── my_project_spec.rb
└── lib/             # Source code directory

Gemfile Example

ruby
source 'https://rubygems.org'

gem 'rails', '~> 6.1.0'
gem 'sqlite3', '~> 1.4'
gem 'puma', '~> 5.0'
gem 'sass-rails', '>= 6'
gem 'webpacker', '~> 5.0'
gem 'turbo-rails'
gem 'stimulus-rails'
gem 'bcrypt', '~> 3.1.7'

🔧 Environment Variable Configuration

PATH Environment Variable

Ensure Ruby and Gem executables are in PATH:

bash
# Add to ~/.bashrc or ~/.zshrc
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"

RUBY_ENV Environment Variable

Used to set Ruby environment:

bash
# Development environment
export RUBY_ENV=development

# Production environment
export RUBY_ENV=production

# Test environment
export RUBY_ENV=test

🧪 Verify Installation

Basic Verification

bash
# Check Ruby version
ruby -v

# Check Gem version
gem -v

# Check Bundler version
bundle -v

# Check Ruby installation path
which ruby

Interactive Ruby (IRB)

Ruby's built-in interactive interpreter for quick code testing:

bash
# Start IRB
irb

# Test in IRB
irb(main):001:0> puts "Hello, Ruby!"
Hello, Ruby!
=> nil

irb(main):002:0> 2 + 3
=> 5

irb(main):003:0> exit

Simple Test Program

Create a simple Ruby program to verify installation:

ruby
# hello.rb
puts "Hello, Ruby World!"
puts "Ruby version: #{RUBY_VERSION}"
puts "Platform: #{RUBY_PLATFORM}"

# Run the program
# ruby hello.rb

🚀 Your First Ruby Program

Create and Run Program

  1. Create a new directory for Ruby project:
bash
mkdir my_first_ruby_app
cd my_first_ruby_app
  1. Create your first Ruby program:
ruby
# app.rb
# First Ruby program
class HelloWorld
  def initialize(name)
    @name = name
  end
  
  def greet
    puts "Hello, #{@name}! Welcome to the Ruby world!"
  end
end

# Create object and call method
hello = HelloWorld.new("Learner")
hello.greet
  1. Run the program:
bash
ruby app.rb
# Output: Hello, Learner! Welcome to the Ruby world!

🔧 Common Issues and Solutions

1. Permission Issues

bash
# If you encounter permission issues, use sudo
sudo gem install bundler

2. Version Conflicts

bash
# Use rbenv to manage multiple Ruby versions
rbenv versions          # View installed versions
rbenv global 3.0.0      # Set global version
rbenv local 2.7.0       # Set project local version

3. Network Issues

bash
# Use domestic mirror source
gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/

🎯 Best Practices

1. Use Version Manager

It is recommended to use rbenv or RVM to manage Ruby versions to avoid issues from system-level installation.

2. Use Bundler for Dependency Management

Always use Gemfile to declare project dependencies to ensure team members use the same dependency versions.

3. Configure Development Environment

  • Set up appropriate editor
  • Configure code formatting tools
  • Install necessary development tools

📚 Next Steps

Now that you have successfully installed and configured the Ruby development environment, you can continue learning:

Continue your Ruby learning journey!

Content is for learning and research only.