Ruby on Rails 環境構築時に `require’: cannot load such file — bootsnap/setup (LoadError)

Ruby も初めてなのでお作法的なのをとりあえずメモ

お作法

  1. bundler を使って gem 管理する。
    $ gem install bundler
    

    Gemfile を作成する

    $ bundle init
    

    作成した Gemfile に使用する gem を記述していく。
    ちなみに bundler v1.15 から bundle add GEM_NAME で自動で Gemfile に追記して、 bundle install をしてくれるコマンドが増えた。

  2. bundle install のときには --path を指定する

    指定しない場合はグローバルインストールになってしまうので注意。
    実際にはバージョンが混在しても問題ないらしいが個人的にはごちゃごちゃさせたくない。

    $ bundle install --path vendor/bundle
    

    プロジェクトの .bundle/config に path が記述されるので二回目以降は不要になる。
    初回時のパス指定を忘れがちなので、 bundle config path vendor/bundle を実行しておくと、
    ユーザーの .bundle/config に path が記述され、うっかりオプションを忘れてもここから参照してくれるのでやっておくと良い、かもしれない。

    優先順位は上にある方が高く、下の設定をオーバーライドする

    1. Local config (app/.bundle/config)
    2. Environmental variables (ENV)
    3. Global config (~/.bundle/config)
    4. Bundler default config

環境構築でハマった

さてやるかと思って、下のコマンドを実行したらなんかエラー出た

$ bundle exec rails new src \
    --skip-coffee --skip-turbolinks --skip-sprockets \
    --webpack=angular --database=postgresql
Bundled gems are installed into `./vendor/bundle`
       rails  webpacker:install
Traceback (most recent call last):
        3: from bin/rails:3:in `<main>'
        2: from bin/rails:3:in `require_relative'
        1: from /Users/xxx/rails-app/src/config/boot.rb:4:in `<top (required)>'
/Users/xxx/rails-app/src/config/boot.rb:4:in `require': cannot load such file -- bootsnap/setup (LoadError)
       rails  webpacker:install:angular
Traceback (most recent call last):
        3: from bin/rails:3:in `<main>'
        2: from bin/rails:3:in `require_relative'
        1: from /Users/xxx/rails-app/src/config/boot.rb:4:in `<top (required)>'
/Users/xxx/rails-app/src/config/boot.rb:4:in `require': cannot load such file -- bootsnap/setup (LoadError)
         run  bundle exec spring binstub --all
* bin/rake: spring inserted
* bin/rails: spring inserted

bootsnap は入ってるのになんでや!と思って色々調べたら何故か bin の中に webpack と webpack-dev-server がなかった。

$ bundle exec rails webpacker:install:angular

を再実行しても効果なし。

最終的には下のコマンドで解決した。
先に webpacker:install をやるのがキモらしい。

$ bundle exec rails webpacker:install
$ bundle exec rails webpacker:install:angular