[For beginners] Build the environment for Ruby on Rails Tutorial 4th Edition (Rails 5.1) in less than an hour!

Contents

Needless to say, the Ruby on Rails tutorial is a great teaching material. I think that it is a god tutorial that raised me to the level of an engineer who fled me as a complete outsider, and it is the best textbook that I often look back on even now when I got a job as an engineer.

The first big barrier to getting started with the Rails tutorial is building the environment. I myself just started working as a Ruby engineer by changing jobs from a different industry about 3 months ago, so I remember the time when I took the time to "prepare before starting programming learning" such as environment construction.

In this article, I want as many programming beginners as possible to feel the appeal of Rails tutorials, so for the 4th edition of Rails tutorials that are released for free, that is, "Ruby on Rails 5.1.6 development" Here are the steps to get your environment in less than an hour.

goal

Build an Ubuntu virtual environment with a Ruby on Rails 5.1.6 development environment on macOS (target within 1 hour).

Prerequisite environment

macOS Catalina version 10.15.7

Virtual Box 6.1.16

Vagrant 2.2.14

Assumed reader

・ For macOS users -Experience using macOS terminals and knowing the meaning of basic Linux commands (cd, mkdir, ls, etc.) ・ Those who have learned the outline of Ruby on Rails through Progate etc. ・ Those who are feeling the limits of cloud-based integrated development environments such as AWS Could 9 ・ Those who have been frustrated in building an environment for Ruby on Rails in the past

What is a virtual environment?

It refers to an environment in which another OS (guest OS: Ubuntu is used in this article) is included in the OS (host OS: macOS is assumed in this article).

When building a development environment directly on the host OS, there is a possibility that the host OS will be adversely affected due to incorrect settings or deletions, but if you are building an environment on a virtual environment If you make a mistake, you can delete the entire virtual environment and start over, and it will not adversely affect the host OS.
It also has more freedom to expand resources than cloud-based integrated development environments such as AWS Could 9, and rarely suffers from CPU performance limits or memory shortages (at least if you're just developing web applications with Rails). I would say that.

Virtual Box

First, install VirtualBox as "virtualization software" for building a virtual environment.
From the download page below, click the "OS X hosts" link (since this article assumes macOS) to download the installer.
If you start the downloaded installer, the installation procedure is written in an easy-to-understand manner, and if you follow it, the installation of Virtual Box will be completed.
(* The latest version as of January 7, 2021 is 6.1.16, so from here on, it is assumed that it will work with that version.)

Virtual Box Download Page

Vagrant

Next, install Vagrant, a tool that manages virtualization software. If you access the download page below and follow the steps of downloading the installer, starting the installer, and installing the installer in the same way as VirtualBox above, you should be able to complete it with almost no hesitation.
If you can recognize that the tool for operating the virtualization software "VirtualBox" is "Vagrant", the minimum knowledge is OK for the time being.
(* The latest version as of January 7, 2021 is 2.2.14, so from here on, it is assumed that it will work with that version.)

Vagrant Download Page

Environment construction procedure

After installing Virtual Box and Vagrant without any problems, I would like to actually build a virtual environment from here. This time, we will build a virtual environment that contains a guest OS called Ubuntu (18.04).

The operations from here are performed on the macOS terminal. Make sure vagrant is installed correctly before you start working. Open a terminal and try typing vagrant -v. If you get the output Vagrant 2.2.14, you're ready to go, let's get started!

1. Install vagrant-vbguest

$ vagrant plugin install vagrant-vbguest

For the time being, it's OK to understand that it has a role to make the operation in the virtual machine easier.

2. Create a directory anywhere

$ mkdir rails516_dev
$ cd rails516_dev

3. Create Vagrantfile

$ vagrant init

The above command will create a default Vagrantfile.

Vagrantfile(Default|Excerpt)


# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
  ~
  ~
  config.vm.box = "base"
  ~
  ~
  # config.vm.network "forwarded_port", guest: 80, host: 8080
  ~
  ~
  # config.vm.network "private_network", ip: "192.168.33.10"
  ~
  ~
end

Please rewrite the contents of the above default state Vagrantfile with the following code.

Vagrantfile(After rewriting)


# -*- mode: ruby -*-
# vi: set ft=ruby :
# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
  GUEST_RUBY_VERSION = '2.6.6'
  GUEST_RAILS_VERSION = '5.1.6'
  config.vm.box = "bento/ubuntu-18.04"
  config.vm.box_check_update = false
  config.vm.network "forwarded_port", guest: 3000, host: 3000
  config.vm.network "private_network", ip: "192.168.33.10"
  config.vm.synced_folder "./", "/home/vagrant/work"
  config.ssh.forward_agent = true
  config.vm.provider "virtualbox" do |vb|
      vb.gui = false
  end
  config.vm.provision "shell", inline: <<-SHELL
      echo '### installing tools ###'
      sudo timedatectl set-timezone Asia/Tokyo
      sudo apt update -y
      sudo apt upgrade -y
      sudo apt install build-essential -y
      sudo apt install -y libssl-dev libreadline-dev zlib1g-dev
      sudo apt install -y imagemagick
  SHELL
  config.vm.provision "shell", privileged: false, inline: <<-SHELL
      echo '### installing Ruby ###'
      git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
      echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
      echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
      source ~/.bash_profile
      git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
      rbenv install #{GUEST_RUBY_VERSION}
      rbenv global #{GUEST_RUBY_VERSION}
      echo '### installing Rails ###'
      gem install rails -v #{GUEST_RAILS_VERSION}
      echo '### installing SQLITE3 ###'
      sudo apt install libsqlite3-dev
      sudo apt install sqlite3
      echo '### installing NodeJS ###'
      sudo apt install -y nodejs npm
      sudo npm install n -g
      sudo n lts
      sudo apt purge -y nodejs npm
      sudo apt -y autoremove
      echo '### increasing inotify ###'
      sudo sh -c "echo fs.inotify.max_user_watches=524288 >> /etc/sysctl.conf"
      sudo sysctl -p
      echo ' -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-'
      echo 'You are now on Rails!'
      echo ' -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-'
  SHELL
end

I won't explain it in this article, but this Vagrantfile contains the settings to enable Rails. It may seem difficult at first, but we recommend that you read it once.

Of course, this Vagrantfile I wrote is by no means the only correct answer, so comments such as "I have a better way to write it" and "I think this one is easier to use" are welcome!

4. Virtual environment construction

$ vagrant up

Build a virtual environment with the above command (it takes about 15 to 30 minutes depending on the specifications of the PC).
I think it would be nice if you could suppress the fact that the Rails development environment is being created by executing the settings written in the Vagrantfile line by line.

5. SSH connection to virtual environment

$ vagrant ssh

SSH to the virtual environment created by the above command.
Keep in mind that an SSH connection is a mechanism for safely remotely controlling another PC (in this case, a virtual environment) from your own PC via a network.

This is the operation on the macOS terminal. Please be aware that in the following "6. Creating a Rails application" and "8. Closing the ssh connection", you are operating Ubuntu as a guest OS by making an ssh connection from macOS.

6. Rails application creation

vagrant@vagrant:~$ cd work
vagrant@vagrant:~/work$ rails new sample_app
vagrant@vagrant:~/work$ cd sample_app

(Supplement) How to proceed with the development of Rails application

A file with the same name as the Rails application created in "6. Rails application creation" should have been created in the directory created in "2. Create a folder in any location", so develop by rewriting the code there. I will proceed.

Commands such as rails g controller Users and bundle install will be tapped on the directory of the created Rails application with SSH connection.

7. Edit Gemfile

Rewrite Gemfile to below (Rails tutorial Chapter 3 content)

Gemfile


source 'https://rubygems.org'

gem 'rails',        '5.1.6'
gem 'puma',         '3.9.1'
gem 'sass-rails',   '5.0.6'
gem 'uglifier',     '3.2.0'
gem 'coffee-rails', '4.2.2'
gem 'jquery-rails', '4.3.1'
gem 'turbolinks',   '5.0.1'
gem 'jbuilder',     '2.7.0'

group :development, :test do
  gem 'sqlite3', '1.3.13'
  gem 'byebug',  '9.0.6', platform: :mri
end

group :development do
  gem 'web-console',           '3.5.1'
  gem 'listen',                '3.1.5'
  gem 'spring',                '2.0.2'
  gem 'spring-watcher-listen', '2.0.1'
end

group :test do
  gem 'rails-controller-testing', '1.0.2'
  gem 'minitest',                 '5.10.3'
  gem 'minitest-reporters',       '1.1.14'
  gem 'guard',                    '2.16.2'
  gem 'guard-minitest',           '2.4.4'
end

group :production do
  gem 'pg', '0.20.0'
end

#Tzinfo in Windows environment-Must include a gem called data
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

After the above rewrite is finished

bundle update
bundle install --without production
rails s -b 0.0.0.0

If you access http: // localhost: 3000 / in this state, the default screen of Yay! You ’re on Rails! Will be displayed.
rails

You can stop the Rails server you started with command + C.

8. End ssh connection

$ exit

You can terminate the SSH connection with the above command.

9. Shut down the machine in the virtual environment

vagrant halt

You can shut down the guest OS on the virtual environment with the above command.
If you want to start it up again, start it up with vagrant up and connect with vagrant ssh (the second and subsequent times will be completed in a few minutes).

At the end

What did you think? If it goes smoothly, I think it will take less than an hour to display Yay! You ’re on Rails!.

The language called Ruby and the framework called Rails are really interesting and never get tired of touching, so I hope that you will be addicted to its charm as a result of building the environment in this article.

If you have any questions or suggestions for improvements regarding this article, please leave a comment or DM on Twitter!

Recommended Posts

[For beginners] Build the environment for Ruby on Rails Tutorial 4th Edition (Rails 5.1) in less than an hour!
How to build a Ruby on Rails environment using Docker (for Docker beginners)
[Ruby on Rails] Let's build an environment on mac
[Ruby on Rails Tutorial] Error in the test in Chapter 3
[Ruby on Rails] Let's build an environment on mac
Build an environment where pip3 can be used with CentOS7 + Python3
Build a development environment where Ruby on Rails breakpoints work on Windows
Build an environment with Docker on AWS
Build an Ultra96v2 development environment on Docker 1
Try to build Java8 environment on Amazon Linux2
Build a Laravel environment on an AWS instance
[Rails] How to build an environment with Docker
Build a Ruby on Rails development environment on AWS Cloud9
[For beginners] Build the environment for Ruby on Rails Tutorial 4th Edition (Rails 5.1) in less than an hour!
[Procedure 1 for beginners] Ruby on Rails: Construction of development environment
[Environment construction] Get the Ruby on Rails 6 development environment within 1 hour
Build an environment for Rails projects under Git management in Cloud9
Things to remember and concepts in the Ruby on Rails tutorial
Explanation of Ruby on rails for beginners ①
Beginners create portfolio in Ruby on Rails
Build debug environment on container --Build local development environment for Rails tutorial with Docker-
Cloud IDE: Heroku couldn't be installed in the Ruby on Rails tutorial
[Environment construction] Ruby on Rails 5.2 system development environment construction [within 1 hour]
Build a Ruby on Rails development environment on AWS Cloud9
Docker the development environment of Ruby on Rails project
I got an "ActionView :: Template :: Error: Permission denied" error in the test of 3.3.1 of Rails tutorial 6th edition, so I solved it.
Explanation of Ruby on rails for beginners ⑥ ~ Creation of validation ~
Explanation of Ruby on rails for beginners ② ~ Creating links ~
Explanation of Ruby on rails for beginners ⑦ ~ Flash implementation ~
Difficulties in building a Ruby on Rails environment (Windows 10) (SQLite3)
Definitely useful! Debug code for development in Ruby on Rails
Ruby on Rails for beginners! !! Summary of new posting functions
Explanation of Ruby on rails for beginners ③ ~ Creating a database ~
Rails Tutorial (4th Edition) Summary
How to make an application with ruby on rails (assuming that the environment has been built)
Build a development environment where Ruby on Rails breakpoints work on Windows
Explanation of Ruby on rails for beginners ⑤ ~ Edit and delete database ~
Rails Tutorial (6th Edition) -Explanation of background operation for each function-
Create a development environment for Ruby 3.0.0 and Rails 6.1.0 on Ubuntu 20.04.1 LTS
I summarized the flow until implementing simple_calendar in Ruby on Rails.
Steps to build a Ruby on Rails development environment with Vagrant
Ruby on Rails 6.0 environment construction memo
[Ruby on Rails] When logging in for the first time ・ How to split the screen in half using jQuery
Rails Tutorial (4th Edition) Memo Chapter 6
"" Application.js "is not present in the asset pipeline" error in Ruby on Rails
How to build an environment for any version of Ruby using rbenv
Rails tutorial (6th edition) Background operation of the posting function of the micro post
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
Ruby on Rails DB Tips for creating methods to reduce the load
How to solve the local environment construction of Ruby on Rails (MAC)!
How to debug the processing in the Ruby on Rails model only on the console
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
[Cloud9] Yay! You ’re on Rails! Is not displayed in the rails tutorial
Rails Tutorial 6th Edition Learning Summary Chapter 10
Rails Tutorial 6th Edition Learning Summary Chapter 7
[Ruby on Rails] Creating an inquiry form
Rails Tutorial 6th Edition Learning Summary Chapter 4
Ruby on Rails installation method [Mac edition]
Rails Tutorial 6th Edition Learning Summary Chapter 9
Rails Tutorial 6th Edition Learning Summary Chapter 6
Rails Tutorial 6th Edition Learning Summary Chapter 5
Ruby on Rails in Visual Studio Codespaces
Prepare the security check environment for Rails 6
Rails Tutorial 6th Edition Learning Summary Chapter 3
Build an environment with Docker on AWS
[Rails] Reset the database in the production environment
Muscle Ruby on Rails Day 1 ~ Environment Construction ~
Rails tutorial (6th edition) Follow/unfollow background operation
Rails Tutorial 6th Edition Learning Summary Chapter 8
[Ruby on Rails] Solving the addiction when setting crontab using whenever in EC2