[Environment construction] Get the Ruby on Rails 6 development environment within 1 hour

Contents

It is a royal road learning for programming beginners who have begun to think "I want to start programming!" And "I want to be a web engineer!" To challenge Ruby and Ruby on Rails, a framework that uses it. I think it's a pattern.

The first big barrier to getting started with Ruby on Rails is building an 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 would like as many programming beginners as possible to feel the appeal of Rails, so I will introduce the procedure for that purpose with the goal of "getting a Ruby on Rails 6 development environment within an hour". To do.

update information

[2020/01/07 Vagarntfile update]

  ~

Fixed a bug where the # rails dbconsole command couldn't be used

  echo '### installing SQLITE3 ###'
  sudo apt install libsqlite3-dev
  sudo apt install sqlite3
  ~

Fixed a bug where # rails test failed

  echo '### increasing inotify ###'
  sudo sh -c "echo fs.inotify.max_user_watches=524288 >> /etc/sysctl.conf"
  sudo sysctl -p
  ~

goal

Build an Ubuntu virtual environment with a Ruby on Rails 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 1, 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 1, 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 rails6_dev
$ cd rails6_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 = '6.0.3.4'
  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
      sudo npm install yarn -g
      sudo chown -R $USER:$GROUP ~/.config
      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!

(The source code of this Vagrantfile is also given on Github.)

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 "7. 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
vagrant@vagrant:~/work/sample-app$ 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.

7. End ssh connection

$ exit

You can terminate the SSH connection with the above command.

8. 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).

9. How to proceed with Rails application development

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.

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, we would appreciate it if you could comment or DM on Twitter!

Recommended Posts

[Environment construction] Get the Ruby on Rails 6 development environment within 1 hour
[Environment construction] Ruby on Rails 5.2 system development environment construction [within 1 hour]
Ruby on Rails development environment construction on M1 Mac
Docker the development environment of Ruby on Rails project
Ruby on Rails 6.0 environment construction memo
[Procedure 1 for beginners] Ruby on Rails: Construction of development environment
[Docker] Development environment construction Rails6 / Ruby2.7 / MySQL8
Muscle Ruby on Rails Day 1 ~ Environment Construction ~
Ruby on Rails --From environment construction to simple application development on WSL2
Ruby on Rails development environment construction with Docker + VSCode (Remote Container)
[Personal memo] Ruby on Rails environment construction (Windows)
How to solve the local environment construction of Ruby on Rails (MAC)!
Rails6 development environment construction [Mac]
Rails engineer environment construction ruby2.7.1
Deploy to Ruby on Rails Elastic beanstalk (Environment construction)
[Environment construction Mac] Ruby on Rails (+ Webpacker handles errors)
Ruby on Rails environment construction using VirtualBox, Vagrant, cyberduck
From 0 to Ruby on Rails environment construction [macOS] (From Homebrew installation to Rails installation)
Rails on Docker environment construction procedure
Build a development environment where Ruby on Rails breakpoints work on Windows
Create a development environment for Ruby 3.0.0 and Rails 6.1.0 on Ubuntu 20.04.1 LTS
Steps to build a Ruby on Rails development environment with Vagrant
part of the syntax of ruby ​​on rails
[Ruby] Building a Ruby development environment on Ubuntu
CentOS8.2 (x86_64) + ruby2.5 + Rails5.2 + MariaDB (10.3.17) environment construction
[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 development environment with Docker (Rails 6.x)
Environment construction of Ruby on Rails from 0 [Cloud9] (From Ruby version change to Rails installation)
[Error] Switch environment construction to use Ruby on Rails oss (open source)
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
When the Ruby on Rails terminal rolls back
[Ruby on Rails] Let's build an environment on mac
Determine the current page with Ruby on Rails
Rails Docker environment construction
Ruby on Rails Elementary
Ruby on Rails basics
java development environment construction
Ruby On Rails Association
Introduction to Slay the Spire Mod Development (2) Development Environment Construction
Java development environment construction on Mac-JDK Install (2020 preservation version)
[Ruby on Rails] How to change the column name
Solve the N + 1 problem with Ruby on Rails: acts-as-taggable-on
[Ruby on Rails] From MySQL construction to database change
(Ruby on Rails6) Reflecting the posted content from the form
Try using the query attribute of Ruby on Rails
Environment construction method and troubleshooter at the time of joint development (rails, docker and github)
[Ruby on Rails] Only the user who posted can edit
Ruby on rails learning record -2020.10.03
Difficulties in building a Ruby on Rails environment (Windows 10) (SQLite3)
Definitely useful! Debug code for development in Ruby on Rails
Portfolio creation Ruby on Rails
Ruby on rails learning record -2020.10.04
[Ruby on Rails] Debug (binding.pry)
Ruby on rails learning record -2020.10.05
Ruby on rails learning record -2020.10.09
Ruby on Rails config configuration
[Ruby on Rails] Quickly display the page title in the browser
Ruby on Rails basic learning ①
[Ruby on Rails] about has_secure_password
(Ruby on Rails6) Display of the database that got the id of the database
Ruby on rails learning record-2020.10.07 ②