Why bundle exec rake does not run to proper version of ruby - ruby

I am currently having a strange issue with bundler and ruby.
if I type:
$ which ruby
I get:
/home/martinos/.rubies/1.8.7-p370/bin/ruby
And when I type:
$ which bundle
I get
/home/martinos/.gem/ruby/1.8.7/bin/bundle
But for some reason when I run
$ bundle exec rake db:migrate
The task is run with ruby 1.9.3 (I have written a puts RUBY_VERSION in environment.rb)
Any one as an idea why this happens?
Here is more infos:
When I type:
$ which rake
I get:
/home/martinos/.gem/ruby/1.8.7/bin/rake
But if I
$ head -1 `which rake`
I get:
#!/usr/bin/env ruby1.9.1

There are a variety of pieces that could be in play. The first is that it could be a conflict between your Ruby version management tools and your global gems. Meaning, I suppose it is possible that you only have a Rake version that can work on Ruby 1.9.1 that is in your global set. So when you fire up Rake it is forced to run in Ruby 1.9.1.
What you may want to do is create a directory specific gemset. If you're using RVM you can see the documentation on how to do that by looking at their Gemset documentation. Once that is in place with the Ruby version you want to test with, then do a gem install of Rake at the version that will work with that Ruby version. At that point you should find that the Ruby version being used to run Rake in that directory will be the same as the version you have running.
I apologize if this does not answer your question, or if you have thought of this approach already. Trying to wrap my head around this without the ability to reproduce the problem is a tricky deal.

Related

How to fix "Unknown ruby interpreter version (do not know how to handle): RUBY_VERSION."

Today i just want to setup a jekyll Blog on my Mac, and have already install ruby 2.3.0, but when make '$ jekyll serve', it's error. and show this in terminal:
Error information:
Unknown ruby interpreter version (do not know how to handle): RUBY_VERSION.
So it looks like
bundle exec jekyll new
will create the a Gemfile with the line
ruby RUBY_VERSION
I believe you'll want to edit that file to be e.g.
ruby '2.1.1'
Coincidentally today I am also trying to setup Jekyll and am seeing the same problem. I am using RVM and it otherwise works fine (running multiple Rails dev sites locally). When I run env | grep 'RUBY' I get:
$ env | grep 'RUBY'
MY_RUBY_HOME=/Users/myusername/.rvm/rubies/ruby-2.0.0-p247
RUBY_VERSION=ruby-2.0.0-p247
However, I just continued and ran bundle install, then bundle exec jekyll serve and the site booted up without issue.
Seems like there is no variable RUBY_VERSION in your env.
Try in your shell: env | grep 'RUBY'
Output should be like this:
RUBY_VERSION=ruby-2.3.0
If you don't see anything, you need to reinstall ruby.
Use RVM or other ruby version manager. Here is a good manual
I used to have ruby '~> 2.6.3' in Gemfile and got same warning.
I changed it to ruby '2.6.3' and it fixed it.

Bundler and wrong binstubs?

I run rails s or bundle exec rails s and I get this warning:
Bundler is using a binstub that was created for a different gem.
This is deprecated, in future versions you may need to `bundle binstub rails` to work around a system/bundle conflict.
What does this mean? From looking around the bundler site, my understanding of binstubs is that you can set executables to them, so instead of running bundle exec blabla you can just do bin/blabla. So this error is saying my bundler isn't set to the right binstub?
When I run the bundle binstub rails I get this output
rails has no executables, but you may want one from a gem it depends on.
railties has: rails
bundler has: bundle, bundler
I do not understand what my system is trying to tell me, and it's not breaking anything, but I have a hunch this could turn into a bigger issue if I don't fix it
ruby 2.0.0p247
which ruby
/Users/evan/.rvm/rubies/ruby-2.0.0-p247/bin/ruby
which bundler
/Users/evan/.rvm/gems/ruby-2.0.0-p247/bin/bundler
Rails 4.0.2
Edit:
So, if I run the commands in the nag message:
bundle config --delete bin # Turn off Bundler's stub generator
rake rails:update:bin # Use the new Rails 4 executables
I end up getting uninitialized constant Bundler errors with bundle exec commands and the only way I've found to fix that is to rerun bundle install --binstubs which brings back the nag message at the start of this post.
What worked for me was
rm -rf bin/*
Then open a new terminal session and
bundle exec rake app:update:bin
Solution in my case: - Other solutions didn't work for me.
In your Rails directory:
mv /usr/bin/rails /usr/bin/rails.old
bundle config --delete bin
rm -rf bin
# for rails 4.x:
rake rails:update:bin
# for rails 3.x:
bundle install --binstubs
# if you're using rbenv
rbenv rehash
rm -rf ~/.rbenv/plugins/{rbenv-bundle-exec,rbenv-gemset,bundler}
Also be sure that bin/rails is added to the path like:
PATH=./bin:$PATH
Good luck.
This error may raise when you update your ruby but not the related gems.
To check if this is your case, try to make a new rails app in a new empty directory (to be sure RVM is not autoloading any gemset)
make /tmp/test && cd test && rails new test
If this fails stating that it cannot find a suitable 'rails', then simply run
gem update
and overwrite any conflicting rails.
gem uninstall bundler
gem install bundler
Uninstalling all my versions of Bundler, and then installing the latest version fixed it for me. I had multiple versions of bundler installed, so when I ran bundle exec rails s I think the wrong Bundler was used, giving me the warning message.
You may need to generate new stubs after reinstalling Bundler, but I didn't have to.
I was able to fix this by looking at the commit history for bin/rails using git log -p bin/rails
The current, error producing content is:
#!/usr/bin/env ruby
#
# This file was generated by Bundler.
#
# The application 'rails' is installed as part of a gem, and
# this file is here to facilitate running it.
#
require 'pathname'
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
Pathname.new(__FILE__).realpath)
require 'rubygems'
require 'bundler/setup'
load Gem.bin_path('railties', 'rails')
The original, non-error content was:
#!/usr/bin/env ruby
APP_PATH = File.expand_path('../../config/application', __FILE__)
require_relative '../config/boot'
require 'rails/commands'
When I restored the original bin/rails content, the warning message disappeared. Previous attempts had returned uninitialized constant Bundler errors on all bundle exec commands, but now they work. It's worth noting that the original content appears to be exactly what rails new blabla generates in rails 4.0.x.
Still, I would like to know why the first code block causes issues because it's exactly what bundle install --binstubs generates.
Edit: turns out this solution does not work. Pushed this fix to a heroku staging server and heroku errors on startup: all bin/rails commands throw uninitialized constant Bundler and heroku starts up with bin/rails server ..... so this is a not really a fix.
Edit2:
If I add these two lines to the second block (the original bin/rails content), all bin/rails commands are working again:
require 'rubygems'
require 'bundler/setup'
My guess is that the second line is what fixes the bundler errors I was having.
Interestingly, when I tried to edit the first block of code in this post to try and debug which line was throwing the warning, any change I made caused all rails commands to failure, with no information except for the nag note in the OP. weird.
Final bin/rails that fixed my issue:
#!/usr/bin/env ruby
APP_PATH = File.expand_path('../../config/application', __FILE__)
require_relative '../config/boot'
require 'rubygems'
require 'bundler/setup'
require 'rails/commands'
Any additional insight from people who find this would be welcome!

Using whenever gem with sinatra

#config/scheduler.rb
set :output, 'log/cron.log'
every 1.minutes do
rake 'reports:generate'
end
When I run crontab -l I get the following output:
* * * * * /bin/bash -l -c 'cd <path-to-app> bundle exec rake reports:generate --silent >> log/cron.log 2>&1'
First off, it seems to use my system ruby which is 1.8.7, is there a way to change this?
Secondly, the task is not run, but the only thing that shows up in my cron.log is the following:
Your Ruby version is 1.8.7, but your Gemfile specified 2.0.0
I've tried puts to the log with no success, so I have basically no idea what's going on. It seems the task is being initiated, but then fails without anything being written to the log. I have no idea how to go about debugging this, so please help.
I'm on OS X, and my application is running on Sinatra.
edit: I'm using rbenv to manage my ruby versions, so I guess I have to tell the cron job to load rbenv somehow? When I cd into the app's folder and run ruby -v I get
ruby 2.0.0p353 (2013-11-22 revision 43784) [x86_64-darwin12.5.0]
It seems the problem was with rbenv, and not necessarily sinatra or whenever. This solved my problem:
#schedlue.rb
command "cd #{Dir.pwd} && RACK_ENV=#{environment} rbenv exec bundle exec rake reports:generate"
By adding rbenv exec we successfully loaded the correct ruby version before running the rake task.

How to use Bundler with path specified Ruby version

I am on a VM (Lucid 64b) with a system Ruby version of 1.9.3p0.
I have a Ruby script that creates a .deb file -- The script needs to use Ruby 1.8.7 which I have installed in /foo/ruby/1.8.7.
There is an existing Gemfile to be used with Bundler
I can't use RVM and I can't install gems at the system level.
My .bashrc includes (and has been sourced)
export PATH=$PATH:/foo/ruby/1.8.7/bin
but ruby -v still gives me
ruby 1.9.3p0 (2011-10-30) [x86_64-linux]
Questions
How can I change the Ruby version for my user to use Ruby 1.8.7?
I've run: bundle install --path vendor/bundle
So in that directory (actually ./vendor/bundle/ruby/1.8/cache/gems) are all the gems I need but, when I run the Ruby script it doesn't find the required gems. I run the script like so /foo/ruby/1.8.7 script_to_gen_deb_file.rb
How can I get ruby to see/use the bundled gems?
Update
I was able to solve it. I needed to use
/foo/ruby1.8.7/bundle exec /foo/ruby1.8.7/ruby script_to_gen_deb_file.rb
I had tried this before, but I got an unrelated error and believed there was an environment problem.
Change your path so the special ruby gets precedence?
export PATH=/foo/ruby/1.8.7/bin:$PATH

how do you activate or set the default rake?

I have seen many
You have already activated rake 0.9.x, but your Gemfile requires rake 0.x.x
errors.
Of course, they can be solved (temporarily or always) by some methods like the following.
bundle exec rake
The method above works but you always have to type bundle exec.
It can also be solved by
bundle update
But bundle update also updates your other gems.
Some say it can be solved by
gem uninstall unwanted_rake_version
Yes, the unwanted rake can be installed but it is still marked as activated thus, still giving the error.
One solution would be to explicitly specify the rake version in your Gemfile but, that is not the question. It is on how to set the default rake version, or activate that specific version in rvm or other types of ruby installations?
The newer versions of rake can be activated by supplying an optional first argument, that is the gem version.
$ rake 0.9.2
Alternatively, if you have an older version of rake you can update the rake script manually to include this parameter (or specify any specific version you want).
The rake script usually lives in /usr/bin/rake (or ~/.rvm/gems/ruby-#{ruby-name}/rake if using rvm). And dictates the version of them gem to load before parsing paramaters.
It looks like this on my system.
$ cat ~/.rvm/gems/ruby-1.9.2-p180/bin/rake
#!/home/tomcat/.rvm/rubies/ruby-1.9.2-p180/bin/ruby
#
# This file was generated by RubyGems.
#
# The application 'rake' is installed as part of a gem, and
# this file is here to facilitate running it.
#
require 'rubygems'
version = ">= 0"
if ARGV.first =~ /^_(.*)_$/ and Gem::Version.correct? $1 then
version = $1
ARGV.shift
end
gem 'rake', version
load Gem.bin_path('rake', 'rake', version)
The important bit is gem 'rake', version changing version will force rake to a specific version system/rvm wide.
For more info, Katz' article explains nicely how binaries run under rubygems
When I get that error, its usually a result of working between projects that depend on different versions of rake. An easy fix is
gem uninstall rake
And then in your project directory (assuming you're working with Bundler) simply
bundle
I always uninstall rake first, command like this:
gem uninstall rake -v=version
then install another version
gem install rake -v=version

Resources