Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails not using correct version of Ruby

New information

This is what happens with rails console:

Loading development environment (Rails 3.1.1)
ruby-1.9.2-p290 :001 > RUBY_VERSION
 => "1.8.7" 

There's something really weird going on here - the prompt says 1.9.2 but RUBY_VERSION shows 1.8.7.


I'm using RVM, but I'm having problems with Rails on Ubuntu.

$ which ruby
/home/nick/.rvm/rubies/ruby-1.9.2-p290/bin/ruby

$rvm list
rvm rubies
=> ruby-1.9.2-p290 [ i686 ]

But if I do gem env I get

RubyGems Environment:
  - RUBYGEMS VERSION: 1.8.11
  - RUBY VERSION: 1.8.7 (2011-06-30 patchlevel 352) [i686-linux]
  - INSTALLATION DIRECTORY: /home/nick/.rvm/gems/ruby-1.9.2-p290
  - RUBY EXECUTABLE: /usr/bin/ruby1.8
  - EXECUTABLE DIRECTORY: /home/nick/.rvm/gems/ruby-1.9.2-p290/bin

and when I run Rails, if I do puts RUBY_VERSION it outputs 1.8.7.

In fact, in irb, RUBY_VERSION gives 1.9.2 but in rails console it gives 1.8.7.

The main problem is that it's failing when it gets to my JSON-style hash-literals.

I've tried various approaches of uninstalling rvm, rails, bundler, etc. but nothing seems to work. Any ideas?

EDIT:

Here is the output of rvm info:

ruby-1.9.2-p290:

  system:
    uname:       "Linux ubie 3.0.0-12-generic #20-Ubuntu SMP Fri Oct 7 14:50:42 UTC 2011 i686 i686 i386 GNU/Linux"
    bash:        "/bin/bash => GNU bash, version 4.2.10(1)-release (i686-pc-linux-gnu)"
    zsh:         " => not installed"

  rvm:
    version:      "rvm 1.8.6 by Wayne E. Seguin ([email protected]) [https://rvm.beginrescueend.com/]"

  ruby:
    interpreter:  "ruby"
    version:      "1.9.2p290"
    date:         "2011-07-09"
    platform:     "i686-linux"
    patchlevel:   "2011-07-09 revision 32553"
    full_version: "ruby 1.9.2p290 (2011-07-09 revision 32553) [i686-linux]"

  homes:
    gem:          "/home/nick/.rvm/gems/ruby-1.9.2-p290"
    ruby:         "/home/nick/.rvm/rubies/ruby-1.9.2-p290"

  binaries:
    ruby:         "/home/nick/.rvm/rubies/ruby-1.9.2-p290/bin/ruby"
    irb:          "/home/nick/.rvm/rubies/ruby-1.9.2-p290/bin/irb"
    gem:          "/home/nick/.rvm/rubies/ruby-1.9.2-p290/bin/gem"
    rake:         "/home/nick/.rvm/gems/ruby-1.9.2-p290/bin/rake"

  environment:
    PATH:         "/home/nick/.rvm/gems/ruby-1.9.2-p290/bin:/home/nick/.rvm/gems/ruby-1.9.2-p290@global/bin:/home/nick/.rvm/rubies/ruby-1.9.2-p290/bin:/home/nick/.rvm/bin:/home/nick/local/node/bin:/home/nick/bin:/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
    GEM_HOME:     "/home/nick/.rvm/gems/ruby-1.9.2-p290"
    GEM_PATH:     "/home/nick/.rvm/gems/ruby-1.9.2-p290:/home/nick/.rvm/gems/ruby-1.9.2-p290@global"
    MY_RUBY_HOME: "/home/nick/.rvm/rubies/ruby-1.9.2-p290"
    IRBRC:        "/home/nick/.rvm/rubies/ruby-1.9.2-p290/.irbrc"
    RUBYOPT:      ""
    gemset:       ""

but still gem env tells me that in the RubyGems Environment the Ruby version is 1.8.7. I just don't get it.

like image 956
Skilldrick Avatar asked Oct 15 '11 10:10

Skilldrick


People also ask

How do I use a different version of rails?

Every Rails project has the version of Rails in the Gemfile that it will use separately from every other app. There's nothing you need to do to manage two different Rails versions. The only thing you'll want to do is gem install rails to get the latest version and create your new project with rails new myapp .

What does Ruby version do?

RVM (“Ruby Version Manager”) RVM allows you to install and manage multiple installations of Ruby on your system. It can also manage different gemsets. It is available for macOS, Linux, or other UNIX-like operating systems.


2 Answers

You should set default ruby like:

rvm use 1.9.2 --default

This will override system installed ruby with rvm one.

Updated:

notme, thank you for remark

Leading double hyphen before default is significant to set default ruby.

Below listed detailed & self-explained example, enjoy:

$ruby -v
ruby 1.8.7 (2008-08-11 patchlevel 72) [i686-linux]

$rvm list
rvm rubies

   ruby-1.9.2-p290 [ i386 ]

$rvm use 1.9.2 --default
Using /usr/local/rvm/gems/ruby-1.9.2-p290

$rvm list default

Default Ruby (for new shells)

   ruby-1.9.2-p290 [ i386 ]

$rvm use default
Using /usr/local/rvm/gems/ruby-1.9.2-p290

$ruby -v
ruby 1.9.2p290 (2011-07-09 revision 32553) [i686-linux]

$rvm list 

rvm rubies

=> ruby-1.9.2-p290 [ i386 ]
like image 172
sarvavijJana Avatar answered Oct 08 '22 00:10

sarvavijJana


Most likely Culprit:

Check your PATH environment variable by typing printenv PATH in the shell -- /usr/bin might come before wherever RVM installs it's Rubies -- but you don't want that when using RVM!!

If /usr/bin comes before the RVM-location, when checking for programs, it will take /usr/bin/ruby -- the Ruby which came with your LINUX (most likely 1.8.7) -- not the RVM default ruby (which is 1.9.2)

You can double check this like this by asking your system which ruby:

which ruby                           # this should be an RVM path! , not /usr/bin
ruby --version                       # you want this to be 1.9.2
/usr/bin/ruby --version              # the old system default Ruby is most likely 1.8.7

~/.rvm/rubies/ruby-1.9.2-p0/bin/ruby
~/.rvm/rubies/default/bin/ruby --version

Check your PATH environment variable -- make sure that the directory where your RVM ruby version is installed comes FIRST, before /usr/bin

e.g. in my PATH, the ~/.rvm/... comes first, then /usr/local/bin, then /usr/bin (with a couple of other dirs of course)


as mentioned before, rvm --default use 1.9.2 , so you get the default Ruby version in any new shell.

Looks like you have installed RVM system-wide under /usr/local.

IMHO system-wide installation can be a mess when upgrading gems, and it can cause a couple of problems going forward.

I would not recommend doing that! I had a longer email discussion with Wayne Seguin, and even he uses local user RVM for his environments.

Check the RVM docu - it says there: Please note that Single-User supercedes Multi-User [...RVM installation].

That means that if you have installed RVM system-wide, and you also (accidentially?) installed it for a user account, then for that user, the locally installed RVM takes precedence. This is an extra pit-fall which is easy to avoid by not installing system-wide in the first place :)

Personally, I would uninstall the system-wide RVM and install it only for the relevant user(s), who can then more easily install their gems themselves without interfering with other users.

Finally: when installing local to the user account, make sure that you install your gems specifically for the correct Ruby version, and that you do not use sudo to install the gems!

hopes this helps! good luck!


EDIT

If you're getting different output from RUBY_VERSION than your irb or rails console prompt, then there must be something amiss with the way Ruby was compiled / installed. This looks more and more like you need to re-install RVM and your Ruby version 1.9.2, but first check your PATH, to make sure that's not the culprit.


A Sanity Check, to check if there is interference from your LINUX installation:

create a new user account, do a new RVM install for that user and install Ruby 1.9.2 -- then try if it works in that account, or if it shows the same problems.

like image 26
Tilo Avatar answered Oct 07 '22 23:10

Tilo