Confusion about bundler path - passenger

I opened a dedicated hosting account on DreamHost. I deployed an rails app to that. I got the following error.
You have already activated rack 1.2.1, but your Gemfile requires rack 1.3.6. Using bundle exec may solve this.
I checked the version.
$ gem list -d rack
rack (1.2.1, 1.1.0, 1.0.1, 1.0.0)
Author: Christian Neukirchen
Rubyforge: http://rubyforge.org/projects/rack
Homepage: http://rack.rubyforge.org
Installed at (1.2.1): /usr/lib/ruby/gems/1.8
(1.1.0): /usr/lib/ruby/gems/1.8
(1.0.1): /usr/lib/ruby/gems/1.8
(1.0.0): /usr/lib/ruby/gems/1.8
Rack 1.3.6 is not there. But when I checked it with "bundle show" it's already installed. (Actually I did "bundle install --deployment")
$ bundle show rack
/.../my_rails_app_root/vendor/bundle/ruby/1.8/gems/rack-1.3.6
And I have config/setup_load_paths.rb
if ENV['MY_RUBY_HOME'] && ENV['MY_RUBY_HOME'].include?('rvm')
begin
rvm_path = File.dirname(File.dirname(ENV['MY_RUBY_HOME']))
rvm_lib_path = File.join(rvm_path, 'lib')
$LOAD_PATH.unshift rvm_lib_path
require 'rvm'
RVM.use_from_path! File.dirname(File.dirname(__FILE__))
rescue LoadError
# RVM is unavailable at this point.
raise "RVM ruby lib is currently unavailable."
end
end
ENV['BUNDLE_GEMFILE'] = File.expand_path('../Gemfile', File.dirname(__FILE__))
require 'bundler/setup'
Actually I found a solution. Just "gem install rack -v 1.3.6" fixed the problem.
But why does passenger pick up system's rack gem(or user's rack gem) instead of bundle's rack gem? How do you avoid this problem?
Thanks.
Sam

Typically this is what you get when you run your app (e.g. rails server) without prefixing the command with bundle exec.
When you ran bundle install --deployment, bundler took your gems from ./vendor/cache and whacked them in ./vendor/bundle. From then on, Bundler knows where to find them, but you have to be running the app through Bundler.
Rubygems, however, does not know where these gems are, which is why they are not shown when you run the rubygems command gem list. When you installed Rack 1.3.6 using rubygems, naturally rubygems found it and your app started working.
Not using bundler to start your app lets rubygems satisfy the requirements of your app according to it's own method, and that is a fairly random - I would be quite surprised if your app is currently running all the same gem versions that you ran your tests on, for instance (Eek!)
The approach I usually take is to uninstall all the gems from the server, install a single version of rubygems and bundler, and then rely exclusively on Bundler to maintain my app's gems. The beauty of bundler is that it calculates a valid set of gems and reliably uses it.
Hope this helps!

Related

`require': cannot load such file -- rack/handler (LoadError)

I'm not a Ruby person, so this may be a 101 question. I'm just trying to use a utility that happens to be written in Ruby.
I'm using tilemaker, a utility in the openstreetmap ecosystem. It creates tiles in the mbtiles format. The repository comes with a simple utility to serve the tiles on a browser to test the files you create. This utility is written in Ruby, and is what I'm having trouble with.
The repo's README has instructions for the server utility. The installation instructions read:
(If you don't already have them, you'll need to install Ruby and the required gems to run the demonstration server. On Ubuntu, for example, sudo apt install sqlite3 libsqlite3-dev ruby ruby-dev and then sudo gem install sqlite3 cgi glug rack.)
I'm on Debian 11 (on Qubes, so I don't mind running sudo gem install as they recommend). I hope this is close enough to Ubuntu but maybe this is related to the problem.
This is what I get:
$ ruby server.rb ~/countries-raster.mbtiles
Starting local server
Traceback (most recent call last):
3: from server.rb:22:in `<main>'
2: from server.rb:118:in `<class:MapServer>'
1: from /usr/lib/ruby/vendor_ruby/rubygems/core_ext/kernel_require.rb:85:in `require'
/usr/lib/ruby/vendor_ruby/rubygems/core_ext/kernel_require.rb:85:in `require': cannot load such file -- rack/handler (LoadError)
What am I missing here? Thanks.
It appears that the issue is that you are using a 3.X release of rack.
According to the CHANGELOG as of version 3.0 Rack::Handler was removed from rack and pulled out into its own gem (rackup).
To resolve the issue you will need to either use an older version of rack
gem 'rack', '~> 2.2'
Or you will need to add the rackup gem as a dependency:
gem 'rack'
gem 'rackup'
Either option will provide access to rack/handler:
rack (2.2.5) - Source
rackup (0.2.3) - Source
for a better setup under your user, this could be done:
ensure to have ruby running under your user by:
ruby -v # 2.7 or higher is better
then create a file besides your script server.rb called Gemfile:
# Gemfile
source "https://rubygems.org"
gem "rack"
gem "sqlite3"
gem "cgi"
gem "glug"
After that, ensure to have bundler installed (this is a gem to manage gem versioning), by:
gem install bundler
If you face a permission error, is because your ruby program is under root user. So you can use via sudo bundle install, but anything else must be run under sudo, or you can install and setup rvm which will install and configure ruby under your user.
After installing you can call:
bundle install
# will install all the gems needed and will lock the latest versions for you inside Gemfile.lock
run your server by:
bundle exec ruby server.rb
by running via bundle exec your telling to ruby to use the gems installed and versioned by the Gemfile.lock. This guarantees to your software to require the specific versions and avoid collisions or anything else.

Incompatible version for gem "activesupport" while installing "threetaps-client" gem

I'm fairly new to rails and I encountered this gem conflict, while running bundle install, between ActiveSupport and threetaps-client (which I need to use for my project).
I tried removing the Gemfile.lock file and running bundle install again but it gave me the same error message again. I also tried running bundle update which also gave the same result :(
Bundler could not find compatible versions for gem "activesupport":
In snapshot (Gemfile.lock):
activesupport (3.2.13)
In Gemfile:
threetaps-client (>= 0) ruby depends on
activesupport (~> 3.0.0) ruby
Running `bundle update` will rebuild your snapshot from scratch, using only
the gems in your Gemfile, which may resolve the conflict.
The issue here is that you are using Rails 3.2 (and thus, activesupport 3.2). However, threetaps-client is version locked to rails/activesupport 3.0.x. The easiest solution to this would be to downgrade Rails to 3.0 in your Gemfile with:
gem "rails", "~> 3.0"
And then remove the Gemfile.lock and bundle install again. You should be good to go after that.
EDIT
I was able to get the gem to support activesupport 3.2 (I think). The tests do not pass on this branch, but they did not pass on master either. I assume this probably has to do with credentials or something. Update your Gemfile to use this repo for threetaps-client
gem "threetaps-client", git: "git#github.com:ehowe/3taps-Ruby-Client"
Insert required "your mileage may vary" warning here.

Including the bundler gem itself in my Rails app

Bundler is great. But there's one gem that it won't work for, and that's the bundler gem itself, which still needs to be installed manually:
Could not load the bundler gem. Install it with `gem install bundler`.
Besides being an extra step, this opens the door to version incompatibilities with Bundler itself.
Is there a way to include Bundler itself in my app, so that it relies on the included bundler gem?
No. The ruby gems stay on the user environment, you can't distribute it with your project, the user need to install the bundler.
and Bundler can't bundle itself. :)

Problems with bundled gems and datamapper: conflict with muti_json version?

I have a site running with nginx/unicorn/sinatra (bundler/rvm).
After my last bundle update, I am getting an error:
in `raise_if_conflicts': Unable to activate dm-serializer-1.2.1, because multi_json-1.3.5 conflicts with multi_json (~> 1.0.3)
My Gemfile is:
source "http://rubygems.org"
gem 'unicorn'
gem 'sinatra'
gem 'datamapper'
gem 'dm-mysql-adapter'
gem 'haml'
gem 'sass'
gem 'omniauth-twitter'
Gemfile.lock does not have any reference to multi_json 1.0.3
Any ideas?
Solution was:
check Gemfile.lock to see which gem(s) bring in later version (in this case - omniauth-twitter)
Find a version of 'offender' that does not require too high version
Rollback later versions, lock to a proper version in Gemfile
In this particular case, Gemfile that works needed lines:
gem 'omniauth-twitter', '0.0.9'
gem 'multi_json', '~> 1.0.3'
One of the gems in your bundle has an older version of multi_json as a dependency it looks like. See if bundle viz tells you. You'll need to install the ruby-graphviz gem and graphviz itself if you don't have them installed already, though.
Another way to see what's up is to add multi_json to your gemfile at the version you're trying to upgrade to, then do a bundle install and see what errors come out.
This is how to fix this problem:
rvm uninstall multi_json
It will tell you that you have many versions installed, show you a list of them, and ask you which one exactly you want to uninstall.
Try the first one, if it tells you that it is used by some other gem, try the second one, and so on. Keep deleting all the unused versions until only one remains.
This is how I do it, but there may be some clearner solution. If anyone knows it, thanks for sharing it with us.

How to switch between different version of gem installed?

I have three version of rack installed on local machine (rack (1.4.1, 1.3.6, 1.3.5)). For some gem (such as Cucumber), it requires a lower version of rack to be activated?
I have tried with bundle but there is no good.
When executed, cucumber will still use the activated rack with version 1.4.1 of the system. Bundlespecifies which gem should be installed but doesn't ensure which gem will be activated.
How could I activate certain version of rack?
You can specify a version in gemfile of your project
gem "rack", "1.3.5"
Pointed by matt:
To use gem specified in Gemfile:
bundle exec cucumber
Use bundler it will manage it for you.

Resources