概要

仮想環境でRailsの環境構築する備忘録です。

環境

  • ホスト OS X 10.13.1
  • ゲスト CentOS 7
  • Vagrant 2.0.2
  • VirtualBox 5.2.6
  • Rails 5

Vagrantのインストールはこちら
VirtualBoxのインストールはこちらを参考に

手順

1 ディレクトリを作成して移動

terminal
$ mkdir centos
$ cd centos/

2 Vagrantfileの作成

terminal
$ vagrant init centos/7
実行結果
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

3 Vagrantfile を書き換える

Vagrantfile
 # Create a private network, which allows host-only access to the machine
 # using a specific IP.
 # config.vm.network "private_network", ip: "192.168.33.10"

上記の行の「config.vm.network」の行のコメントアウトを外す。

Vagrantfile
 # Create a private network, which allows host-only access to the machine
 # using a specific IP.
 config.vm.network "private_network", ip: "192.168.33.10"

4 Vagrant Up で Vagrant 仮想環境を立ち上げる

terminal
$ vagrant up
実行結果
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'centos/7' could not be found. Attempting to find and install...
    default: Box Provider: virtualbox
    default: Box Version: >= 0
==> default: Loading metadata for box 'centos/7'
    default: URL: https://vagrantcloud.com/centos/7
==> default: Adding box 'centos/7' (v1801.02) for provider: virtualbox
    default: Downloading: https://vagrantcloud.com/centos/boxes/7/versions/1801.02/providers/virtualbox.box
==> default: Successfully added box 'centos/7' (v1801.02) for 'virtualbox'!
==> default: Importing base box 'centos/7'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'centos/7' is up to date...
==> default: Setting the name of the VM: centos_default_1518326878115_8378
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: 
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default: 
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: No guest additions were detected on the base box for this VM! Guest
    default: additions are required for forwarded ports, shared folders, host only
    default: networking, and more. If SSH fails on this machine, please install
    default: the guest additions and repackage the box to continue.
    default: 
    default: This is not an error message; everything may continue to work properly,
    default: in which case you may ignore this message.
==> default: Configuring and enabling network interfaces...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
==> default: Rsyncing folder: ${your current directory} => /vagrant

一応確認する

terminal
$ vagrant status
実行結果
Current machine states:

default                   poweroff (virtualbox)

The VM is powered off. To restart the VM, simply run `vagrant up`

5 CentOS 環境にログイン

terminal
$ vagrant ssh

6 パッケージを更新

terminal
# アップデートできるパッケージの確認
$ sudo yum check-update

# システム全体のアップグレード
$ sudo yum upgrade

7 Gitをインストール

terminal
sudo yum -y install git
実行結果
Failed to set locale, defaulting to C
Loaded plugins: fastestmirror
base                                                                                                                      | 3.6 kB  00:00:00     
extras                                                                                                                    | 3.4 kB  00:00:00     
updates                                                                                                                   | 3.4 kB  00:00:00     
(1/4): extras/7/x86_64/primary_db                                                                                         | 166 kB  00:00:00     
(2/4): base/7/x86_64/group_gz                                                                                             | 156 kB  00:00:00     
(3/4): updates/7/x86_64/primary_db                                                                                        | 6.0 MB  00:00:02     
(4/4): base/7/x86_64/primary_db                                                                                           | 5.7 MB  00:00:03     
Determining fastest mirrors
 * base: ftp.nara.wide.ad.jp
 * extras: www.ftp.ne.jp
 * updates: www.ftp.ne.jp
Resolving Dependencies
--> Running transaction check

*************省略*************

Installed:
  git.x86_64 0:1.8.3.1-12.el7_4                                                                                                                  

Dependency Installed:
  libgnome-keyring.x86_64 0:3.12.0-1.el7         perl.x86_64 4:5.16.3-292.el7                 perl-Carp.noarch 0:1.26-244.el7                   
  perl-Encode.x86_64 0:2.51-7.el7                perl-Error.noarch 1:0.17020-2.el7            perl-Exporter.noarch 0:5.68-3.el7                 
  perl-File-Path.noarch 0:2.09-2.el7             perl-File-Temp.noarch 0:0.23.01-3.el7        perl-Filter.x86_64 0:1.49-3.el7                   
  perl-Getopt-Long.noarch 0:2.40-2.el7           perl-Git.noarch 0:1.8.3.1-12.el7_4           perl-HTTP-Tiny.noarch 0:0.033-3.el7               
  perl-PathTools.x86_64 0:3.40-5.el7             perl-Pod-Escapes.noarch 1:1.04-292.el7       perl-Pod-Perldoc.noarch 0:3.20-4.el7              
  perl-Pod-Simple.noarch 1:3.28-4.el7            perl-Pod-Usage.noarch 0:1.63-3.el7           perl-Scalar-List-Utils.x86_64 0:1.27-248.el7      
  perl-Socket.x86_64 0:2.010-4.el7               perl-Storable.x86_64 0:2.45-3.el7            perl-TermReadKey.x86_64 0:2.30-20.el7             
  perl-Text-ParseWords.noarch 0:3.29-4.el7       perl-Time-HiRes.x86_64 4:1.9725-3.el7        perl-Time-Local.noarch 0:1.2300-2.el7             
  perl-constant.noarch 0:1.27-2.el7              perl-libs.x86_64 4:5.16.3-292.el7            perl-macros.x86_64 4:5.16.3-292.el7               
  perl-parent.noarch 1:0.225-244.el7             perl-podlators.noarch 0:2.5.1-3.el7          perl-threads.x86_64 0:1.87-4.el7                  
  perl-threads-shared.x86_64 0:1.43-6.el7       

Complete!

8 rbenvインストール

8-1. rbenvリポジトリをclone

terminal
$ git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
実行結果
Cloning into '/home/vagrant/.rbenv'...
remote: Counting objects: 2706, done.
remote: Total 2706 (delta 0), reused 0 (delta 0), pack-reused 2706
Receiving objects: 100% (2706/2706), 503.75 KiB | 39.00 KiB/s, done.
Resolving deltas: 100% (1695/1695), done.

8-2.PATHを設定

terminal
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile

8-3. ターミナル(bash)実行時に rbenv init を実行するように設定

terminal
$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile

8-4. .bash_profile のリロード

terminal
$ source ~/.bash_profile

8-5. 確認

terminal
$ rbenv --version
実行結果
rbenv 1.1.1-28-gb943955

9 ruby-buildをインストール

terminal
$ git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
実行結果
Cloning into '/home/vagrant/.rbenv/plugins/ruby-build'...
remote: Counting objects: 8588, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 8588 (delta 0), reused 0 (delta 0), pack-reused 8586
Receiving objects: 100% (8588/8588), 1.79 MiB | 177.00 KiB/s, done.
Resolving deltas: 100% (5485/5485), done.

10 Rubyのインストール

10-1. 各種必要なパッケージをインストール

terminal
$ sudo yum -y install openssl-devel gcc readline-devel libffi-devel mysql-devel gcc-c++
実行結果
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror.qoxy.com
 * extras: download.nus.edu.sg
 * updates: download.nus.edu.sg
Resolving Dependencies

*************省略*************

Installed:
  gcc.x86_64 0:4.8.5-16.el7_4.1                 gcc-c++.x86_64 0:4.8.5-16.el7_4.1             libffi-devel.x86_64 0:3.0.13-18.el7          
  mariadb-devel.x86_64 1:5.5.56-2.el7           openssl-devel.x86_64 1:1.0.2k-8.el7           readline-devel.x86_64 0:6.2-10.el7           

Dependency Installed:
  cpp.x86_64 0:4.8.5-16.el7_4.1                  glibc-devel.x86_64 0:2.17-196.el7_4.2       glibc-headers.x86_64 0:2.17-196.el7_4.2        
  kernel-headers.x86_64 0:3.10.0-693.17.1.el7    keyutils-libs-devel.x86_64 0:1.5.8-3.el7    krb5-devel.x86_64 0:1.15.1-8.el7               
  libcom_err-devel.x86_64 0:1.42.9-10.el7        libkadm5.x86_64 0:1.15.1-8.el7              libmpc.x86_64 0:1.0.1-3.el7                    
  libselinux-devel.x86_64 0:2.5-11.el7           libsepol-devel.x86_64 0:2.5-6.el7           libstdc++-devel.x86_64 0:4.8.5-16.el7_4.1      
  libverto-devel.x86_64 0:0.2.5-4.el7            mpfr.x86_64 0:3.1.1-4.el7                   ncurses-devel.x86_64 0:5.9-14.20130511.el7_4   
  pcre-devel.x86_64 0:8.32-17.el7                zlib-devel.x86_64 0:1.2.7-17.el7           

Complete!

10-2. Rubyの最新バージョンを確認

terminal
$ rbenv install -l
実行結果
Available versions:
  1.8.5-p52
  1.8.5-p113

*************省略*************

  2.4.0
  2.4.1
  2.4.2
  2.4.3
  2.5.0-dev
  2.5.0-preview1
  2.5.0-rc1
  2.5.0
  2.6.0-dev
  jruby-1.5.6

*************省略*************

10-3. rubyのインストール

terminal
$ rbenv install 2.5.0
実行結果
Downloading ruby-2.5.0.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.0.tar.bz2
Installing ruby-2.5.0...
Installed ruby-2.5.0 to /home/vagrant/.rbenv/versions/2.5.0

10-4. デフォルトで使用するRubyのバージョンを設定

terminal
$ rbenv global 2.5.0

10-5. 確認

terminal
$ ruby -v
実行結果
ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-linux]

11 Railsのインストール

11-1. multi_jsonのインストール

terminal
$ gem install multi_json -v '1.10.1'
実行結果
Fetching: multi_json-1.10.1.gem (100%)
Successfully installed multi_json-1.10.1
Parsing documentation for multi_json-1.10.1
Installing ri documentation for multi_json-1.10.1
Done installing documentation for multi_json after 0 seconds
1 gem installed

11-2. Bundlerのインストール

terminal
$ rbenv exec gem install bundler
実行結果
Successfully installed bundler-1.16.1
Parsing documentation for bundler-1.16.1
Installing ri documentation for bundler-1.16.1
Done installing documentation for bundler after 3 seconds
1 gem installed

11-3. rbenv rehash

terminal
$ rbenv rehash

11-4. Railsのインストール

terminal
$ gem install rails

$ source ~/.bash_profile

11-5. アプリケーションの作成

terminal
$ rails new sample -d mysql
実行結果
      create  
      create  README.md
      create  Rakefile
      create  config.ru
      create  .gitignore
      create  Gemfile
         run  git init from "."
Initialized empty Git repository in /home/vagrant/sample/.git/
      create  app
      create  app/assets/config/manifest.js

*************省略*************

Installing web-console 3.5.1
Bundle complete! 16 Gemfile dependencies, 70 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
         run  bundle exec spring binstub --all
* bin/rake: spring inserted
* bin/rails: spring inserted

11-6. Gemfileの編集

$ cd sample

$ vi Gemfile

gem 'therubyracer', platforms: :rubyのコメントアウトを外す

Gemfile
source 'https://rubygems.org'

git_source(:github) do |repo_name|
  repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
  "https://github.com/#{repo_name}.git"
end


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.1.4'
# Use mysql as the database for Active Record
gem 'mysql2', '>= 0.3.18', '< 0.5'
# Use Puma as the app server
gem 'puma', '~> 3.7'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
gem 'therubyracer', platforms: :ruby

# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '~> 2.13'
  gem 'selenium-webdriver'
end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '>= 3.0.5', '< 3.2'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

Gemfileを編集したのでbundleinstall

terminal
$ bundle install

12 サーバー起動

$ rails s

http://192.168.33.10:3000 にアクセスして以下の画面になればOK!

image.png

Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account log in.