2018年以降の記事はGitHub Pagesに移行しました

Rubyのgemバージョン管理ができるBundlerを実行できなかった

あらすじ

  • gemでライブラリを入れまくり、updateしまくりでバージョンの不整合が起こり復旧不可能になった
  • 同じ過ちは繰り返したくない…ので今度はうまく管理したい
  • Bundlerを使えばgemのバージョン管理ができるみたい
    • 設定さえあればどの環境も同じにする事が出来る?

Bundlerを入れてみます。

環境

  • Windows XP
  • Ruby 1.8.7
  • 必要なDLLなど
    • zlib.dll
    • readline.dll
    • iconv.dll
    • iconv.lib
    • iconv_a.lib
    • ssleay32.dll
    • libeay32.dll

手順

Rubygemsインストール
$ ruby setup.rb
RubyGems 1.7.2 installed

・ソ=== 1.7.2 / 2011-04-05

* 1 Bug Fix:
  * Warn on loading bad spec array values (ntlm-http gem has nil in its cert
    chain)


------------------------------------------------------------------------------

RubyGems installed the following executables:
        C:/ruby/bin/gem
  • バージョン、環境を確認
$ gem -v
1.7.2

$ gem env
RubyGems Environment:
  - RUBYGEMS VERSION: 1.7.2
  - RUBY VERSION: 1.8.7 (2010-12-23 patchlevel 330) [i386-mswin32]
  - INSTALLATION DIRECTORY: C:/ruby/lib/ruby/gems/1.8
  - RUBY EXECUTABLE: C:/ruby/bin/ruby.exe
  - EXECUTABLE DIRECTORY: C:/ruby/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86-mswin32-60
  - GEM PATHS:
     - C:/ruby/lib/ruby/gems/1.8
     - %USERPROFILE%/.gem/ruby/1.8
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :benchmark => false
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - http://rubygems.org/
  • C:\ruby\binにgemコマンドが格納される
  • C:\ruby\lib\ruby\site_ruby\1.8がgemのパスとなっており、rbconfigやrubygemsが作成されている
    • rubyフォルダ下と%USERPROFILE%下に.gemフォルダも定義されている。各ユーザ用かな?

※proxyエラー等が起こった場合は~/に.gemrcファイルを作成し、http-proxy: PROXY_URLと記述すればよい?

Bundlerインストール
  • Bundlerは最新版を導入する(0.X系と1.X系で大きく変わったところがあるらしいので気をつける)
$ gem install bundler
Fetching: bundler-1.0.20.gem (100%)
Successfully installed bundler-1.0.20
1 gem installed
Installing ri documentation for bundler-1.0.20...
Installing RDoc documentation for bundler-1.0.20...
  • bundleコマンドが実行できるようになるが、今うってもエラーが出る。
$ bundle install
Could not locate Gemfile

Gemfileを作成する必要があるようだ。

Gemfile作成
source :rubygems
source "http://rubygems.org"
  • 次にインストールするgemとバージョンを指定する
gem 'rails', '2.3.5'
  • バージョンの指定方法は以下仮でまとめ
記載なし 最新版
'1.5' 1.5
'>=1.5' 1.5以上の最新版?
'~>1.5' 1.5以上2.0未満?
  • プロジェクトに配備
    • プロジェクトのルートフォルダにGemfileを配備(redmineで使おうと思ったらredmine/Gemfileとする)
gemインストール
  • 下記コマンドでPATHへgemがインストールされる
bundle install PATH
  • お勧めはプロジェクトルート/vendor/bundleらしい
$ bundle install vendor/bundle
Fetching source index for http://rubygems.org/
Fetching source index for http://rubygems.org/
Installing rake (0.9.2)
Installing activesupport (2.3.5)
Installing rack (1.0.1)
Installing actionpack (2.3.5)
Installing actionmailer (2.3.5)
Installing activerecord (2.3.5)
Installing activeresource (2.3.5)
Installing rails (2.3.5)
Using bundler (1.0.20)
Your bundle is complete! It was installed into ./vendor/bundle
The path argument to `bundle install` is deprecated. It will be removed in version 1.1. Please use `bundle install --path vendor/bundle` instead.

するとこんな感じでファイルが作成される。

redmine # redmineルートフォルダ
┃
┣(略)redmine系のファイルとフォルダ
┃
┣Gemfile # 今作ったGemfile
┣Gemfile.lock # Gemfileからバージョンの整合性などを確認している?
┣.bundle
┃┗config # bundleのコンフィグファイル
┗vendor
 ┃
 ┣(略)元々あるpluginなど
 ┃
 ┗bundle # インストールしたgem格納フォルダ
  ┗ruby
   ┗...
  • ところで、インストール後のコメントを見るとこのbundle install PATHコマンドは1.1でなくなる予定のようで、今後はきちんと--pathオプションを指定しなければならないらしい
bundle install --path vendor/bundle
Fetching source index for http://rubygems.org/
Fetching source index for http://rubygems.org/
Installing rake (0.9.2)
Installing activesupport (2.3.5)
Installing rack (1.0.1)
Installing actionpack (2.3.5)
Installing actionmailer (2.3.5)
Installing activerecord (2.3.5)
Installing activeresource (2.3.5)
Installing rails (2.3.5)
Using bundler (1.0.20)
Your bundle is complete! It was installed into ./vendor/bundle
  • また、システムに同じgemが入っていたらシステム優先とする--disable-shared-gemsオプションというものがあるようだが、最新バージョンではやらなくてよいみたい? 「--pathオプション使え」と出てる
 bundle install vendor/bundle --disable-shared-gems
The disable-shared-gem option is no longer available.

Instead, use `bundle install` to install to your system,
or `bundle install --path path/to/gems` to install to an isolated
location. Bundler will resolve relative paths relative to
your `Gemfile`.

確認(ここから先は失敗したので自分用備忘録…)

  • gemにはbundler以外なし
gem list

*** LOCAL GEMS ***

bundler (1.0.20)
  • bundlerで導入したgemを確認するにはプロジェクトのルートフォルダ下で以下コマンド
bundle show
Gems included by the bundle:
  * actionmailer (2.3.5)
  * actionpack (2.3.5)
  * activerecord (2.3.5)
  * activeresource (2.3.5)
  * activesupport (2.3.5)
  * bundler (1.0.20)
  * rack (1.0.1)
  * rails (2.3.5)
  * rake (0.9.2)
プロジェクトでbundle内のgemを読み込むようにする

今回はredmineを参考にしてみる

  • config/boot.rbを修正
  • config/preinitializer.rbを作成
  • i18nが足りないと言われたのでGemfileに足し再度インストール
ruby script\server -e production
Missing the i18n 0.4.2 gem. Please `gem install -v=0.4.2 i18n`
gem 'rails', '2.3.5'
gem 'i18n', '0.4.2' # Add
$ ruby script\server -e production
C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:32:in `gem_original_require': no such file to load -- tree (MissingSourceFile)
        from C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:32:in `require'

うーん。treeがない…らしい? 前回インストールしてたんだったっけ?

gem install tree

$ ruby script\server -e production
=> Booting WEBrick
=> Rails 2.3.5 application starting on http://0.0.0.0:3000
C:/redmine/config/../vendor/rails/railties/lib/rails/gem_dependency.rb:119:Warning: Gem::Dependency#version_requirements is deprecated and will be removed on or after August 2010.  Use #requirement
C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- spreadsheet (MissingSourceFile)

spreadsheetも足りない?

gem install spreadsheet
||,

>||
$ ruby script\server -e production
=> Booting WEBrick
=> Rails 2.3.5 application starting on http://0.0.0.0:3000
C:/redmine/config/../vendor/rails/railties/lib/rails/gem_dependency.rb:119:Warning: Gem::Dependency#version_requirements is deprecated and will be removed on or after August 2010.  Use #requirement
Missing these required gems:
  rubytree
  coderay  ~> 0.9.7

You're running:
  ruby 1.8.7.330 at C:/ruby/bin/ruby.exe
  rubygems 1.3.7 at %userprofile%/.gem/ruby/1.8, C:/ruby/lib/ruby/gems/1.8

Run `rake gems:install` to install the missing gems.

まだ足りん?rubytreeとcoderay? Run rake gems:installするといいと書いてある…。

$ rake gems:install
rake/rdoctask is deprecated.  Use rdoc/task instead (in RDoc 2.4.2+)
WARNING: 'task :t, arg, :needs => [deps]' is deprecated.  Please use 'task :t, [args] => [deps]' instead.
    at C:/redmine/lib/tasks/email.rake:170
gem.bat install rubytree

unrecognized option `--files'

For help on options, try 'rdoc --help'

========================================================================

 Thank you for installing rubytree.

              WARNING: SIGNIFICANT API CHANGE in 0.8.0 !
              ------------------------------------------

 Please note that as of 0.8.0 the CamelCase method names are DEPRECATED.

 The new method names follow the ruby_convention (separated by '_').

 The old CamelCase methods still work (a warning will be displayed),
 but may go away in the future.

 Details of the API changes are documented in the API-CHANGES file.

========================================================================
Successfully installed rubytree-0.8.1
1 gem installed
Installing ri documentation for rubytree-0.8.1...
gem.bat install coderay --version "~> 0.9.7"
Successfully installed coderay-0.9.8
1 gem installed
Installing ri documentation for coderay-0.9.8...
Installing RDoc documentation for coderay-0.9.8...

…ここで一回断念。どうもRedmineにもともと入っていたファイルを消してしまっている疑惑が発覚したのだが、Redmineをいれなおしてもbundlerからだと起動できなかった……。

うーん。普通のプロジェクトだと使えたのでローカルに存在しているRedmineのどこかでしくってると思うんだけど……。