Ruby-on-rails 我如何在 Ubuntu 中使用 gems?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/909673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:17:17  来源:igfitidea点击:

How do I use gems with Ubuntu?

ruby-on-railsrubyubunturubygemscapistrano

提问by Arthur

I recently upgraded to Ubuntu 9.04 and I have issues using gems. I installed Ruby, Rubygems and Rails using apt-get. The railscommand does work.

我最近升级到 Ubuntu 9.04,但我在使用 gems 时遇到了问题。我使用 apt-get 安装了 Ruby、Rubygems 和 Rails。该rails命令确实有效。

I then installed capistrano and other gems, such as heroku. In order to do that, I used the command:

然后我安装了 capistrano 和其他 gems,比如 heroku。为了做到这一点,我使用了以下命令:

sudo gem install XXX

When I want to use the capcommand it does not work:

当我想使用该cap命令时,它不起作用:

bash: cap: command not found

It is the same with the other gem commands.

它与其他 gem 命令相同。

Do I have something particular to do so that the gem commands work?

我有什么特别的事情要做以便 gem 命令起作用吗?

回答by Chris McCauley

Where are my Gems?

我的宝石在哪里?

You can find where your gems are stored using the gem environmentcommand. For example:

您可以使用该gem environment命令找到您的 gem 的存储位置。例如:

chris@chris-laptop:~$ gem environment
RubyGems Environment:
  - RUBYGEMS VERSION: 1.3.2

  - RUBY VERSION: 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]
  - INSTALLATION DIRECTORY: /usr/lib/ruby/gems/1.8
  - RUBY EXECUTABLE: /usr/bin/ruby1.8
  - EXECUTABLE DIRECTORY: /usr/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86-linux
  - GEM PATHS:
     - /usr/lib/ruby/gems/1.8
     - /home/chris/.gem/ruby/1.8
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :benchmark => false
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:

If you look at the "GEM PATHS:" section you can see that gems can be stored in two places on my laptop: /usr/lib/ruby/gems/1.8or in the .gemdirectory in my home dir.

如果您查看“GEM PATHS:”部分,您会发现 gem 可以存储在我的笔记本电脑上的两个位置:/usr/lib/ruby/gems/1.8.gem我的主目录中的目录中。

You can also see that executables are stored in EXECUTABLE DIRECTORY which in this case is /usr/bin.

您还可以看到可执行文件存储在 EXECUTABLE DIRECTORY 中,在本例中为/usr/bin.

Because /usr/binis in my path this lets me run cap, merb, railsetc.

因为/usr/bin在我的道路这让我跑capmerbrails等。

Updating your PATH

更新你的路径

If for some reason your EXECUTABLE DIRECTORY isn't on your path (for example if it is /var/lib/gems/1.8/bin) then you need to update your PATH variable.

如果由于某种原因您的可执行目录不在您的路径上(例如,如果它是 /var/lib/gems/1.8/bin),那么您需要更新您的 PATH 变量。

Assuming that you are using the bash shell. You can do this quickly for the current session by typing the following at the shell prompt; let's pretend that you want to add /var/lib/gems/1.8/binto the path:

假设您使用的是 bash shell。您可以通过在 shell 提示符下键入以下内容为当前会话快速执行此操作;让我们假设您要添加/var/lib/gems/1.8/bin到路径中:

export PATH=$PATH:/var/lib/gems/1.8/bin

and press return. That appends the new directory to the end of the current path. Note the colon between $PATHand /var/lib/gems/1.8/bin

并按回车。这会将新目录附加到当前路径的末尾。注意冒号之间$PATH/var/lib/gems/1.8/bin

To set the value for all sessions you will need to edit either your .profileor .bashrcfile and add the same line to the end of the file. I usually edit my .bashrcfile for no reason other than that's what I've always done. When finished, save the file and then refresh your environment by typing:

要为所有会话设置值,您需要编辑您的.profile.bashrc文件并将同一行添加到文件末尾。我通常.bashrc无缘无故地编辑我的文件,这就是我一直在做的事情。完成后,保存文件,然后键入以下内容刷新您的环境:

bash

at the shell prompt. That will cause the .bashrcto get reread.

在 shell 提示符下。这将导致.bashrc重新阅读。

At any point you can check the current value of $PATHby typing

在任何时候,您都可以$PATH通过键入来检查的当前值

echo $PATH

at the shell prompt.

在 shell 提示符下。

Here's a sample from one of my own servers, where my username is "chris" and the machine name is "chris-laptop":

这是来自我自己的一台服务器的示例,其中我的用户名是“chris”,机器名称是“chris-laptop”:

chris@chris-laptop:~$ 
chris@chris-laptop:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
chris@chris-laptop:~$ 
chris@chris-laptop:~$ export PATH=$PATH:/var/lib/gems/1.8/bin
chris@chris-laptop:~$ 
chris@chris-laptop:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/var/lib/gems/1.8/bin
chris@chris-laptop:~$ 

My Gem won't load!

我的 Gem 无法加载!

"Ruby gems won't load even though installed" highlights a common problem using multiple different versions of Ruby; Sometimes the Gem environment and Gem path get out of sync:

即使安装了 Ruby gems 也不会加载”强调了使用多个不同版本的 Ruby 的常见问题;有时 Gem 环境和 Gem 路径不同步:

rb(main):003:0> Gem.path

=> ["/opt/ruby1.9/lib/ruby1.9/gems/1.9.1"]
irb(main):004:0> exit

Any Ruby process here is looking only in one place for its Gems.

这里的任何 Ruby 进程都只在一个地方寻找它的 Gems。

:~/$ gem env
RubyGems Environment:
  - RUBYGEMS VERSION: 1.3.7
  - RUBY VERSION: 1.9.1 (2009-05-12 patchlevel 129) [x86_64-linux]
  - INSTALLATION DIRECTORY: /opt/ruby1.9/lib/ruby/gems/1.9.1
  - RUBY EXECUTABLE: /opt/ruby1.9/bin/ruby1.9
  - EXECUTABLE DIRECTORY: /opt/ruby1.9/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86_64-linux
  - GEM PATHS:
     - /opt/ruby1.9/lib/ruby/gems/1.9.1
     - /home/mark/.gem/ruby/1.9.1
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :benchmark => false
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - http://rubygems.org/

Look carefully at the output of gem environment:

仔细看gem环境的输出:

  - GEM PATHS:
     - /opt/ruby1.9/lib/ruby/gems/1.9.1

This isn't the same path as returned by Gem.path:

这与 Gem.path 返回的路径不同:

["/opt/ruby1.9/lib/ruby1.9/gems/1.9.1"]

It's hard to say what exactly caused lib/rubyto change to lib/ruby1.9but most likely the developer was working with multiple Ruby versions. A quick mvor lnwill solve the problem.

很难说到底是什么导致lib/ruby了更改,lib/ruby1.9但很可能开发人员正在使用多个 Ruby 版本。一个快速mvln将解决问题。

If you do need to work with multiple Ruby versions then you really should be using rvm.

如果您确实需要使用多个 Ruby 版本,那么您真的应该使用rvm

回答by Adam Greenhall

As noted by @Chris you need to add the gems environment to your path. You can do this by:

正如@Chris 所指出的,您需要将 gems 环境添加到您的路径中。您可以通过以下方式执行此操作:

echo 'export PATH=$PATH:/var/lib/gems/1.8/bin' | tee --append ~/.bashrc

回答by victor hugo

The folder in which gems are storedmust be on your PATH, for example mine is:

存储宝石的文件夹必须在您的 PATH 上,例如我的是:

/home/victor/.gem/ruby/1.8/bin

/home/victor/.gem/ruby/1.8/bin

Check your path by typing

通过键入检查您的路径

echo $PATH

回答by victor hugo

It seens that when installing rubygems, now in ubuntu 9.04, I have this problem. I noticed that in "gem environment" the executable directory is "/var/lib/gems/1.8/bin", instead of "/usr/bin"... This is a problem with rubygems or with ubuntu 9.04??

它看到在安装rubygems时,现在在ubuntu 9.04中,我遇到了这个问题。我注意到在“gem 环境”中,可执行目录是“/var/lib/gems/1.8/bin”,而不是“/usr/bin”……这是 ruby​​gems 或 ubuntu 9.04 的问题??

The solution that I encountered is to add "/var/lib/gems/1.8/bin" to my $PATH doing this: export PATH=$PATH:/var/lib/gems/1.8/bin But it don't is saved... how can I save my path?

我遇到的解决方案是将“/var/lib/gems/1.8/bin”添加到我的 $PATH 这样做: export PATH=$PATH:/var/lib/gems/1.8/bin 但它没有被保存...我怎样才能保存我的路径?

Thanks...

谢谢...

Resolvi: coloquei o export PATH=$PATH:/var/lib/gems/1.8/bin no ~/.bashrc! =]

解析:coloquei o export PATH=$PATH:/var/lib/gems/1.8/bin no ~/.bashrc! =]

回答by Sohan

mkmf is part of the ruby1.9.1-dev package. This package contains the header files needed for extension libraries for Ruby 1.9.1. You need to install the ruby1.9.1-dev package by doing:

mkmf 是 ruby​​1.9.1-dev 包的一部分。此包包含 Ruby 1.9.1 扩展库所需的头文件。您需要通过执行以下操作来安装 ruby​​1.9.1-dev 包:

sudo apt-get install ruby1.9.1-dev

须藤 apt-get 安装 ruby​​1.9.1-dev