149

I'm setting Rails 4 application with Ruby 2.0, but I'm getting "Web application could not be started" and get this trace:

cannot load such file -- bundler/setup (LoadError)
  /usr/local/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:53:in `require'
  /usr/local/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:53:in `require'
  /usr/lib/ruby/gems/1.8/gems/passenger-4.0.19/lib/phusion_passenger/loader_shared_helpers.rb:212:in `run_load_path_setup_code'
  /usr/lib/ruby/gems/1.8/gems/passenger-4.0.19/helper-scripts/rack-preloader.rb:96:in `preload_app'
  /usr/lib/ruby/gems/1.8/gems/passenger-4.0.19/helper-scripts/rack-preloader.rb:150:in `<module:App>'
  /usr/lib/ruby/gems/1.8/gems/passenger-4.0.19/helper-scripts/rack-preloader.rb:29:in `<module:PhusionPassenger>'
  /usr/lib/ruby/gems/1.8/gems/passenger-4.0.19/helper-scripts/rack-preloader.rb:28:in `<main>'

My apache2.conf is:

LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-4.0.19/buildout/apache2/mod_passenger.so
   PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-4.0.19
   PassengerDefaultRuby /usr/local/bin/ruby

bundle -v is:

Bundler version 1.3.5

ruby -v is:

ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-linux]

gem env is:

RubyGems Environment:
  - RUBYGEMS VERSION: 2.1.5
  - RUBY VERSION: 2.0.0 (2013-06-27 patchlevel 247) [x86_64-linux]
  - INSTALLATION DIRECTORY: /usr/lib/ruby/gems/1.8
  - RUBY EXECUTABLE: /usr/local/bin/ruby
  - EXECUTABLE DIRECTORY: /usr/lib/ruby/gems/1.8/bin
  - SPEC CACHE DIRECTORY: /root/.gem/specs
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86_64-linux
  - GEM PATHS:
     - /usr/lib/ruby/gems/1.8
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - https://rubygems.org/
  - SHELL PATH:
     - /root/.gems/bin
     - /usr/lib/ruby/gems/1.8/bin/
     - /usr/local/bin
     - /usr/bin
     - /bin
     - /usr/bin/X11
     - /usr/games
     - /usr/sbin
     - /sbin

echo $GEM_PATH is:

/usr/lib/ruby/gems/1.8:/usr/lib/ruby/gems/1.8

Shouldn't GEM_PATH be /usr/lib/ruby/gems/2.0?

Inside the virtual host in apache2.conf I added:

SetEnv GEM_HOME /usr/lib/ruby/gems/1.8

And now it is working.

Is it the right way to fix this?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Mr_Nizzle
  • 6,644
  • 12
  • 55
  • 85

18 Answers18

226

I had almost precisely the same error, and was able to completely fix it simply by running:

gem install bundler

It's possible your bundler installation is corrupt or missing - that's what happened in my case. Note that if the above fails you can try:

sudo gem install bundler

...but generally you can do it without sudo.

Andrew Faulkner
  • 3,662
  • 3
  • 21
  • 24
  • 3
    This was me! Question--is the `sudo` actually neccessary? I used `sudo` but now I'm wondering what would have happened if I hadn't. – Pete Jan 21 '16 at 20:30
  • In my case, yes, but I think it depends on your operating system. I initially did it on a Linux Mint 17, but on an OSX box sudo may not have been necessary. Also, I'd already installed all of my other Ruby-related software using sudo. (Bad practice, but it's not for production-facing applications at this point anyway). – Andrew Faulkner Jan 28 '16 at 20:15
  • 2
    If you're using a different version of Ruby than the system default, you need to NOT use `sudo`! `sudo gem ....` will install the `default` version of the gem, and (in my case, Ubuntu 14.04) `sudo` can't run rvm. – Farfromunique Jun 01 '16 at 23:46
  • Update: you don't generally need sudo for this on OSX. I second what @Farfromunique says. As a general note: only ever use sudo as a last resort for anything to do with Ruby (I've updated my answer to reflect this) – Andrew Faulkner Jul 29 '16 at 04:47
  • 1
    On my macOS system, I needed `sudo` for the system default ruby, but didn't need it for any version of ruby I installed with rvm. – Ben Visness Jun 13 '17 at 15:00
  • 1
    Just a note to say that this was the solution for me using Windows Subsystem for Linux, after the recent Windows update. For some reason the update blew away my RVM ruby installs. – Ben Fulton Nov 07 '17 at 16:32
  • 1
    Hi, I have the same problem as the question using ubuntu with ```gem 2.7.6``` and ```ruby 2.5.0p0``` what to do? – TheCrazyProfessor Apr 13 '18 at 07:12
50

It could be that there was a previous Ruby env installed on your system prior to your installation of 2.0? This might have had an existing GEM_PATH that lead to the /1.8 directory which the installation of version 2.0 simply kept.

The problem you where likely having, then, was that Passenger/Apache was looking in the /2.0 directory when in fact the gems were in the /1.8 directory. Your explicitly telling apache to use the /1.8 directory thus makes sense to fix the problem.

SetEnv GEM_HOME /usr/lib/ruby/gems/1.8

You might also try using the Ruby Version Manager to handle multiple Ruby envs.

Some things I found in Google:

Community
  • 1
  • 1
robmclarty
  • 2,215
  • 2
  • 20
  • 21
30

You most likely have more than one Ruby installed.

If you are using RVM, you probably need to run:

rvm use system

to set the version of ruby to use.

See http://rvm.io/rubies/default

ruby -v

will tell you the version you are currently using.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Michael Guild
  • 808
  • 9
  • 8
28

You can try to run:

bundle exec rake rails:update:bin

As @Dinesh mentioned in Rails 5:

rails app:update:bin
zolter
  • 7,070
  • 3
  • 37
  • 51
10

In my case, the lines appended to the apache config file after installing passenger were as follows:

LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-.0.24/buildout/apache2/mod_passenger.so 
PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-4.0.24 
PassengerDefaultRuby /usr/bin/ruby1.8

But the app requires Ruby 2.0.0 so it took me a while but finally, the error was resolved after specifying a different path using 'PassengerRuby' below, within the Apache virtual host config file for the app:

...
VirtualHost *:80>
  ServerName www.yourhost.com

 **PassengerRuby /home/user/.rvm/gems/ruby-2.0.0-p247**
  # !!! Be sure to point DocumentRoot to 'public'!
  DocumentRoot /somewhere/public    
  <Directory /somewhere/public>
     # This relaxes Apache security settings.
     AllowOverride all
     # MultiViews must be turned off.
     Options -MultiViews
  </Directory>
</VirtualHost...
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
user2997993
  • 121
  • 3
  • This was the case with me as well. In my case `PassengerDefaultRuby` was referring to an old installation of ruby. – Waseem Sep 09 '16 at 15:14
8

I got this error in a fresh Rails app with bundle correctly installed. Commenting out the spring gem in Gemfile resolved the problem.

nachbar
  • 2,643
  • 1
  • 24
  • 34
7

I ran into the same issue, but I think it was due to spring caching some gems and configurations. I fixed it by running gem pristine --all.

This restores installed gems to pristine condition from files located in the gem cache.

or you can just try for your gem like

gem pristine your_gem_name
Ümit Öztürk
  • 105
  • 1
  • 13
NM Pennypacker
  • 6,704
  • 11
  • 36
  • 38
7

Bundler Version maybe cause the issue.

Please install bundler with other version number.

For example,

gem install bundler -v 1.0.10

hobbydev
  • 1,513
  • 19
  • 31
  • 1
    In my case this error occurs after installing **bundler v2**. I resolve it with `gem uninstall -a bundler; gem install bundler -v '<2.0'`. – SergA Jan 23 '20 at 10:02
5

After spend a lot time, trying follow these answers actually after code below it worked for me. Before do it just but be sure that there is no problem in update :)

gem update --system

Fábio BC Souza
  • 1,170
  • 19
  • 22
2

Other possible situation: you have multiple users defined in your server environment. In that case, running

passenger-config --ruby-command

will give you the necessary command to specify your nginx/sites-enabled/relevant_application file with your use case, example:

passenger-config was invoked through the following Ruby interpreter:
Command: /home/other_user/.rbenv/versions/2.4.5/bin/ruby
Version: ruby 2.4.5p335 (2018-10-18 revision 65137) [x86_64-linux]
To use in Apache: PassengerRuby /home/other_user/.rbenv/versions/2.4.5/bin/ruby
To use in Nginx : passenger_ruby /home/other_user/.rbenv/versions/2.4.5/bin/ruby
To use with Standalone: /home/other_user/.rbenv/versions/2.4.5/bin/ruby /usr/bin/passenger start
Jerome
  • 5,583
  • 3
  • 33
  • 76
1

For me the problem was associating RVM Ruby with Passenger. So I needed to integrate RVM ruby wrapper to passenger config file.

I find out rvm ruby wrapper path with command:

passenger-config --ruby-command

I took the path from the result and entered to a passenger config in nginx/passenger.conf:

passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
passenger_ruby /usr/local/rvm/gems/ruby-2.3.1/wrappers/ruby;
Gediminas Šukys
  • 7,101
  • 7
  • 46
  • 59
1

This was happening in the production environment for me.

rm /vendor/bundle

then bundle install --deployment

resolved the issue.

vanboom
  • 1,274
  • 12
  • 20
1

I've fixed that problem by creating test rails project and install all gems then I've replaced my current Gemfile.lock with the test and all thing works fine.

I think that this problem from bundler versions with hosting, so please make sure that hosting bundler is the same version with your project.

hsul4n
  • 491
  • 7
  • 15
0

I had this because something bad was in my vendor/bundle. Nothing to do with Apache, just in local dev env.

To fix, I deleted vendor\bundle, and also deleted the reference to it in my .bundle/config so it wouldn't get re-used.

Then, I re-bundled (which then installed to GEM_HOME instead of vendor/bundle and the problem went away.

thewoolleyman
  • 592
  • 6
  • 13
0

NOTE: My hosting company is Site5.com and I have a Managed VPS.

I added env variables for both GEM_HOME and GEM_PATH to the .htaccess file in my public_html directory (an alias to the public directory in the rails app)

They were not needed before so something must have changed on the hosts side. It got this error after touching the restart.txt file to restart the passenger server.

Got GEM_PATH by:

echo $GEM_PATH

Got the GEM_HOME by:

gem env

 RubyGems Environment:
   - RUBYGEMS VERSION: 2.0.14
   - RUBY VERSION: 2.0.0 (2013-11-22 patchlevel 353) [x86_64-linux]
   - INSTALLATION DIRECTORY: /home/username/ruby/gems
   - RUBY EXECUTABLE: /usr/local/ruby20/bin/ruby
   - EXECUTABLE DIRECTORY: /home/username/ruby/gems/bin
   - RUBYGEMS PLATFORMS:
     - ruby
     - x86_64-linux
   - GEM PATHS:
      - /home/username/ruby/gems
      - /usr/local/ruby2.0/lib64/ruby/gems/
   - GEM CONFIGURATION:
      - :update_sources => true
      - :verbose => true
      - :backtrace => false
      - :bulk_threshold => 1000
      - "gem" => "--remote --gen-rdoc --run-tests"
      **- "gemhome" => "/home/username/ruby/gems"**
      - "gempath" => ["/home/username/ruby/gems", "/usr/local/ruby2.0/lib64/ruby/gems/"]
      - "rdoc" => "--inline-source --line-numbers"
   - REMOTE SOURCES:
      - https://rubygems.org/

Updated .htaccess file with the following lines:

SetEnv GEM_HOME /usr/local/ruby2.0/lib64/ruby/gems/
SetEnv GEM_PATH /home/username/ruby/gems:/usr/local/ruby20/lib64/ruby/gems/:/home/username/ruby/gems:/usr/
Rob Little
  • 21
  • 4
0

i had the same issue and tried all the answers without any luck.

steps i did to reproduce:

  1. rvm instal 2.1.10
  2. rvm gemset create my_gemset
  3. rvm use 2.1.10@my_gemset
  4. bundle install

however bundle install installed Rails, but i still got cannot load such file -- bundler/setup (LoadError)

finally running gem install rails -v 4.2 fixed it

0

The version of ruby version which phusion passenger was used is differenced with your rails app.

<IfModule mod_passenger.c>
  PassengerRoot /usr/local/rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/passenger-6.0.2
  PassengerDefaultRuby /usr/local/rbenv/versions/2.5.3/bin/ruby
</IfModule>

Make sure the version on httpd config is the same with rails app.

giapnh
  • 2,950
  • 24
  • 20
-1

In my situation it was matter of the permissions:

 sudo chmod -R +777 <your_folder_path>
petrov
  • 57
  • 1
  • 1
  • 9
  • 1
    -1 : Never put 777 on files or directories. If there is a split between "owner", "group" and "others", it's because it's a security matter. – Florian Doyen Jun 06 '20 at 15:51