Setting Up Ruby and Rails Development Environment

Tutorial 3 of 5

1. Introduction

This tutorial aims to guide you through the process of setting up a Ruby on Rails (RoR) development environment. Ruby on Rails is a popular web development framework that uses the Ruby programming language. By the end of this tutorial, you will have installed Ruby, Rails, and other necessary tools on your system. You will also learn how to configure them for optimal development.

You will learn:

  • How to install Ruby
  • How to install Rails
  • How to set up a simple Rails application

Prerequisites:

  • Basic knowledge of command-line interfaces
  • Access to a computer with an internet connection

2. Step-by-Step Guide

Installing Ruby

Ruby can be installed using a version management tool like rbenv or rvm. For simplicity, we will use rbenv.

  1. Install rbenv and ruby-build

On macOS, you can use Homebrew:

brew install rbenv ruby-build

On Ubuntu, you can use apt:

sudo apt-get install rbenv ruby-build
  1. Add rbenv to bash so that it loads every time you open a Terminal
echo 'if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi' >> ~/.bash_profile
source ~/.bash_profile
  1. Install Ruby
rbenv install 2.7.2
rbenv global 2.7.2
  1. Verify that Ruby was installed correctly
ruby -v

Installing Rails

After installing Ruby, you can install Rails.

  1. Install Rails
gem install rails -v 6.1.0
  1. Verify that Rails was installed correctly
rails -v

3. Code Examples

Let's create a new Rails application.

rails new my_app

This will create a new Rails application called my_app. You can replace my_app with the name of your choice.

Then, navigate into your new application's directory:

cd my_app

Start the Rails server:

rails server

You should see output indicating the server is running. You can visit http://localhost:3000 in your web browser to view your application.

4. Summary

In this tutorial, you've installed Ruby and Rails, set up a simple Rails application, and got it running on your local server.

Next steps for learning:

  • Learn more about Ruby syntax and concepts
  • Explore the Rails framework in depth
  • Build a simple web application with Rails

Additional resources:

5. Practice Exercises

  1. Create a new Rails application with a different name.
  2. Solution: Replace my_app in rails new my_app with your desired application name.

  3. Start the Rails server for your new application.

  4. Solution: Navigate to your application's directory with cd your_app_name, then start the server with rails server.

  5. Visit your application in the web browser.

  6. Solution: With the server running, visit http://localhost:3000 in your web browser. You should see a default Rails page.

Keep practicing by reading and writing more Ruby code, and building Rails applications. The more you practice, the more comfortable you'll become!