Ruby on Rails - Installation
Ruby on Rails - Modern Installation Guide
Section titled “Ruby on Rails - Modern Installation Guide”To develop web applications with Ruby on Rails, you need to install several key components. This guide covers modern installation practices. As of writing, current stable versions are around Ruby 3.2.x and Rails 7.1.x. Always check the official Ruby and Rails websites for the latest recommended versions.
- Ruby: The programming language.
- RubyGems: The package manager for Ruby libraries (gems).
- Bundler: Manages application-specific gem dependencies.
- Rails Framework: The Rails gem itself.
- Node.js and Yarn/npm: For JavaScript asset management (Rails uses tools like esbuild, which often require a JS runtime).
- A Database System: SQLite is the default for new apps (easy setup). PostgreSQL or MySQL are common for development and production.
Rails comes with the Puma web server by default, suitable for both development and production.
Rails Installation on Windows
Section titled “Rails Installation on Windows”The recommended way to install Ruby and Rails on Windows is using RubyInstaller.
Step 1: Install Ruby with RubyInstaller
Section titled “Step 1: Install Ruby with RubyInstaller”- Visit rubyinstaller.org.
- Download the recommended Ruby+Devkit installer (e.g., Ruby+Devkit 3.2.x (x64)). The Devkit is crucial for installing gems with native extensions.
- Run the installer. Accept the default settings and ensure the “Add Ruby executables to your PATH” option is checked.
- After the Ruby installation finishes, a command prompt window will appear asking which components to install. Choose option
1(MSYS2 base installation) and then3(MSYS2 and MINGW development toolchain). This installs necessary tools for compiling gems.
Step 2: Verify Ruby and RubyGems Installation
Section titled “Step 2: Verify Ruby and RubyGems Installation”Open a new Command Prompt (cmd.exe or PowerShell) and type:
C:\> ruby -vC:\> gem -vYou should see the installed Ruby version (e.g., ruby 3.2.2p53) and Gem version.
Step 3: Install Bundler and Rails
Section titled “Step 3: Install Bundler and Rails”C:\> gem install bundlerC:\> gem install railsThis installs the latest stable version of Rails. The gem install rails command might take a few minutes as it downloads and installs Rails and its many dependency gems. You’ll see output listing the gems being installed.
Step 4: Install Node.js and Yarn (Optional but Recommended)
Section titled “Step 4: Install Node.js and Yarn (Optional but Recommended)”Modern Rails uses JavaScript extensively. Install Node.js (which includes npm) from nodejs.org. After installing Node.js, you can install Yarn (another package manager often used with Rails) via npm:
C:\> npm install --global yarnStep 5: Verify Rails Installation
Section titled “Step 5: Verify Rails Installation”C:\> rails -vThis should output the Rails version, e.g., Rails 7.1.2.
Congratulations! You have successfully installed Ruby on Rails on Windows.
Rails Installation on Linux (using rbenv)
Section titled “Rails Installation on Linux (using rbenv)”Using a Ruby version manager like rbenv or RVM is highly recommended on Linux and macOS. This guide uses rbenv.
Step 1: Install Prerequisite Dependencies
Section titled “Step 1: Install Prerequisite Dependencies”Dependencies vary by distribution. For Debian/Ubuntu:
$ sudo apt update$ sudo apt install -y git curl libssl-dev libreadline-dev zlib1g-dev autoconf bison build-essential libyaml-dev libreadline-dev libncurses5-dev libffi-dev libgdbm-dev libjemalloc-devFor Fedora/CentOS/RHEL:
$ sudo dnf install -y git curl autoconf bison gcc-c++ readline-devel zlib-devel openssl-devel libyaml-devel libffi-devel make bzip2 patchStep 2: Install rbenv and ruby-build
Section titled “Step 2: Install rbenv and ruby-build”$ git clone https://github.com/rbenv/rbenv.git ~/.rbenv$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc # Or ~/.zshrc if using Zsh$ echo 'eval "$(rbenv init -)"' >> ~/.bashrc # Or ~/.zshrc$ exec $SHELL
$ git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-buildRestart your shell or run exec $SHELL for changes to take effect.
Step 3: Install Ruby
Section titled “Step 3: Install Ruby”List available Ruby versions: rbenv install -l. Then install a recent stable version (e.g., 3.2.2):
$ rbenv install 3.2.2$ rbenv global 3.2.2 # Set this version as defaultVerify:
$ ruby -v# Expected output: ruby 3.2.2p53 (YYYY-MM-DD revision XXXXXX) [architecture]Disable documentation installation for gems (optional, speeds up gem installs):
$ echo "gem: --no-document" > ~/.gemrcStep 4: Install Bundler and Rails
Section titled “Step 4: Install Bundler and Rails”$ gem install bundler$ gem install rails$ rbenv rehash # Important after installing gems that provide executablesVerify Rails:
$ rails -v# Expected output: Rails 7.1.2 (or similar latest version)Step 5: Install Node.js and Yarn
Section titled “Step 5: Install Node.js and Yarn”Install Node.js (e.g., LTS version) and Yarn. Using NodeSource repositories is common on Linux:
# For Debian/Ubuntu (example for Node.js 20.x)$ curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -$ sudo apt-get install -y nodejs
# Install Yarn globally via npm$ sudo npm install --global yarnRefer to Node.js and Yarn documentation for specific instructions for your distribution.
Step 6: Install a Database (Example: PostgreSQL on Ubuntu)
Section titled “Step 6: Install a Database (Example: PostgreSQL on Ubuntu)”$ sudo apt install postgresql postgresql-contrib libpq-dev$ sudo -u postgres createuser -s $(whoami) # Creates a PostgreSQL superuser with your Linux username (for easy local dev)Congratulations! You have successfully installed Ruby on Rails on Linux.
Keeping Rails Up-to-Date in a Project
Section titled “Keeping Rails Up-to-Date in a Project”To update the Rails gem for a specific project, navigate to your project directory and run:
$ bundle update railsTo update the globally installed Rails gem (if you installed it outside a project context, less common for project work):
$ gem update rails$ rbenv rehash # If using rbenvInstallation Verification: Creating Your First Rails App
Section titled “Installation Verification: Creating Your First Rails App”Let’s create a new Rails application to verify the installation:
$ rails new demo_appThis command will generate a new Rails application in a directory named demo_app. You’ll see a lot of output as Rails creates files and directories, and runs bundle install to install default gems. Output typically includes lines like create app/controllers/application_controller.rb, run bundle install, etc.
Now, navigate into your new application’s directory and start the Rails development server:
$ cd demo_app$ bin/rails server # or simply 'rails s'You should see output indicating the Puma web server is starting, similar to:
=> Booting Puma
=> Rails 7.1.2 application starting in development
=> Run bin/rails server —help for more startup options
Puma starting in single mode...
* Puma version: X.Y.Z (ruby A.B.C-p...)
* Min threads: 5, max threads: 5
* Listening on http://localhost:3000
Use Ctrl-C to stop
Open your web browser and navigate to http://localhost:3000.
You should see the default Rails welcome page, often a simple page with a Ruby on Rails logo and information indicating your application is running successfully in development mode. This confirms your Rails installation is working!