[Mac] rails-dev-boxで環境構築


[rails/rails-dev-box · GitHub](https://github.com/rails/rails-dev-box)

## [rails-dev-box](https://github.com/rails/rails-dev-box)とは

Ruby on Rails の開発環境を自動で構築するプロジェクトのこと。vagrantのBoxとプロビジョニングがセットになっていて手早く開発環境が構築できる。

## [Boxに入っているもの](https://github.com/rails/rails-dev-box#whats-in-the-box) (2015/02/13時点)

- Development tools
- Git
- Ruby 2.2
- Bundler
- SQLite3, MySQL, and Postgres
- Databases and users needed to run the Active Record test suite
- System dependencies for nokogiri, sqlite3, mysql, mysql2, and pg
- Memcached
- Redis
- RabbitMQ
- An ExecJS runtime


## 手順

1.rails-dev-box を取得

```bash
$ git clone https://github.com/rails/rails-dev-box.git
```

```bash:実行結果
Cloning into 'rails-dev-box'...
remote: Counting objects: 1447, done.
remote: Total 1447 (delta 0), reused 1 (delta 0)
Receiving objects: 100% (1447/1447), 801.74 KiB | 71.00 KiB/s, done.
Resolving deltas: 100% (331/331), done.
Checking connectivity... done.
```


2.仮想マシンの起動

プロビジョニングで自動で環境構築が終わるまでだいたい10分。

```bash
$ cd rails-dev-box
$ vagrant up
```

```bash:実行結果
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'ubuntu/trusty32'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'ubuntu/trusty32' is up to date...
==> default: Setting the name of the VM: rails-dev-box_default_1423833481874_1181
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 3000 => 3000 (adapter 1)
    default: 22 => 2222 (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: Warning: Connection timeout. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Setting hostname...
==> default: Mounting shared folders...
    default: /vagrant => /Users/yamagh/GoogleDrive/dev/lang/ruby/rails-dev-box/rails-dev-box
==> default: Running provisioner: shell...
    default: Running: /var/folders/9z/99ll8khn25zgjjr39m7c2h_00000gn/T/vagrant-shell20150213-1116-1dnjyaf.sh
==> default: stdin: is not a tty
==> default: updating package information
==> default: installing development tools
==> default: installing Ruby
==> default: installing Bundler
==> default: installing Git
==> default: installing SQLite
==> default: installing memcached
==> default: installing Redis
==> default: installing RabbitMQ
==> default: installing PostgreSQL
==> default: installing MySQL
==> default: installing Nokogiri dependencies
==> default: installing ExecJS runtime
==> default: all set, rock on!
```

3.仮想マシンにログイン

```bash
$ vagrant ssh
```

```bash:実行結果
Welcome to Ubuntu 14.04.1 LTS (GNU/Linux 3.13.0-44-generic i686)

 * Documentation:  https://help.ubuntu.com/

  System information as of Fri Feb 13 13:18:27 UTC 2015

  System load:  0.79              Processes:           85
  Usage of /:   2.7% of 39.34GB   Users logged in:     0
  Memory usage: 13%               IP address for eth0: 10.0.2.15
  Swap usage:   0%

  Graph this data and manage this system at:
    https://landscape.canonical.com/

  Get cloud support with Ubuntu Advantage Cloud Guest:
    http://www.ubuntu.com/business/services/cloud

0 packages can be updated.
0 updates are security updates.
```

4.[Railsインストール](http://qiita.com/yamagh/items/61e540765fa4d2152dc4#5-railsのインストール)

```bash
$ sudo gem install rails
```

```bash:実行結果
Fetching: thread_safe-0.3.4.gem (100%)
Successfully installed thread_safe-0.3.4
Fetching: minitest-5.5.1.gem (100%)
.
.
.
```

5.サンプルアプリ作って動作確認

```bash
$ rails new foo
$ cd foo
$ vi Gemfile  # therubyracer をコメント解除
$ bundle
$ rails s -b 0.0.0.0
```

ホストOSのブラウザで http://localhost:3000 を開いて確認。