[Ruby] Steps to build Ruby on Rails development environment with Vagrant

2 minute read

environment

macOS 10.15.4

Install #Vagrant Install Vagrant from the home page.

Screenshot 2020-07-12 16.09.53.png https://www.vagrantup.com

Cent OS installed

First, check if Vagrant is installed.

terminal


$ vagrant -v
Vagrant 2.2.9

Create a directory and move it.

terminal


$ mkdir -p vagrant/centos7
$ cd vagrant
$ cd centos7

Execute the following command in the moved directory.

terminal


$ vagrant init centos/7

After the installation is complete, open the editor and uncomment the following line.

Vagrantfile


# config.vm.network "private_network", ip: "192.168.33.10"

Screenshot 2020-07-12 16.22.25.png

Run the command below and wait a moment…

terminal


$ vagrant up

Log in to CentOS

Execute the following command to log in.

terminal


$ vagrant ssh
[vagrant@localhost ~]$

install rbenv

Prepare the yum package.

terminal


$ sudo yum install -y git gcc openssl-devel readline-devel zlib-devel sqlite-devel gcc-c++ libicu-devel cmake vim

Install rbenv.

terminal


$ 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

Check if the installation is completed.

terminal


$ rbenv --version
rbenv 1.1.2-30-gc879cb0

add ruby-build plugin

Execute the following command to add the ruby-build plugin.

terminal


$ git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build

Install Ruby

Install Ruby.

  • As an aside, the version was 2.4 when I referred to someone else’s site. However, Rails 5 or higher and the current mainstream Rails version require 2.5 or higher, so be careful about specifying the version.

terminal


$ rbenv install 2.7.1
$ rbenv global 2.7.1
$ rbenv rehash
$ ruby -v

If you can confirm the completion of installation, proceed to the next step.

Install Bundler

Execute the following command to install Bundler.

terminal


$ gem install bundler
$ rbenv rehash
$ bundle -v
Bundler version 2.1.4

Install Ruby on Rails

Create and move directories for installation.

terminal


$ mkdir -p app/memo_app
$ cd app
$ cd memo_app

Then run the command to install.

terminal


$ bundle init
$ sed -i's/# gem "rails"/gem "rails", "~> 5.1.0"/g' Gemfile
$ bundle install --path vendor/bundler
$ bundle exec rails new.
$ sed -i "s/# gem'therubyracer'/gem'therubyracer'/g" Gemfile
$ bundle install

Start server

Execute the following command to access the address below.

terminal


$ ./bin/rails s -b 0.0.0.0

http://192.168.33.10:3000/

Screenshot 2020-07-12 12.29.09.png

This ends the procedure ^_^

(Supplement) Shutdown and login/logout

I’ll also explain shutdown and login/logout, as you’ll need it when you close it or open it again.

Logout

terminal


# Command to log out from CentOS
$ exit
 
# Command to shut down CentOS
$ vagrant halt
   

Login

terminal


# Command to log in to Cent OS
$ vagrant up
   
$ vagrant ssh
[vagrant@localhost ~]$

Vagrant development environment procedure to create an app with a high-performance editor (VS Code)

A virtual environment of Vagrant has been built, but I will explain how to connect it so that you can also create and edit it with a high-performance editor (VS Code), so please also see ^_^

https://qiita.com/chisaki0606/items/982f296389ffcb850994