20分くらいでできるVagrantによるRails開発環境の構築
はじめに
Railsの環境をパパっと作りたい。でもVagrantもChefもよくわからない。そんな私でもすぐできました。
こちらが参考になりました。
以下内容をまとめてみました。
Using Vagrant for Rails Development
Overview
読み終えるまでにかかる時間は20分くらい
Vagrantは開発環境のセットアップを自動化するツールです。あなたのコンピュータ上に仮想環境を構築します。Vagrantを使えば開発環境とプロダクションサーバの整合性を保ったり、同僚と同じ環境で開発したりできるようになります。
仮想環境上に構築したRails開発環境は、ホストコンピュータの設定がいかなる状態になろうとも、同じ状態を維持できます。もし、1年後に開発プロジェクトを再度稼働する必要が生じた場合にも、数分でプロジェクトを起動できます。これは大変便利なことです。
ここでは仮想環境上の開発環境の自動構築にChefを使用します。ホストコンピュータ上にRubyと依存するパッケージがセットアップされているか注意してください。
セールストークは十分でしょう。それでは始めましょうか。
Setting Up Vagrant
Vagrantで仮想環境上でRailsアプリケーションを起動するためにはゲストOSを起動しなければなりません。そのため、ホストコンピュータ上で1GB、2GB程度のRAMが利用できることを確認しておいてください。
最初のStepはコンピュータ上にVagrantとVirtualBoxをインストールしてください。
VirtualBoxは仮想マシンを実行するソフトウェアです。VirtualBoxはバックグラウンドで動作するヘッドレスなソフトウェアとして起動できるので、ゲストOSとSSHを使ってインタラクティブにやりとりすることができます。
- vagrant-vbguest VirtualBox Guest Additionsを自動的にインストールします
- vagrant-librarian-chef マシンの起動時にchefを自動的に実行します
vagrant plugin install vagrant-vbguest vagrant plugin install vagrant-librarian-chef
インストールには少し時間がかかるでしょう。
Create the Vagrant config
ChefをセットアップするためにRailsプロジェクトのトップに移動し、次のコマンドを実行します。
cd MY_RAILS_PROJECT # Change this to your Rails project directory vagrant init touch Cheffile
ここで作成されたVagrantfileとCheffileをカスタマイズすることになります。
Your Cheffile
ここではCheffileを定義します。CheffileはRailsのGemfileとよく似ています。このファイルにはプロジェクトに必要なChefのクックブックを定義します。後ほど作成するVagrantfileによって、Vagrantはこれらのクックブックを参照することで環境を構築するようになります。
Cheffileに次のコードをペーストすると良いでしょう。
site "http://community.opscode.com/api/v1" cookbook 'apt' cookbook 'build-essential' cookbook 'mysql' cookbook 'ruby_build' cookbook 'nodejs', git: 'https://github.com/mdxp/nodejs-cookbook' cookbook 'rbenv', git: 'https://github.com/fnichol/chef-rbenv' cookbook 'vim'
Your Vagrantfile
Vagrantfileには仮想マシン上のOSとChefの設定を定義します。
ゲストOSにはUbuntu 14.04 trusty 64-bitを使用します(32-bitがよければtrusty32でもOKです)。ここでは2GBのメモリを定義しています。続いてrails serverを実行した際、ブラウザでアクセスしやすくするため、ホストOS上の3000番ポートを仮想マシンの3000番ポートにフォワードするように設定しています。最後に仮想マシン内にChef、Ruby2.1.2、MySQLをセットアップしています。
Vagrantfileは次のように変更してください。
# -*- mode: ruby -*- # vi: set ft=ruby : VAGRANTFILE_API_VERSION = "2" Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| # Use Ubuntu 14.04 Trusty Tahr 64-bit as our operating system config.vm.box = "ubuntu/trusty64" # Configurate the virtual machine to use 2GB of RAM config.vm.provider :virtualbox do |vb| vb.customize ["modifyvm", :id, "--memory", "2048"] end # Forward the Rails server default port to the host config.vm.network :forwarded_port, guest: 3000, host: 3000 # Use Chef Solo to provision our virtual machine config.vm.provision :chef_solo do |chef| chef.cookbooks_path = ["cookbooks", "site-cookbooks"] chef.add_recipe "apt" chef.add_recipe "nodejs" chef.add_recipe "ruby_build" chef.add_recipe "rbenv::user" chef.add_recipe "rbenv::vagrant" chef.add_recipe "vim" chef.add_recipe "mysql::server" chef.add_recipe "mysql::client" # Install Ruby 2.1.2 and Bundler # Set an empty root password for MySQL to make things simple chef.json = { rbenv: { user_installs: [{ user: 'vagrant', rubies: ["2.1.2"], global: "2.1.2", gems: { "2.1.2" => [ { name: "bundler" } ] } }] }, mysql: { server_root_password: '' } } end end
Running Vagrant
以上でVagrantとChefの設定は完了です。Vagrantで仮想マシンを起動してsshで接続してみましょう。
# The commented lines are the output you should see when you run these commands vagrant up #==> default: Checking if box 'ubuntu/trusty64' is up to date... #==> default: Clearing any previously set forwarded ports... #==> default: Installing Chef cookbooks with Librarian-Chef... #==> default: The cookbook path '/Users/chris/code/test_app/site-cookbooks' doesn't exist. Ignoring... #==> 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: Running 'pre-boot' VM customizations... #==> 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: Machine booted and ready! #==> default: Checking for guest additions in VM... #==> default: Mounting shared folders... # default: /vagrant => /Users/chris/code/test_app # default: /tmp/vagrant-chef-1/chef-solo-1/cookbooks => /Users/chris/code/test_app/cookbooks #==> default: VM already provisioned. Run `vagrant provision` or use `--provision` to force it
vagrant ssh #Welcome to Ubuntu 14.04 LTS (GNU/Linux 3.13.0-24-generic x86_64) # # * Documentation: https://help.ubuntu.com/ # # System information disabled due to load higher than 1.0 # # Get cloud support with Ubuntu Advantage Cloud Guest: # http://www.ubuntu.com/business/services/cloud # # #vagrant@vagrant-ubuntu-trusty-64:~$
初めてvagrant upコマンドを実行するときは時間がかかります。これはChefの設定ファイルに従って仮想マシンをプロビジョニングするためです。次回以降のvagrant upコマンドはChefを実行する必要なくなるため、起動に時間はかからなくなるでしょう。
VagrantfileやCheffileを編集した場合は、次のコマンドを実行してマシンを再設定しなければなりません。
vagrant provision
Using Rails inside Vagrant
Vagrantは/vagrantフォルダを仮想マシンとホストOS間で共有します。cd /vagrantで移動してlsコマンドを叩けばRailsアプリケーションのファイルを確認できるでしょう。
このディレクトリでbundleコマンドを実行すればgemをインストールできますし、rake db:create && rake db:migrateを実行すればデータベースを構築しマイグレーションも行えます。
このディレクトリでrails serverコマンドを実行すればRailsアプリケーションが3000番ポートで起動します。いつもどおりlocalhost:3000にアクセスすればRailsアプリケーションを操作できます。
Conclusion
Vagrantはポータブルな開発環境を作成するソフトウェアツールです。そこにはあなたのコードを含むこともできます。Chefを使ってシンプルな設定を施すだけで、ほとんど時間をかけずに仮想マシンを起動することができます。
もし、あなたが再びVagrantマシンをセットアップする必要がでてきたり、同僚がセットアップを必要とする場合は、vagrant upコマンドを実行するだけで仮想マシンを手に入れることができます。