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.
The environment will be built using Virtual Box + Vagrant instead of AWS Could9 recommended by the Rails tutorial.
Click here for sister articles of this article [Environment construction] Get Ruby on Rails 6 development environment within 1 hour [Environment construction] Ruby on Rails 5.2 system development environment construction [within 1 hour]
Build an Ubuntu virtual environment with a Ruby on Rails 5.1.6 development environment on macOS (target within 1 hour).
Virtual Box 6.1.16
Vagrant 2.2.14
・ 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
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.)
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.)
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!
$ 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.
$ mkdir rails516_dev
$ cd rails516_dev
$ 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!
$ 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.
$ 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.
vagrant@vagrant:~$ cd work
vagrant@vagrant:~/work$ rails new sample_app
vagrant@vagrant:~/work$ cd sample_app
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.
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.
You can stop the Rails server you started with command + C
.
$ exit
You can terminate the SSH connection with the above command.
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).
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