Ruby on RailsをUbuntuに導入する

Ubuntu上でRuby on Railsを参考にRuby on Railsを導入する。

前提として、UbuntuにRubyを導入する - みさとのPCめもの続き。

1. gemを使えるようにする。

% sudo apt-get install Rubygems

これでgemコマンドを使えるようにする。

2. gemsetを作成する。

% rvm gemset list
% rvm gemset create rails3
% rvm gemset use rails3

3. Railsをインストールする。

% rvmsudo gem install rails

4. Ubuntu上でRuby on Railsからの引用

Railsで入れるべきgemライブラリーをいちいちインストールするのは面倒なので、bundleを利用してまとめてインストールする。まずは新しいプロジェクトを作成する

% rails new demo
% cd demo
% vi Gemfile

Gemfileに以下を付け加える。

gem 'execjs'
gem 'therubyracer'

必要なgemライブラリーを追加する。

% bundle install

―――― 以上引用 ――――

と、ここでエラー。

Installing therubyracer (0.10.1) with native extensions
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

/usr/bin/ruby1.8 extconf.rb
Invalid gemspec in [/var/lib/gems/1.8/specifications/mail-2.4.4.gemspec]: invalid date format in specification: "2012-03-14 00:00:00.000000000Z"
Invalid gemspec in [/var/lib/gems/1.8/specifications/mime-types-1.18.gemspec]: invalid date format in specification: "2012-03-21 00:00:00.000000000Z"
Invalid gemspec in [/var/lib/gems/1.8/specifications/libv8-3.3.10.4-x86-linux.gemspec]: invalid date format in specification: "2011-11-15 00:00:00.000000000Z"
Invalid gemspec in [/var/lib/gems/1.8/specifications/jquery-rails-2.0.2.gemspec]: invalid date format in specification: "2012-04-03 00:00:00.000000000Z"
Invalid gemspec in [/var/lib/gems/1.8/specifications/sass-rails-3.2.5.gemspec]: invalid date format in specification: "2012-03-19 00:00:00.000000000Z"
Invalid gemspec in [/var/lib/gems/1.8/specifications/tilt-1.3.3.gemspec]: invalid date format in specification: "2011-08-25 00:00:00.000000000Z"

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.

Provided configuration options:
--with-opt-dir
--without-opt-dir
--with-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--with-make-prog
--without-make-prog
--srcdir=.
--curdir
--ruby=/usr/bin/ruby1.8
/usr/lib/ruby/vendor_ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require': no such file to load -- libv8 (LoadError)
from /usr/lib/ruby/vendor_ruby/1.8/rubygems/custom_require.rb:36:in `require'
from extconf.rb:7


Gem files will remain installed in /home/handa/.bundler/tmp/20818/gems/therubyracer-0.10.1 for inspection.
Results logged to /home/handa/.bundler/tmp/20818/gems/therubyracer-0.10.1/ext/v8/gem_make.out
An error occured while installing therubyracer (0.10.1), and Bundler cannot continue.
Make sure that `gem install therubyracer -v '0.10.1'` succeeds before bundling.

libv8がインストールされていないとか、"gem install therubyracer -v '0.10.1'"を試してみてと言われているが、今回のケースの場合日付の指定が有効でなかったことが問題だった。(libv8についてはちゃんとインストールされている。)


Ubuntu + Ruby環境をつくる | 崖の上のプログラマー日記
を参考に、以下のコマンドを実行。

% sudo find /var/lib/gems/1.8/specifications/ -name “*.gemspec” -exec sed -i ‘s/ 00:00:00.000000000Z//’ {} \;

日付の形式を直します。直っているか確認するために、以下のコマンド。

% grep "000000000Z" /var/lib/gems/1.8/specifications/*.gemspec

検出されなかったらOK。検出されたら、手動で削除。場合によってはスーパーユーザである必要性がある。

もう一回

% bundle install

うまくいった。



5. Ubuntu上でRuby on Railsからの引用

scaffoldしてrailsがちゃんと動くかを確認する。

% rails generate scaffold person name:string age:integer
% rake db:migrate
% rails server

http://localhost:3000/people で追加、削除、編集ができたら成功。





5/9追記

本手順における手順4において

% bundle install

を行った際に起こったエラーは、Ruby1.8.7で起こる問題だった。Ruby1.9.3ではエラーは起こらない。