Ruby on RailsのセットアップをCygwinで頑張ってみた話。



Windows7 64bit上に、Ruby2.0、Rails の開発環境を構築してみます。

目的はRuby on Railsをいじって雰囲気を味わうため。

ちなみにRubyは触ったことないです。

でも、Python経験あるのでノリでいきなりRails触れると踏んで、いきなりRailsから始めます。

ぶっちゃけWindowsでのセットアップって他のLinux系OSのセットアップに比べると、

クソ面倒なのでやりたくないんですが。。。。

2014/08/13 時点で最新っぽいインストール手順は以下のとおり



1. Rubyのセットアップ


まずは下記リンクからRubyInstallerでRubyをインストールします。

Ruby2.0系が新しいようです。

OK 連打でサクっとインストールされます。

http://rubyinstaller.org/downloads/

2. Development Kitの解凍 & コピー

1で訪問したサイトにある Development Kitを落とします。

Rubyのversionごとに異なる様子なので、1でおとしたRubyに対応するものを取得するのに注意。


コマンドライン or cygwinで解凍されたディレクトリまで行って、以下のコマンド。



$ ruby dk.rb init 

  < config.xmlが作成されます >

$ ruby dk.rb install

  < Development Kit のインストール完了 >


3. Railsのインストール

普通にやってればうまくいくようですが、私はコマンドライン嫌だったんでcygwin上でやってました。

そしたらこんなエラーが。。


C:\>gem install rails
ERROR:  While executing gem ... (Errno::ENOENT)
No such file or directory - C:/Users/*****


誠に糞であります!


"gem"とかいうコマンドがうまく実行されないお。。




とりあえずコマンドのパス確認。

$ which gem
/cygdrive/c/Ruby200-x64/bin/gem


 =>  特に問題なさそう。


エクスプローラーで該当ディレクトリ開いてみる。


... "gem.bat"なる香ばしいファイルが!


おそらくgem.bat叩かなきゃいけないのに gem.exe  探してしまってるんちゃうかなぁと思い、


Google先生にヒアリング。



そして辿り着いた先。


まとめると、gemあかんからgem.batにエイリアスしてくださいよとのこと。


素直なので私は ~/.bash_profile にこれ貼りましたよ。

alias gem='gem.bat'
alias rake='rake.bat'
alias erb='erb.bat'
alias irb='irb.bat'
alias rdoc='rdoc.bat'
alias ri='ri.bat'
alias rspec='rspec.bat'
alias cucumber='cucumber.bat'
alias bundle='bundle.bat'

# まぁaliasしてがんばらんでも *.batって打てばPATHさえ通っていればどうにかなるんですが、
# 将来的にサードパーティ製スクリプトとか回すとなるとトラブル踏みがちなので
# 素直にaliasしたほうがよいでしょう。

ちなみにこういう感じでハードコードするのは非常に良くないです。

リンクにもあるようにスクリプト化して自動でbatファイルをエイリアスするようなコードを入れておくべきです。

さもなくば都度都度.bash_profileの変更をしなきゃいけなくてアホくさいですよ。
っていうのが以下のコメントにあります。


The trick is to alias all .bat files as Robert pointed out in his answer.
Adding a new alias to your .bashrc or .zshrc after every gem install ain't fun though ...
Therefore i create these aliases automatically by scanning Ruby's bindir:
if [[ -n "$(which ruby 2>/dev/null)" ]]; then
  RUBY_BIN=$(cygpath -u $(ruby -e 'puts RbConfig::CONFIG["bindir"]') | tr '\r' ' ')
  for f in $(find ${RUBY_BIN} -regex ".*bat$"| xargs -n1 basename); do
      alias ${f%.bat}=${f}
  done
fi
share|improve this answer

問題も解決されたので、やっとRails をインストール!

$ gem install rails
unable to convert "\xFF" from ASCII-8BIT to UTF-8 for guides/assets/images/akshaysurve.jpg, skipping
unable to convert "\x89" from ASCII-8BIT to UTF-8 for guides/assets/images/belongs_to.png, skipping
unable to convert "\xF4" from ASCII-8BIT to UTF-8 for guides/assets/images/book_icon.gif, skipping
unable to convert "\x91" from ASCII-8BIT to UTF-8 for guides/assets/images/bullet.gif, skipping
unable to convert "\xF5" from ASCII-8BIT to UTF-8 for guides/assets/images/chapters_icon.gif, skipping
unable to convert "\xF5" from ASCII-8BIT to UTF-8 for guides/assets/images/check_bullet.gif, skipping
unable to convert "\xF4" from ASCII-8BIT to UTF-8 for guides/assets/images/credits_pic_blank.gif, skipping
unable to convert "\x89" from ASCII-8BIT to UTF-8 for guides/assets/images/csrf.png, skipping
unable to convert "\x89" from ASCII-8BIT to UTF-8 for guides/assets/images/edge_badge.png, skipping
unable to convert "\x9E" from ASCII-8BIT to UTF-8 for guides/assets/images/favicon.ico, skipping
unable to convert "\xF0" from ASCII-8BIT to UTF-8 for guides/assets/images/feature_tile.gif, skipping
unable to convert "\xF0" from ASCII-8BIT to UTF-8 for guides/assets/images/footer_tile.gif, skipping
unable to convert "\x89" from ASCII-8BIT to UTF-8 for guides/assets/images/fxn.png, skipping
unable to convert "\x89" from ASCII-8BIT to UTF-8 for guides/assets/images/getting_started/article_with_comments.png, skipping
unable to convert "\x89" from ASCII-8BIT to UTF-8 for guides/assets/images/getting_started/challenge.png, skipping



また何かうるさいエラー発生。


なんだろうこれ、調査。。。


https://github.com/dvdxaker/Rails-Installer-for-ubuntu-13.04/issues/1


jeasoft commented on 4 Aug 2013
Hey guys. As I watched here, it's an issue with Rdoc and Ruby 2.0.0. I have the same inconvenient, but Rails works with this .

はい、既知の不具合なんですね。

将来的に誰かが直してくれるんですね。

無視して問題ないんですね。



ちょっと気になったのは、 gem install rails コマンド叩いてから数分反応ない時間があったこと。

何かの同期処理がてっぺんで実行されてるんだろうけど、

うんともすんともいわないのは良くないので修正してもらえるとありがたいですな。。

私みたいな短気な人間は、すぐにCtrl + C 押して原因調査のためググり始めてしまうので。。



4. Rails serverを動かす

Railsのインストール終わったので、男は黙って

$ rails new helloworld

はい、helloworld ディレクトリ内に最低限必要なファイルが自動生成された模様です。

次、サーバー動かす。

$ cd helloworld; rails server
C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/tzinfo-1.2.2/lib/tzinfo/data_source.rb:182:in `rescue in create_default_data_source': No source of timezone data could be found. (TZInfo::DataSourceNotFound)
Please refer to http://tzinfo.github.io/datasourcenotfound for help resolving this error.
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/tzinfo-1.2.2/lib/tzinfo/data_source.rb:179:in `create_default_data_source'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/tzinfo-1.2.2/lib/tzinfo/data_source.rb:40:in `block in get'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/tzinfo-1.2.2/lib/tzinfo/data_source.rb:39:in `synchronize'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/tzinfo-1.2.2/lib/tzinfo/data_source.rb:39:in `get'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/tzinfo-1.2.2/lib/tzinfo/timezone.rb:629:in `data_source'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/tzinfo-1.2.2/lib/tzinfo/timezone.rb:92:in `get'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/tzinfo-1.2.2/lib/tzinfo/timezone_proxy.rb:67:in `real_timezone'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/tzinfo-1.2.2/lib/tzinfo/timezone_proxy.rb:30:in `period_for_utc'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/tzinfo-1.2.2/lib/tzinfo/timezone.rb:549:in `current_period'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/activesupport-4.1.4/lib/active_support/core_ext/object/try.rb:45:in `public_send'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/activesupport-4.1.4/lib/active_support/core_ext/object/try.rb:45:in `try'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/activesupport-4.1.4/lib/active_support/values/time_zone.rb:223:in `utc_offset'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/activesupport-4.1.4/lib/active_support/values/time_zone.rb:396:in `block in []'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/activesupport-4.1.4/lib/active_support/values/time_zone.rb:396:in `tap'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/activesupport-4.1.4/lib/active_support/values/time_zone.rb:396:in `[]'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/activesupport-4.1.4/lib/active_support/core_ext/time/zones.rb:60:in `find_zone!'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/activesupport-4.1.4/lib/active_support/railtie.rb:20:in `block in <class:Railtie>'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.1.4/lib/rails/initializable.rb:30:in `instance_exec'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.1.4/lib/rails/initializable.rb:30:in `run'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.1.4/lib/rails/initializable.rb:55:in `block in run_initializers'
        from C:/Ruby200-x64/lib/ruby/2.0.0/tsort.rb:150:in `block in tsort_each'
        from C:/Ruby200-x64/lib/ruby/2.0.0/tsort.rb:183:in `block (2 levels) in each_strongly_connected_component'
        from C:/Ruby200-x64/lib/ruby/2.0.0/tsort.rb:219:in `each_strongly_connected_component_from'
        from C:/Ruby200-x64/lib/ruby/2.0.0/tsort.rb:182:in `block in each_strongly_connected_component'
        from C:/Ruby200-x64/lib/ruby/2.0.0/tsort.rb:180:in `each'
        from C:/Ruby200-x64/lib/ruby/2.0.0/tsort.rb:180:in `each_strongly_connected_component'
        from C:/Ruby200-x64/lib/ruby/2.0.0/tsort.rb:148:in `tsort_each'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.1.4/lib/rails/initializable.rb:54:in `run_initializers'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.1.4/lib/rails/application.rb:300:in `initialize!'
        from C:/home/helloworld/config/environment.rb:5:in `<top (required)>'
        from C:/home/helloworld/config.ru:3:in `require'
        from C:/home/helloworld/config.ru:3:in `block in <main>'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/rack-1.5.2/lib/rack/builder.rb:55:in `instance_eval'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/rack-1.5.2/lib/rack/builder.rb:55:in `initialize'
        from C:/home/helloworld/config.ru:in `new'
        from C:/home/helloworld/config.ru:in `<main>'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/rack-1.5.2/lib/rack/builder.rb:49:in `eval'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/rack-1.5.2/lib/rack/builder.rb:49:in `new_from_string'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/rack-1.5.2/lib/rack/builder.rb:40:in `parse_file'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/rack-1.5.2/lib/rack/server.rb:277:in `build_app_and_options_from_config'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/rack-1.5.2/lib/rack/server.rb:199:in `app'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.1.4/lib/rails/commands/server.rb:50:in `app'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/rack-1.5.2/lib/rack/server.rb:314:in `wrapped_app'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.1.4/lib/rails/commands/server.rb:130:in `log_to_stdout'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.1.4/lib/rails/commands/server.rb:67:in `start'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.1.4/lib/rails/commands/commands_tasks.rb:81:in `block in server'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.1.4/lib/rails/commands/commands_tasks.rb:76:in `tap'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.1.4/lib/rails/commands/commands_tasks.rb:76:in `server'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.1.4/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.1.4/lib/rails/commands.rb:17:in `<top (required)>'
        from bin/rails:4:in `require'
        from bin/rails:4:in `<main>'
=> Booting WEBrick
=> Rails 4.1.4 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Notice: server is listening on all interfaces (0.0.0.0). Consider using 127.0.0.1 (--binding option)
=> Ctrl-C to shutdown server
Exiting




またもかわいいエラー。


ここまで来ると笑けてきます。


トラブルシュートこそエンジニアの成長の糧!


と思いつつグーグリネス。


たどり着いた先。


https://github.com/tzinfo/tzinfo/wiki/Resolving-TZInfo::DataSourceNotFound-Errors


アプリケーションのルートディレクトリにあるGemfileなるものに、

gem 'tzinfo-data', platforms: [:mingw, :mswin]

ってのがあるけど、

gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw]

これに変更してくれよという内容でした。
それに加え、tzinfo-dataとの依存関係があるので、

$ gem install tzinfo-data
Successfully installed tzinfo-data-1.2014.6
Parsing documentation for tzinfo-data-1.2014.6
Done installing documentation for tzinfo-data after 1 seconds
1 gem installed


してモジュールをインストールしておきます。

マスターのrailsリポジトリ見たところ、この問題は修正済っぽいです。
https://github.com/rails/rails/blob/master/railties/lib/rails/generators/rails/app/templates/Gemfile

いつリリースされるかわかりませんが、これ以降同じ問題でハマる方が出てこないことを祈ります。


トラブルシュート終わったので、再度サーバー動かすのに挑戦。

変更点の反映のため、bundleのアップデートを忘れずに!

$ bundle update
DL is deprecated, please use Fiddle
Fetching gem metadata from https://rubygems.org/..........
Resolving dependencies...
Using rake 10.3.2
Using i18n 0.6.11
Using json 1.8.1
Using minitest 5.4.0
Using thread_safe 0.3.4
Using tzinfo 1.2.2
Using activesupport 4.1.4
Using builder 3.2.2
Using erubis 2.7.0
Using actionview 4.1.4
Using rack 1.5.2
Using rack-test 0.6.2
Using actionpack 4.1.4
Using mime-types 1.25.1
Using polyglot 0.3.5
Using treetop 1.4.15
Using mail 2.5.4
Using actionmailer 4.1.4
Using activemodel 4.1.4
Using arel 5.0.1.20140414130214
Using activerecord 4.1.4
Using bundler 1.6.5
Using coffee-script-source 1.7.1
Using execjs 2.2.1
Using coffee-script 2.3.0
Using thor 0.19.1
Using railties 4.1.4
Using coffee-rails 4.0.1
Using hike 1.2.3
Using multi_json 1.10.1
Using jbuilder 2.1.3
Using jquery-rails 3.1.1
Using tilt 1.4.1
Using sprockets 2.11.0
Using sprockets-rails 2.1.3
Using rails 4.1.4
Using rdoc 4.1.1
Using sass 3.2.19
Using sass-rails 4.0.3
Using sdoc 0.4.1
Using sqlite3 1.3.9
Using turbolinks 2.2.2
Using tzinfo-data 1.2014.6
Using uglifier 2.5.3
Your bundle is updated!

$ rails server --port=2000
[2014-08-13 02:41:08] INFO  WEBrick 1.3.1
[2014-08-13 02:41:08] INFO  ruby 2.0.0 (2014-05-08) [x64-mingw32]
[2014-08-13 02:41:08] INFO  WEBrick::HTTPServer#start: pid=6012 port=2000




5. まとめ


いろいろトラブルに遭遇しつつ進めたRailsセットアップですが、Blog書きつつすすめてたら1hくらいかかってしまいました。。。
なんという生産性の低さ。
# しかしながら、ググって解決するとか、なんとか問題解決する楽しさはたくさん味わえましたw


今回ワタシがセットアップしたような、Windows + cygwin環境で頑張るのは得策ではなく、
Mac, Ubuntuでサクッとセットアップしたほうがおそらく大分楽です。

コメント

  1. ブログを書きつつ1時間でできたんですね!
    僕はこのブログを参考にさせていただきながら3日もかかってしまいました。
    おっしゃる通りcygwinで頑張るのではなくlinuxなどでセットアップしたほうがよいですね

    返信削除
    返信
    1. 雑多な情報ですが読んでいただいてありがとうございます。
      今時は AWS 使うと楽ですよね。
      cygwin は亜流ですしウェブ上に情報は少ないし割りと茨の道です。。

      削除

コメントを投稿

このブログの人気の投稿

Callback関数を知らん人がまず理解すべきことのまとめ。

C言語でBluetoothスタックを叩きたい人のBluetooth開発入門その1

C++プログラミング入門(1) // 倉庫番プログラムの実装