Rails 未使用正确版本的 Ruby

发布于 2024-12-10 11:51:02 字数 3117 浏览 0 评论 0原文

新信息

这就是 rails console 发生的情况:

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

这里发生了一些非常奇怪的事情 - 提示显示 1.9.2RUBY_VERSION 显示1.8.7


我正在使用 RVM,但在 Ubuntu 上使用 Rails 时遇到问题。

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

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

但是如果我执行gem env

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

当我运行Rails时,如果我执行puts RUBY_VERSION,它会输出1.8.7

事实上,在irb中,RUBY_VERSION给出1.9.2,但在rails console中它给出1.8。 7.

主要问题是当它到达我的 JSON 样式哈希文字时它会失败。

我尝试了各种卸载 rvm、rails、bundler 等的方法,但似乎没有任何效果。有什么想法吗?

编辑:

这是 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:       ""

gem env 仍然告诉我,在 RubyGems 环境中,Ruby 版本是 1.8.7。我就是不明白。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

梦忆晨望 2024-12-17 11:51:02

您应该像这样设置默认 ruby​​:

rvm use 1.9.2 --default

这将使用 rvm one 覆盖系统安装的 ruby​​。

更新:

notme,谢谢您的评论

默认前面的双连字符对于设置默认 ruby​​ 很重要。

下面列出了详细的&不言自明的例子,享受:

$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 ]

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 ]
じ违心 2024-12-17 11:51:02

最有可能的罪魁祸首:

通过在 shell 中输入 printenv PATH 检查您的 PATH 环境变量 - /usr/bin 可能位于 RVM 安装的位置之前,它是 Rubies - 但在使用 RVM 时您不希望出现这种情况! !

如果 /usr/bin 出现在 RVM 位置之前,则在检查程序时,它将采用 /usr/bin/ruby —— LINUX 附带的 Ruby(很可能是 1.8.7) —— 而不是 RVM 默认的 ruby​​(即 1.9.2)

您可以通过询问您的系统哪个 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

检查您的 PATH 环境变量 - 确保您的 RVM ruby​​ 版本安装的目录存在第一的,在 /usr/bin 之前,

还有其他几个目录)


例如在我的 PATH 中, ~/.rvm/... 首先出现,然后是 /usr/local/bin ,然后是 /usr/bin (当然 之前提到过,rvm --default use 1.9.2,因此您可以在任何新 shell 中获得默认的 Ruby 版本。

看起来您已经在系统范围内的 /usr/local 下安装了 RVM。

恕我直言,升级 gem 时,系统范围内的安装可能会很混乱,并且可能会导致一些问题。

我不建议这样做!我与 Wayne Seguin 进行了更长的电子邮件讨论,甚至他在他的环境中使用了本地用户 RVM。

检查 RVM 文档 - 它说:请注意单用户取代多用户 [...RVM 安装]。

这意味着,如果您在系统范围内安装了 RVM,并且还(不小心?)为用户帐户安装了它,那么对于该用户,本地安装的 RVM 优先。这是一个额外的陷阱,通过首先不在系统范围内安装很容易避免:)

就我个人而言,我会卸载系统范围的 RVM 并仅为相关用户安装它,然后他们可以更多自己轻松安装 gem,而不会干扰其他用户。

最后:在本地安装到用户帐户时,请确保专门为正确的 Ruby 版本安装 gem,并且不要使用 sudo 来安装 gem!

希望这有帮助!
祝你好运!


编辑

如果您从 RUBY_VERSION 获得的输出与 irb 或 Rails 控制台提示符不同,那么 Ruby 的编译/安装方式一定有问题。 这看起来越来越像您需要重新安装 RVM 和 Ruby 版本 1.9.2,但首先检查您的 PATH,以确保这不是罪魁祸首。


健全性检查,检查您的 LINUX 安装是否存在干扰:

创建一个新用户帐户,为该用户执行新的 RVM 安装并安装 Ruby 1.9.2 - 然后尝试它是否在该帐户中工作,或者是否显示同样的问题。

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.

左耳近心 2024-12-17 11:51:02

您的用户目录中有 .rvmrc 文件吗?

$ cat ~/.rvmrc
=> export rvm_path="/home/nick/.rvm"

它应该看起来像那样吗?

还要检查您是否设置了一个 .bashrc 文件,如下所示:

echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"' >> ~/.bashrc 

Do you have a .rvmrc file in your user directory?

$ cat ~/.rvmrc
=> export rvm_path="/home/nick/.rvm"

It should look something like that?

And also check you've set up a .bashrc file like this:

echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"' >> ~/.bashrc 
几味少女 2024-12-17 11:51:02

尝试使用:

rvm --default use 1.9.2

Try to use:

rvm --default use 1.9.2
橘香 2024-12-17 11:51:02

您使用 Passenger 还是 Pow 来运行 Rails 应用程序?如果是这样,您需要将服务器设置为使用 rvm 1.9.2

请参阅此处的第 2.3 节,例如: http ://pow.cx/manual.html#section_2

或乘客:http://blog.phusion.nl/2010/ 09/21/phusion-passenger-running-multiple-ruby-versions/

Are you using Passenger or Pow to run your Rails apps? If so you'll need to set the server to use rvm 1.9.2

See section 2.3 here, for example: http://pow.cx/manual.html#section_2

Or for passenger: http://blog.phusion.nl/2010/09/21/phusion-passenger-running-multiple-ruby-versions/

初心 2024-12-17 11:51:02

我会选择错误的 gem/rails 安装:

gem empty
gem update --system
gem install bundler --no-ri --no-rdoc
gem install rails --no-ri --no-rdoc

I would go for bad gem/rails install:

gem empty
gem update --system
gem install bundler --no-ri --no-rdoc
gem install rails --no-ri --no-rdoc
鸠魁 2024-12-17 11:51:02

确保您没有将 sudo 与 RVM 一起使用。我遇到了这样的问题,我有 2 个不同的位置放置 Ruby 和我的宝石。

Make sure you didn't use sudo with RVM. I ran into an issue like this where I had 2 different locations for Ruby and my gems.

温馨耳语 2024-12-17 11:51:02

PATH 可能是错误的 - 检查 printenv PATH

PATH is probably wrong - check printenv PATH

芸娘子的小脾气 2024-12-17 11:51:02

伙计,我遇到了这个确切的问题,我解决它的方法是重新安装捆绑器。看来捆绑程序正在将 RUBY_VERSION 缓存到 1.8.7。

我没有使用rvm,我必须使用它来设置默认的ruby版本:

sudo update-alternatives --config ruby
sudo update-alternatives --config gem

然后,我重新安装了bundler,它起作用了!

希望你能用这个方法解决这个问题

Man, I was having this exact problem and the way I solved it was by reinstalling bundler. It seems that bundler was caching the RUBY_VERSION to 1.8.7.

I'm not using rvm, I had to use this to set the default ruby version:

sudo update-alternatives --config ruby
sudo update-alternatives --config gem

And then, I reinstalled bundler and it worked!

Hopefully you can solve it with this approach

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文