ユーザ用ツール

サイト用ツール


bundler

bundler

rubygems の管理ツール。バージョン依存を解決したり、アプリごとにgem管理を行える

Gemfileのバージョン指定

gem "hoge"              # 未指定、無ければ最新版を入れ、既にあればそれを使う
gem "hoge", "=1.3.2"    # バージョン 1.3.2 のみ
gem "hoge", ">=1.3.2"   # バージョン 1.3.2 以降のどれか
gem "hoge", "~>1.3.2"   # バージョン 1.3.2 以降 1.4 未満のどれか
gem "hoge", "~>1.3"     # バージョン 1.3 以降 2 未満のどれか

bundlerのインストール

bundler自体はgemで普通にインストールする。最近のRubyには最初からインストールされているので以下は不要

$ gem install bundler

bundlerを使う

Gemfileを作成する

$ bundle init 

gemライブラリインストール先の指定

–pathによる指定は非推奨。以下で設定する

bundle config set --local path ./vendor

Gemの依存性を解決

$ bundle install 

最新版ではないが条件を満たすものが既にインストールされている場合、アップデートされない。

Gemを更新

$ bundle update

条件を満たす最新版にアップデートされる。

bundler自身を更新

普通にgemでインストールする

$ gem install bundler 

バージョンを指定する場合

$ gem install bundler -v '2.5.3'

アプリ毎にGemを管理

 $ bundle install

実行

bundleでpathを指定してインストールした場合、普通にrubyやirbコマンドを叩いてもライブラリパスに入っていないのでインストールしたgemを使えない。

 $ bundle exec ruby 

とする。

Gemでインストールされるコマンドを使う

例えばrspec等のコマンドを持っているgemをbundleでpathを指定してインストールした場合、パスもイブラリパスも通っていないので、そのままでは使えない。

 $ bundle install --binstubs
 

と打つと ./bin/rspec が作成される。

 $ ./bin/rspec 

でrspecを実行できる。

コマンドを作成する場所も指定可能

 $ bundle install --binstubs=vendor/bin

Gemを開発する

bundlerを利用するGemを開発するには…

$ bundle gem PROJECT_NAME

これで、プロジェクトのひな形が作られる。

gemインストールエラー対策

nokogiriでlibxml2が見つからない

  • ruby 2.5.1
  • bundler 2.0.1
  • gem 2.7.6
  • macOS 10.14.3
Fetching nokogiri 1.8.2
Installing nokogiri 1.8.2 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: /Users/nullpon/Documents/private/bluenotes-rails/vendor/bundle/ruby/2.5.0/gems/nokogiri-1.8.2/ext/nokogiri
/Users/nullpon/.rbenv/versions/2.5.1/bin/ruby -r ./siteconf20190225-38046-kqdrce.rb extconf.rb --use-system-libraries
checking if the C compiler accepts ... yes
checking if the C compiler accepts -Wno-error=unused-command-line-argument-hard-error-in-future... no
Building nokogiri using system libraries.
ERROR: cannot discover where libxml2 is located on your system. please make sure `pkg-config` is installed.
*** 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.
$ brew install libxml2
$ bundle config build.nokogiri --with-libxml2-include=/usr/local/opt/libxml2/include/libxml2

ググると –use-system-libraries が必要と出てくるが最近はデフォルトで付いてるらしい。

mysql2でsslライブラリが見つからない

  • ruby 2.5.1
  • bundler 2.0.1
  • gem 2.7.6
  • macOS 10.14.3
compiling statement.c
linking shared-object mysql2/mysql2.bundle
ld: library not found for -lssl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [mysql2.bundle] Error 1

make failed, exit code 2
$ brew install openssl
$ bundle config build.mysql2 "--with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include"

rmagickで wand/MagickWand.h が見つからない

checking for wand/MagickWand.h... no

Can't install RMagick 2.13.4. Can't find MagickWand.h.

wand/MagickWand.h は imagemagick 6に入っている(7にはない)

brew install imagemagick@6
brew link --force imagemagick@6

DidYouMean

Calling `DidYouMean::SPELL_CHECKERS.merge!(error_name => spell_checker)' has been deprecated. Please call `DidYouMean.correct_error(error_name, spell_checker)' instead.

bundler 2.2 or 2.3あたりを使っていると表示された。bundler自身を更新することで解決する

Gitリポジトリからgemをインストール

Gemfileに以下のように記述

gem "http-server", git: 'https://github.com/paulownia/http-server.git'

または

git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }

gem "http-server", github: 'paulownia/http-server'
bundler.txt · 最終更新: 2023/12/29 07:38 by nullpon