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
Method One: Using RubyInstaller (Recommended)
- Visit RubyInstaller Official Website
- Download the latest version of Ruby+Devkit installer
- Run the installer and complete the installation following the prompts
- Make sure to check "Add Ruby to PATH environment variable" option
# Verify installation
ruby -v
# Output example: ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x64-mingw32]
gem -v
# Output example: 3.2.0Method Two: Using Chocolatey Package Manager
# After installing Chocolatey, execute
choco install rubymacOS System
Method One: Using Homebrew (Recommended)
# Install Homebrew (if not already installed)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# Install Ruby
brew install rubyMethod Two: Using rbenv Version Manager
# Install rbenv
brew install rbenv
# Initialize rbenv
rbenv init
# Install the latest version of Ruby
rbenv install 3.0.0
rbenv global 3.0.0Linux System (Ubuntu/Debian)
Using Package Manager to Install
# Update package list
sudo apt update
# Install Ruby
sudo apt install ruby-full
# Verify installation
ruby -v
gem -vUsing rbenv to Install (Recommended)
# 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
Recommended Text Editors
Visual Studio Code
- Install VS Code
- Install Ruby extension pack:
- Ruby
- Ruby Solargraph
- Endwise
Sublime Text
- Install Package Control
- Install Ruby-related packages
Atom
- Install ruby-block extension
- Install language-ruby extension
Recommended IDEs
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).
# 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 railsBundler
A tool for managing project dependencies.
# 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 directoryGemfile Example
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:
# Add to ~/.bashrc or ~/.zshrc
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"RUBY_ENV Environment Variable
Used to set Ruby environment:
# Development environment
export RUBY_ENV=development
# Production environment
export RUBY_ENV=production
# Test environment
export RUBY_ENV=test🧪 Verify Installation
Basic Verification
# Check Ruby version
ruby -v
# Check Gem version
gem -v
# Check Bundler version
bundle -v
# Check Ruby installation path
which rubyInteractive Ruby (IRB)
Ruby's built-in interactive interpreter for quick code testing:
# 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> exitSimple Test Program
Create a simple Ruby program to verify installation:
# 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
- Create a new directory for Ruby project:
mkdir my_first_ruby_app
cd my_first_ruby_app- Create your first Ruby program:
# 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- Run the program:
ruby app.rb
# Output: Hello, Learner! Welcome to the Ruby world!🔧 Common Issues and Solutions
1. Permission Issues
# If you encounter permission issues, use sudo
sudo gem install bundler2. Version Conflicts
# 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 version3. Network Issues
# 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!