2

I've got a Rakefile with the following contents:

namespace :geo_data do
  desc 'Imports geo data to Heroku'
  task :heroku_import => :environment do
    system "heroku pg:psql DATABASE -c 'DELETE FROM <table name>' -a <heroku app name>"

    # some more stuff...
  end
end

Whenever I run rake geo_data:heroku_import, it'll crash on my Heroku command with this error:

Your Ruby version is 1.9.3, but your Gemfile specified 2.2.2

However, if I run the Heroku command - heroku pg:psql DATABASE -c 'DELETE FROM <table name>' -a <heroku app name> - directly from the command line in a terminal window, it works fine.

So it would appear that the system command is using a different version of Ruby.

Interestingly, as far as I can tell, I don't even have 1.9.3 installed on my machine. This is the output from rvm list:

rvm rubies

   ruby-2.0.0-p598 [ x86_64 ]
   ruby-2.2.0 [ x86_64 ]
=* ruby-2.2.2 [ x86_64 ]

# => - current
# =* - current && default
#  * - default

Based on that output, I don't have 1.9.3 installed and 2.2.2 is set as my current and default Ruby version.

Am I correct in determining that system is using a different version of Ruby, or is there something else I'm missing? And if it is using a different version, how can I make it use 2.2.2 (or preferably whatever my default is currently set to)?


UPDATE

This is the contents of my Gemfile:

source 'https://rubygems.org'

ruby '2.2.2'

gem 'rails', '4.2.3'
gem 'rails-api'
gem 'spring', :group => :development
gem 'pg'
gem 'stripe'
gem 'jsonapi-resources', '0.7.0'
gem 'rails_api_auth'
gem 'rack-cors', require: 'rack/cors'
gem 'activerecord-postgis-adapter'
gem 'rgeo-geojson'
gem 'geocoder'
gem 'activerecord-import'
gem 'sidekiq'
gem 'unicorn'

group :development, :test do
  gem 'rspec-rails'
  gem 'forgery'
  gem 'foreman'
end

group :development do
  gem 'pry-rails'
end
Community
  • 1
  • 1
jeffdill2
  • 3,968
  • 2
  • 30
  • 47
  • I think that error is coming from Heroku, not your local machine. Try replacing the system command with `heroku run "ruby -v"` to verify. See here for more troubleshooting: https://devcenter.heroku.com/articles/ruby-versions#troubleshooting. Besides that I would try the suggestion from @nick-aschenbach below. – lps Dec 29 '15 at 17:55
  • I don't think it's a Heroku issue. If it was, then running the command by itself from the command line (without using `system`) would produce the same results. But running it from the command line works fine. `heroku run ruby -v -a ` correctly returns `ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-linux]`. – jeffdill2 Dec 29 '15 at 19:44

3 Answers3

2

There a few environment variables that Ruby will check when it is launched that you can use to control things like the load path and to set options.

Bundler is “infectious”, in that when you use it it adds some of these environment variables so that another Ruby process executed as a child will automatically try to use Bundler with the same Gemfile.

Compare the output of ruby -e "puts ENV.inspect" with bundle exec ruby -e "puts ENV.inspect" inside a project using Bundler.

This is normally what you want to happen, so that you are always using the versions of Gems and Ruby defined in your Gemfile.

The heroku command includes its own installation of Ruby (it’s in /usr/local/heroku/ruby on my machine) which it uses rather than your normal version. This is quite an old version of Ruby, mine is 1.9.3p194.

So when you use system to run the heroku command, the Ruby process that is created checks the environment variables and loads Bundler (using the Gem from the version of Ruby you’re using) and then tries to setup everything according to your Gemfile. This is where the Ruby version mismatch happens.

To fix this, you need to tell Bundler not to include the various environment variables when launching a subprocess. There are a few methods that deal with this, in your case you want to use Bundler.clean_system instead of plain system:

Bundler.clean_system "heroku pg:psql DATABASE -c 'DELETE FROM <table name>' -a <heroku app name>"

This will allow the Heroku client to run without interference from Bundler and your Gemfile.

matt
  • 78,533
  • 8
  • 163
  • 197
  • Since this is *vaguely* related, I don't suppose you'd be able to lend any insight to this [other question](http://stackoverflow.com/questions/34700261/how-to-enable-quiet-mode-for-postgres-commands-on-heroku) I have? :-) – jeffdill2 Jan 20 '16 at 01:34
0

Try:

bundle exec rake <command>

This runs the rake command in the context of the current bundle.

More information is available here:

What does bundle exec rake mean?

Community
  • 1
  • 1
  • Unfortunately, I'm getting the same error when using `bundle exec rake geo_data:heroku_import`. – jeffdill2 Dec 29 '15 at 19:20
  • It's possible that RVM's ruby isn't being found in the path or isn't the first ruby in the path. When you try: `which ruby` do you see something like `~/.rvm/rubies/ruby-2.2.0/bin/ruby`? – Nick Aschenbach Dec 29 '15 at 20:23
  • `which ruby` is outputting `/Users/jeffreydill/.rvm/rubies/ruby-2.2.2/bin/ruby`. – jeffdill2 Dec 29 '15 at 20:28
  • Does `which rake` return `/Users/jeffreydill/.rvm/rubies/ruby-2.2.2/bin/rake`? – Nick Aschenbach Dec 29 '15 at 20:52
  • Did you create the RVM ruby gemset for your project? I almost always create `.ruby-version` with text of `ruby-2.2.2` and `.ruby-gemset` with text of `project-name`. cd out of the directory and back in should create the appropriate gemset: `cd stack_model/ ruby-2.2.0 - #gemset created /Users/troy/.rvm/gems/ruby-2.2.0@stack-test ruby-2.2.0 - #generating stack-test wrappers..........` – Nick Aschenbach Dec 29 '15 at 21:00
  • `which rake` is outputting `/Users/jeffreydill/.rvm/gems/ruby-2.2.2/bin/rake`. – jeffdill2 Dec 29 '15 at 22:17
  • And I've now created the gemset for my project. This is the output I got: `ruby-2.2.2 - #gemset created /Users/jeffreydill/.rvm/gems/ruby-2.2.2@api ruby-2.2.2 - #generating api wrappers - please wait`. After that, I reinstalled bundler - `gem install bundle`. And then ran `bundle install`. Still no luck. :-( – jeffdill2 Dec 29 '15 at 23:10
  • Try `gem install rake`. What gems are in your Gemfile? – Nick Aschenbach Dec 29 '15 at 23:14
  • I did `gem install rake`. Still no luck. I'll add the gems in my Gemfile to my initial question. – jeffdill2 Dec 29 '15 at 23:36
0

Take a look here How to fix "Your Ruby version is 1.9.3, but your Gemfile specified 2.0.0".

Try ruby -v

I've solved this by running rvm install 2.2.2.

Community
  • 1
  • 1
rjmolesa
  • 11
  • 2
  • Thanks but I've actually already got `2.2.2` installed. It's also set as my current and default. `ruby -v` correctly gives me `ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-darwin14]` – jeffdill2 Dec 29 '15 at 19:31