SSブログ

MacにRails3.2.13を入れてみた。 [rails]

Mac開発環境作りの続き。
Macで開発環境構築のための事前準備
Macにrbenvでruby1.9.3を入れてみた
homebrewでMySQL5.1を入れてみた

今回はRuby on Rails(3.2.13)をインストールします。

まずはBundlerのインストール。
rbenvで管理されているrubyと紐付けたいので、"rbenv exec"を利用してインストールする。
% rbenv exec gem install bundler
Fetching: bundler-1.3.4.gem (100%)
Successfully installed bundler-1.3.4
1 gem installed
Installing ri documentation for bundler-1.3.4...
Installing RDoc documentation for bundler-1.3.4...

% rbenv rehash
% rbenv exec bundle -v
Bundler version 1.3.4


次にRailsのインストール。
Railsはアプリケーションごとにbundleを利用してインストールする。

そこでプロジェクトフォルダを作成し、そこにインストールに必要なGemfileを作成する。
なお、2013/3/29時点でRails3.2系の最新は3.2.13なのでそれを入れる。
% mkdir my_app
% vim my_app/Gemfile
====================================================
source 'http://rubygems.org'
gem 'rails', '3.2.13'
====================================================


必要なGemはもっとあるけど、Gemfileはrailsインストール用に割りきって作成。

次にbundleを利用してrailsをインストールする。
インストールする場所は、Rails標準らしい"vendor/bundle"を指定。
% cd my_app
% bundle install --path vendor/bundle

Fetching gem metadata from http://rubygems.org/...........
Fetching gem metadata from http://rubygems.org/..
Resolving dependencies...
Installing rake (10.0.4)
Installing i18n (0.6.1)
Installing multi_json (1.7.2)
Installing activesupport (3.2.13)
Installing builder (3.0.4)
Installing activemodel (3.2.13)
Installing erubis (2.7.0)
Installing journey (1.0.4)
Installing rack (1.4.5)
Installing rack-cache (1.2)
Installing rack-test (0.6.2)
Installing hike (1.2.1)
Installing tilt (1.3.6)
Installing sprockets (2.2.2)
Installing actionpack (3.2.13)
Installing mime-types (1.22)
Installing polyglot (0.3.3)
Installing treetop (1.4.12)
Installing mail (2.5.3)
Installing actionmailer (3.2.13)
Installing arel (3.0.2)
Installing tzinfo (0.3.37)
Installing activerecord (3.2.13)
Installing activeresource (3.2.13)
Using bundler (1.3.4)
Installing json (1.7.7)
Installing rack-ssl (1.3.3)
Installing rdoc (3.12.2)
Installing thor (0.18.1)
Installing railties (3.2.13)
Installing rails (3.2.13)
Your bundle is complete!
It was installed into ./vendor/bundle
Post-install message from rdoc:
Depending on your version of ruby, you may need to install ruby rdoc/ri data:

<= 1.8.6 : unsupported
 = 1.8.7 : gem install rdoc-data; rdoc-data --install
 = 1.9.1 : gem install rdoc-data; rdoc-data --install
>= 1.9.2 : nothing to do! Yay!

% bundle exec rails -v
Rails 3.2.13 


無事にインストール出来た!
続いてRailsアプリケーションを作成する。

なお、作成時にオプションを付けてDBはMySQLを利用する。
また、普通に作成するとオプションなしの"bundle install"自動的に実行されてしまうので、skipするオプションも追加しておく。
% bundle exec rails new . -d mysql --skip-bundle
       exist 
      create  README.rdoc
      create  Rakefile
      create  config.ru
      create  .gitignore
    conflict  Gemfile

# 途中で聞かれるGemfileの上書きはY(yes)を選択する
Overwrite /Users/**********/Development/my_app/Gemfile? (enter "h" for help) [Ynaqdh] Y
       force  Gemfile
      create  app
      create  app/assets/images/rails.png
      create  app/assets/javascripts/application.js
      create  app/assets/stylesheets/application.css
      create  app/controllers/application_controller.rb
      create  app/helpers/application_helper.rb
      create  app/views/layouts/application.html.erb
      create  app/mailers/.gitkeep
      create  app/models/.gitkeep
      create  config
      create  config/routes.rb
      create  config/application.rb
      create  config/environment.rb
      create  config/environments
      create  config/environments/development.rb
      create  config/environments/production.rb
      create  config/environments/test.rb
      create  config/initializers
      create  config/initializers/backtrace_silencers.rb
      create  config/initializers/inflections.rb
      create  config/initializers/mime_types.rb
      create  config/initializers/secret_token.rb
      create  config/initializers/session_store.rb
      create  config/initializers/wrap_parameters.rb
      create  config/locales
      create  config/locales/en.yml
      create  config/boot.rb
      create  config/database.yml
      create  db
      create  db/seeds.rb
      create  doc
      create  doc/README_FOR_APP
      create  lib
      create  lib/tasks
      create  lib/tasks/.gitkeep
      create  lib/assets
      create  lib/assets/.gitkeep
      create  log
      create  log/.gitkeep
      create  public
      create  public/404.html
      create  public/422.html
      create  public/500.html
      create  public/favicon.ico
      create  public/index.html
      create  public/robots.txt
      create  script
      create  script/rails
      create  test/fixtures
      create  test/fixtures/.gitkeep
      create  test/functional
      create  test/functional/.gitkeep
      create  test/integration
      create  test/integration/.gitkeep
      create  test/unit
      create  test/unit/.gitkeep
      create  test/performance/browsing_test.rb
      create  test/test_helper.rb
      create  tmp/cache
      create  tmp/cache/assets
      create  vendor/assets/javascripts
      create  vendor/assets/javascripts/.gitkeep
      create  vendor/assets/stylesheets
      create  vendor/assets/stylesheets/.gitkeep
      create  vendor/plugins
      create  vendor/plugins/.gitkeep


次に上書きされたGemfileのGemをインストールする。
% bundle install --path vendor/bundle
Fetching gem metadata from https://rubygems.org/...........
Fetching gem metadata from https://rubygems.org/..
Resolving dependencies...
Using rake (10.0.4)
Using i18n (0.6.1)
Using multi_json (1.7.2)
Using activesupport (3.2.13)
Using builder (3.0.4)
Using activemodel (3.2.13)
Using erubis (2.7.0)
Using journey (1.0.4)
Using rack (1.4.5)
Using rack-cache (1.2)
Using rack-test (0.6.2)
Using hike (1.2.1)
Using tilt (1.3.6)
Using sprockets (2.2.2)
Using actionpack (3.2.13)
Using mime-types (1.22)
Using polyglot (0.3.3)
Using treetop (1.4.12)
Using mail (2.5.3)
Using actionmailer (3.2.13)
Using arel (3.0.2)
Using tzinfo (0.3.37)
Using activerecord (3.2.13)
Using activeresource (3.2.13)
Using bundler (1.3.4)
Installing coffee-script-source (1.6.2)
Installing execjs (1.4.0)
Installing coffee-script (2.2.0)
Using rack-ssl (1.3.3)
Using json (1.7.7)
Using rdoc (3.12.2)
Using thor (0.18.1)
Using railties (3.2.13)
Installing coffee-rails (3.2.2)
Installing jquery-rails (2.2.1)
Installing mysql2 (0.3.11)
Using rails (3.2.13)
Installing sass (3.2.7)
Installing sass-rails (3.2.6)
Installing uglifier (1.3.0)
Your bundle is complete!
It was installed into ./vendor/bundle


これでアプリケーション(の土台)の作成が完了。
WEBrickを起動して動作を確認する。
% bundle exec rails server
=> Booting WEBrick
=> Rails 3.2.13 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Exiting
/Users/************/Development/my_app/vendor/bundle/ruby/1.9.1/gems/mysql2-0.3.11/lib/mysql2/client.rb:44:in `connect': Access denied for user 'root'@'localhost' (using password: NO) (Mysql2::Error)
     from /Users/*********/Development/my_app/vendor/bundle/ruby/1.9.1/gems/mysql2-0.3.11/lib/mysql2/client.rb:44:in `initialize'
     from /Users/*********/Development/my_app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.13/lib/active_record/connection_adapters/mysql2_adapter.rb:16:in `new'
(以下略)


エラる。MySQLにパスワードなしのrootで繋ぎにいってるから当然といえば当然か。
先にMySQLにテスト用のデータベースとアクセス用のユーザを作成する。
% mysql -u root -p

mysql> create database my_app_dev;
Query OK, 1 row affected (0.00 sec)

mysql> GRANT ALL PRIVILEGES ON my_app_dev.* TO testuser@localhost IDENTIFIED BY 'hogehoge' WITH GRANT OPTION;


config/database.ymlにMySQLの設定を追加する。
% vim config/database.yml
-----------------------------------------------------------
development:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: my_app_dev
  pool: 5
  username: testuser
  password: hogehoge
  socket: /tmp/mysql.sock
-----------------------------------------------------------


設定が終わったらもう一度WEBrickを起動する
% bundle exec rails server
=> Booting WEBrick
=> Rails 3.2.13 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2013-03-31 20:11:34] INFO  WEBrick 1.3.1
[2013-03-31 20:11:34] INFO  ruby 1.9.3 (2013-01-15) [x86_64-darwin12.2.1]
[2013-03-31 20:11:34] INFO  WEBrick::HTTPServer#start: pid=42119 port=3000


"http://0.0.0.0:3000"にアクセスし、無事に表示される事を確認。
また、"About your application's environment"テキストリンクもクリックし問題なし。

そんな訳でrailsが動くようにはなりました。
タグ:Ruby rails3.2 MAC
nice!(2)  コメント(0) 
共通テーマ:パソコン・インターネット

nice! 2

コメント 0

この広告は前回の更新から一定期間経過したブログに表示されています。更新すると自動で解除されます。