LoginSignup
11
13

More than 5 years have passed since last update.

VagrantでRuby on Rails環境構築

Last updated at Posted at 2018-04-03

Railsを触り始めたので備忘がてらに環境構築手順を記載します。

この記事のゴール

  • Ruby 2.5.0 インストール
  • Rails 5.1.5 インストール、画面を確認

※Ruby, Railsは環境構築時(2018/3時点)の最新安定バージョンです
※実行環境: OS X El Capitan 10.11.6

仮想マシン作成

  • Vagrant, Virtual Box をインストールしておく
    構築時のバージョンはVagrantは1.8.1, Virtual Boxは4.3.26で行いました。
  • CentOS7のboxを取得
    
    $ vagrant box add {作成するbox名} http://developer.nrel.gov/downloads/vagrant-boxes/CentOS-6.4-x86_64-v20130427.box
    
  • 仮想マシン作成
    
    $ vagrant init {box名}
    
  • Vagrantfileを修正し、private_networkのコメントアウトを外す
    
    $ vi Vagrantfile 
    

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

コメントアウトを外したら、再起動を行う


$ vagrant reload

再起動後、以下の作業はvagrant sshでSSH接続をして行います。

rbenvのインストール

  • rbenvのリポジトリをクローン

    
    $ git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
    Cloning into '/home/vagrant/.rbenv'...
    fatal: unable to access 'https://github.com/sstephenson/rbenv.git/': Peer reports incompatible or unsupported protocol version.
    

    ・・・いきなり失敗。
    yum updateでcurlのバージョンを最新版にしてみれば良いのではということで試してみましたがこちらでも失敗。  
    NSS(Network Security Service)のエラーが出ていたため以下を試したところgit cloneが通りました
    
    $ yum update -y nss curl libcurl
    
  • PATHを通す

    
    $ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
    $ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
    $ cat ~/.bash_profile
    // 反映
    $ source ~/.bash_profile
    
  • バージョン確認

    
    $ rbenv --version
    rbenv 1.1.1-30-gc8ba27f
    
  • ruby-buildをインストール

    
    $ git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
    Cloning into '/home/vagrant/.rbenv/plugins/ruby-build'...
    remote: Counting objects: 8557, done.
    remote: Compressing objects: 100% (4/4), done.
    remote: Total 8557 (delta 2), reused 0 (delta 0), pack-reused 8553
    Receiving objects: 100% (8557/8557), 1.79 MiB | 1.11 MiB/s, done.
    Resolving deltas: 100% (5452/5452), done.
    

Rubyのインストール

  • まずインストール可能なバージョンを確認
    
    $ rbenv install -l
    

ここでは2.5.0が最新安定バージョンのため2.5.0をインストール。  
さらにglobalオプションを指定してデフォルトで利用するバージョンを指定しておきます


$ rbenv install 2.5.0
$ rbenv global 2.5.0

$ ruby -v
ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-linux]

Railsのインストール

  • multi_jsonのインストール
    
    $ 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
    
  • bundlerのインストール
    
    $ rbenv exec gem install bundler
    
    また利用中のrubyに対応する、gem内のコマンドラインツールを利用可能にしておきます
    
    $ rbenv rehash
    
  • インストール可能なバージョンを確認後、インストール実行

$ gem search -r rails | grep "^rails ("
rails (5.1.5)

$ gem install rails

アプリケーションの新規作成と起動

Railsのインストールが完了したので新規アプリケーションを作成してみます。


$ source ~/.bash_profile

$ rails new {アプリ名}
      create
      create  README.md
      create  Rakefile
      create  config.ru
      create  .gitignore
      create  Gemfile
         run  git init from "."
Initialized empty Git repository in /home/vagrant/project/.git/
      create  app
      create  app/assets/config/manifest.js
      create  app/assets/javascripts/application.js
      create  app/assets/javascripts/cable.js
      create  app/assets/stylesheets/application.css
      create  app/channels/application_cable/channel.rb
      create  app/channels/application_cable/connection.rb
      create  app/controllers/application_controller.rb
      create  app/helpers/application_helper.rb
      create  app/jobs/application_job.rb
...
新規作成中
...
Installing spring-watcher-listen 2.0.1
Fetching sqlite3 1.3.13
Installing sqlite3 1.3.13 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

current directory:
/home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/sqlite3-1.3.13/ext/sqlite3
/home/vagrant/.rbenv/versions/2.5.0/bin/ruby -r ./siteconf20180311-25598-46lz1i.rb extconf.rb
checking for sqlite3.h... no
sqlite3.h is missing. Try 'brew install sqlite3',
'yum install sqlite-devel' or 'apt-get install libsqlite3-dev'
and check your shared library search path (the
location where your sqlite3 shared library is located).
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

...
...

An error occurred while installing sqlite3 (1.3.13), and Bundler cannot continue.
Make sure that `gem install sqlite3 -v '1.3.13'` succeeds before bundling.

In Gemfile:
  sqlite3
         run  bundle exec spring binstub --all
Could not find gem 'sqlite3' in any of the gem sources listed in your Gemfile.
Run `bundle install` to install missing gems.

sqliteをインストールしろと怒られたため以下を実行します


$ sudo yum -y install sqlite-devel
  • Gemfileを修正
    
    $ vi Gemfile
    
    Gemfile中の以下のコメントアウトを外します

# gem 'therubyracer', platforms: :ruby

修正後は再度bundle installを行います


$ bundle install
...
Fetching web-console 3.5.1
Installing web-console 3.5.1
Bundle complete! 17 Gemfile dependencies, 73 gems now installed.
Use bundle info [gemname] to see where a bundled gem is installed.
  • アプリケーション起動
    rails srails serverが省略された形を指します
    
    $ rails s
    => Booting Puma
    => Rails 5.1.5 application starting in development
    => Run rails server -h for more startup options
    Puma starting in single mode...
    * Version 3.11.3 (ruby 2.5.0-p0), codename: Love Song
    * Min threads: 5, max threads: 5
    * Environment: development
    * Listening on tcp://0.0.0.0:3000
    Use Ctrl-C to stop
    Started GET "/" for 192.168.33.1 at 2018-04-03 15:15:07 +0100
    ...
    
  • http://192.168.33.10:3000/ にアクセスしてみる Ruby on Rails.png

(上記はキャプチャ)
Hello World的な例の画面が表示されました。

11
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
13