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.
[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
~
Build an Ubuntu virtual environment with a Ruby on Rails 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 1, 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 1, 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 rails6_dev
$ cd rails6_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 = '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.)
$ 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 "7. 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
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.
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).
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.
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