LoginSignup
5
5

More than 5 years have passed since last update.

VagrantでRails環境を整備する

Last updated at Posted at 2014-12-25

お疲れ様です。ジャックです。
VagrantでRailsアプリケーションを動かす手順をメモしました。
ゆくゆくは「Rails + Unicron + Nginx」な環境を構築したいです。

Vagrantで仮想環境を準備する

Vagrantboxをダウンロードする

vagrant-ubuntu-14.04というディレクトリを用意し、そこで作業を行う。

$ mkdir vagrant-ubuntu-14.04
$ cd vagrant-ubuntu-14.04
$ vagrant box add ubuntu-14.04 https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box
$ vagrant box list

Vagrantの設定をする

はじめに、Vagrantを初期化しVagrantfileを生成する。

$ vagrant init ubuntu-14.04

Railsアプリケーションを作成した際に http://localhost:3000 でアクセスできるようにする為に、Vagrantfileを変更する。

# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
#config.vm.network :forwarded_port, guest: 80, host: 8080
config.vm.network :forwarded_port, guest: 3000, host: 3000

Vagrantを起動し、sshでログインする。

$ vagrant up
$ vagrant ssh

Ubuntuのアップデート作業

Vagrantで作成した仮想マシンにsshでログイン後、下記コマンドをインストールする。

$ sudo apt-get updat
$ sudo apt-get dist-upgrade

RubyとRails環境を構築する

rbenvとRubyをインストールする

$ sudo apt-get install build-essential
$ sudo apt-get install git
$ sudo apt-get install libssl-dev
$ sudo apt-get install libsqlite3-dev
$ git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
$ echo 'eval "$(rbenv init -)"' >> ~/.bashrc
$ git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
$ source .bashrc
$ rbenv install 2.0.0-p353
$ rbenv global 2.0.0-p353
$ rbenv rehash

Railsをインストールする

$ gem install --no-ri --no-rdoc rails
$ rails -v # Railsのバージョンを確認
Rails 4.2.0

bundleコマンドをインストールする

$ gem install bundler

Railsのサンプルアプリを作成する

サンプルアプリを作成

$ rails new sample
$ cd sample

Gemfileを修正

vim等でファイルを開き「gem 'therubyracer', platforms: :ruby」のコメントアウトを外す。
また、サンプルアプリでは利用しない為「gem 'byebug'」と「gem 'spring'」をコメントアウトする。

source 'https://rubygems.org'


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.0'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc

# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Unicorn as the app server
# gem 'unicorn'

# 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'

  # Access an IRB console on exception pages or by using <%= console %> in views
  gem 'web-console', '~> 2.0'

  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  # gem 'spring'
end

RubyGemsのアップデートを行う

$ gem install rubygems-update
$ update_rubygems
$ gem -v # RubyGemsのバージョンを確認
2.4.5

bundle installを実行

$ bundle install --path=~/sample/vendor/bundle

Railsを起動

$ rails s
=> Booting WEBrick
=> Rails 4.2.0 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2014-12-25 09:56:21] INFO  WEBrick 1.3.1
[2014-12-25 09:56:21] INFO  ruby 2.0.0 (2013-11-22) [x86_64-linux]
[2014-12-25 09:56:21] INFO  WEBrick::HTTPServer#start: pid=8387 port=3000

次回は、Railsアプリケーションを「Unicorn + Nginx」で動かす手順をまとめる予定です。

5
5
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
5
5