如何在 Ubuntu 中使用 gem? [英] How do I use gems with Ubuntu?
问题描述
我最近升级到 Ubuntu 9.04,但在使用 gems 时遇到了问题.我使用 apt-get 安装了 Ruby、Rubygems 和 Rails.rails
命令确实有效.
然后我安装了 capistrano 和其他 gem,例如 heroku.为此,我使用了以下命令:
sudo gem install XXX
当我想使用 cap
命令时它不起作用:
bash: cap: command not found
其他gem命令也一样.
为了使 gem 命令起作用,我有什么特别的事情要做吗?
我的宝石在哪里?
您可以使用 gem environment
命令找到 gem 的存储位置.例如:
chris@chris-laptop:~$ gem 环境RubyGems 环境:- 红宝石版本:1.3.2- 红宝石版本:1.8.7(2008-08-11 补丁级别 72)[i486-linux]- 安装目录:/usr/lib/ruby/gems/1.8- 红宝石可执行文件:/usr/bin/ruby1.8- 可执行目录:/usr/bin- 红宝石平台:- 红宝石- x86-linux- 宝石路径:-/usr/lib/ruby/gems/1.8-/home/chris/.gem/ruby/1.8- 宝石配置:- :update_sources =>真的-:详细=>真的-:基准=>错误的-:回溯=>错误的- :bulk_threshold =>1000- 远程资源:
如果您查看GEM PATHS:"部分,您会发现 gem 可以存储在我笔记本电脑上的两个位置:/usr/lib/ruby/gems/1.8
或 .gem
我的主目录中的目录.
您还可以看到可执行文件存储在可执行目录中,在本例中为 /usr/bin
.
因为 /usr/bin
在我的路径中,这让我可以运行 cap
、merb
、rails
等.
更新你的路径
如果由于某种原因您的可执行目录不在您的路径上(例如,如果它是/var/lib/gems/1.8/bin),那么您需要更新您的 PATH 变量.
假设您使用的是 bash shell.您可以通过在 shell 提示符下键入以下内容为当前会话快速执行此操作;假设您想将 /var/lib/gems/1.8/bin
添加到路径中:
导出 PATH=$PATH:/var/lib/gems/1.8/bin
然后按回车键.这会将新目录附加到当前路径的末尾.注意 $PATH
和 /var/lib/gems/1.8/bin
要为所有会话设置值,您需要编辑 .profile
或 .bashrc
文件并将同一行添加到文件末尾.我通常无缘无故地编辑我的 .bashrc
文件,这就是我一直在做的事情.完成后,保存文件,然后通过键入以下内容刷新您的环境:
<代码>bash
在 shell 提示符下.这将导致 .bashrc
被重新读取.
在任何时候,您都可以通过键入
来检查$PATH
的当前值回显 $PATH
在 shell 提示符下.
这是来自我自己的一台服务器的示例,其中我的用户名是chris",机器名称是chris-laptop":
chris@chris-laptop:~$chris@chris-laptop:~$ echo $PATH/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/gameschris@chris-laptop:~$chris@chris-laptop:~$ export PATH=$PATH:/var/lib/gems/1.8/binchris@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/binchris@chris-laptop:~$
我的宝石无法加载!
"即使安装了Ruby gems也不会加载" 突出显示使用多个不同版本的 Ruby 的常见问题;有时 Gem 环境和 Gem 路径会不同步:
rb(main):003:0>宝石路径=>[/opt/ruby1.9/lib/ruby1.9/gems/1.9.1"]irb(主):004:0>出口
这里的任何 Ruby 进程都只在一个地方寻找它的 Gems.
:~/$ gem 环境RubyGems 环境:- 红宝石版本:1.3.7- 红宝石版本:1.9.1(2009-05-12 补丁级别 129)[x86_64-linux]- 安装目录:/opt/ruby1.9/lib/ruby/gems/1.9.1- 红宝石可执行文件:/opt/ruby1.9/bin/ruby1.9- 可执行目录:/opt/ruby1.9/bin- 红宝石平台:- 红宝石- x86_64-linux- 宝石路径:-/opt/ruby1.9/lib/ruby/gems/1.9.1-/home/mark/.gem/ruby/1.9.1- 宝石配置:- :update_sources =>真的-:详细=>真的-:基准=>错误的-:回溯=>错误的- :bulk_threshold =>1000- 远程资源:- http://rubygems.org/
仔细看gem环境的输出:
- GEM 路径:-/opt/ruby1.9/lib/ruby/gems/1.9.1
这与 Gem.path 返回的路径不同:
["/opt/ruby1.9/lib/ruby1.9/gems/1.9.1"]
很难说究竟是什么原因导致 lib/ruby
更改为 lib/ruby1.9
,但很可能开发人员正在使用多个 Ruby 版本.一个快速的 mv
或 ln
将解决问题.
如果您确实需要使用多个 Ruby 版本,那么您确实应该使用 rvm.p>
I recently upgraded to Ubuntu 9.04 and I have issues using gems.
I installed Ruby, Rubygems and Rails using apt-get.
The rails
command does work.
I then installed capistrano and other gems, such as heroku. In order to do that, I used the command:
sudo gem install XXX
When I want to use the cap
command it does not work:
bash: cap: command not found
It is the same with the other gem commands.
Do I have something particular to do so that the gem commands work?
Where are my Gems?
You can find where your gems are stored using the gem environment
command. For example:
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.8
or in the .gem
directory in my home dir.
You can also see that executables are stored in EXECUTABLE DIRECTORY which in this case is /usr/bin
.
Because /usr/bin
is in my path this lets me run cap
, merb
, rails
etc.
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.
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/bin
to the path:
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 $PATH
and /var/lib/gems/1.8/bin
To set the value for all sessions you will need to edit either your .profile
or .bashrc
file and add the same line to the end of the file. I usually edit my .bashrc
file for no reason other than that's what I've always done. When finished, save the file and then refresh your environment by typing:
bash
at the shell prompt. That will cause the .bashrc
to get reread.
At any point you can check the current value of $PATH
by typing
echo $PATH
at the shell prompt.
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:~$ 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!
"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:
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.
:~/$ 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 PATHS:
- /opt/ruby1.9/lib/ruby/gems/1.9.1
This isn't the same path as returned by Gem.path:
["/opt/ruby1.9/lib/ruby1.9/gems/1.9.1"]
It's hard to say what exactly caused lib/ruby
to change to lib/ruby1.9
but most likely the developer was working with multiple Ruby versions. A quick mv
or ln
will solve the problem.
If you do need to work with multiple Ruby versions then you really should be using rvm.
这篇关于如何在 Ubuntu 中使用 gem?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!