28

I have a small web app, which uses a bunch of gems. Some of them are only used for test and development environments. Now, when I try to start unicorn on the production server using the following command, it fails.

unicorn_rails -E production -D -c config/unicorn.rb

The error I see in the log files is:

Refreshing Gem list
Could not find gem 'spork (>= 0.9.0.rc2, runtime)' in any of the gem sources listed in your Gemfile.
Try running `bundle install`.

I've pasted my gemfile below:

source 'http://rubygems.org'

gem 'rails', '3.0.1'
gem 'unicorn'
gem 'mongoid', '>= 2.0.0.beta.19'
gem 'devise'
gem 'cancan'
gem 'haml', '>= 3.0.0'
gem 'bson'
gem 'bson_ext'
gem 'formtastic'
gem 'bluecloth'

group :production do
  gem 'capistrano'
end

group :development do
  gem 'haml-rails'
  gem 'hpricot', '0.8.2'
  gem 'ruby_parser', '2.0.5'
  gem 'less'
  gem 'rspec-rails', '>= 2.0.1'
end

group :development,:test do
  gem 'spork', '>=0.9.0.rc2'
  gem 'mongoid-rspec'
end

group :test do
  gem 'factory_girl_rails'
  gem 'autotest'
  gem 'cucumber-rails'
  gem 'cucumber'
  gem 'capybara'
  gem 'shoulda'
  gem 'database_cleaner'
  gem 'test_notifier'
  gem 'rspec', '2.0.1'
  gem 'launchy' 
end

Bundler is supposed to detect the right environment and ignore the other gems, right? Right now, I am deleting all the lines which are not in the default group on the server to get this working, but that's an ugly hack.

Khaja Minhajuddin
  • 6,653
  • 7
  • 45
  • 47

3 Answers3

59

After a lot of digging I found the fix for this issue. All I had to do was run bundle install --without development test before starting the server. This adds a .bundle/config file in the rails root with the line BUNDLE_WITHOUT: test:development . Now whenever you run bundle install or start the server it'll ignore those groups.

From the documentation

The Bundler CLI allows you to specify a list of groups whose gems bundle install should not install with the --without option. To specify multiple groups to ignore, specify a list of groups separated by spaces.

bundle install --without test bundle install --without development test After running bundle install --without test, bundler will remember that you excluded the test group in the last installation. The next time you run bundle install, without any --without option, bundler will recall it.

Also, calling Bundler.setup with no parameters, or calling require "bundler/setup" will setup all groups except for the ones you excluded via --without (since they are obviously not available).

Khaja Minhajuddin
  • 6,653
  • 7
  • 45
  • 47
  • Thank you! Was looking for the syntax for the config file :) BUNDLE_WITHOUT: test:development – iNulty Jan 03 '14 at 09:57
  • 2
    I think this is just bad design. Why not ignore anything non-dev by default? You shouldn't have to group in the gemfile and exclude through the command line. It should default to the dev environment and then you include either production or test. Otherwise it's a lot of redundancy and headaches during development. – PhillipKregg Sep 27 '14 at 21:28
  • @PhillipKregg Bundler has no concept of a "dev environment" as distinct from gem groups. Rails does, but Bundler shouldn't couple its installation behavior to the internals of another gem (and not every project uses Rails). – Marnen Laibow-Koser May 20 '15 at 22:36
  • Today I learned. – Jared Menard Feb 06 '23 at 20:52
4

In my case it was installing gems from jenkins env. So i had to set my own bundle_without variable in capistrano.

Gemfile

group :test, :development, :jenkins do  
  gem 'test-unit', '1.2.3'  
  gem 'rspec-rails'
end

deploy.rb

set :bundle_without, [:development, :test, :jenkins]
Pratik Khadloya
  • 12,509
  • 11
  • 81
  • 106
-2

You haven't defined a production group =)

Icid
  • 1,444
  • 1
  • 12
  • 21