如何找到gem文件的安装位置


289

我可以找到使用安装的gem gem list,但没有显示宝石的安装位置。

如何找到宝石所在的位置,以及在将宝石安装到哪里之前如何知道?

Answers:


374

用于gem environment了解您的宝石环境:

RubyGems Environment:
  - RUBYGEMS VERSION: 2.1.5
  - RUBY VERSION: 2.0.0 (2013-06-27 patchlevel 247) [x86_64-darwin12.4.0]
  - INSTALLATION DIRECTORY: /Users/ttm/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0
  - RUBY EXECUTABLE: /Users/ttm/.rbenv/versions/2.0.0-p247/bin/ruby
  - EXECUTABLE DIRECTORY: /Users/ttm/.rbenv/versions/2.0.0-p247/bin
  - SPEC CACHE DIRECTORY: /Users/ttm/.gem/specs
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86_64-darwin-12
  - GEM PATHS:
     - /Users/ttm/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0
     - /Users/ttm/.gem/ruby/2.0.0
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - https://rubygems.org/
  - SHELL PATH:
     - /Users/ttm/.rbenv/versions/2.0.0-p247/bin
     - /Users/ttm/.rbenv/libexec
     - /Users/ttm/.rbenv/plugins/ruby-build/bin
     - /Users/ttm/perl5/perlbrew/bin
     - /Users/ttm/perl5/perlbrew/perls/perl-5.18.1/bin
     - /Users/ttm/.pyenv/shims
     - /Users/ttm/.pyenv/bin
     - /Users/ttm/.rbenv/shims
     - /Users/ttm/.rbenv/bin
     - /Users/ttm/bin
     - /usr/local/mysql-5.6.12-osx10.7-x86_64/bin
     - /Users/ttm/libsmi/bin
     - /usr/local/bin
     - /usr/bin
     - /bin
     - /usr/sbin
     - /sbin
     - /usr/local/bin

请注意以下两个部分:

  • INSTALLATION DIRECTORY
  • GEM PATHS

感谢您的信息!在我的环境中,似乎GEM PATHS只使用了第一个。那正确吗?
ironsand

8
gem env简称
zanderwar '17

我会看看EXECUTABLE DIRECTORY
马特·塞特

EXECUTABLE DIRECTORY包含由宝石安装CLI可执行版本,但它不是宝石本身。
铁皮人

145

我发现使用以下命令获取库文件的位置很有用:

gem which *gemname*

6
这是一个非常好的简单方法,不需要捆绑程序(尽管我确实喜欢捆绑程序:-)。
antinome

2
最简单的答案,不需要导轨或捆绑器!好一个!
lacostenycoder

我喜欢这个版本,因为它对进行logstash开发的人很有用(我的服务器上没有捆绑器)。示例很有用:gem which 'logstash/inputs/tcp.rb'
Cameron Kerr

那并不总是行得通的。仅当您的gem在PATH目录中作为可执行文件存在时才有效。
GiriB

74

安装宝石后,如果您想知道特定宝石在哪里。尝试输入:

 gem list

您将能够看到已安装的gem的列表。现在使用bundle show并命名您想知道其路径的宝石,如下所示:

 bundle show <gemName>

2
我不确定这是否适用于所有目录。我运行了此命令并得到了Could not locate Gemfile or .bundle/ directory
Richie Thomas

1
这是最好的方法,因为它显示完整路径,而(gem env)仅显示文件夹的父文件gems夹:/home/test/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0。里面有很多文件夹,您的gem可能位于其中的几乎任何一个文件夹中-这bundle show <gemName>正是准确显示的(而gem env)却没有。
祝贺

49

要完成其他答案,gem-path gem可以找到特定gem的安装路径。

安装:

gem install gem-path

用法:

gem path rails
=> /home/cbliard/.rvm/gems/ruby-2.1.5/gems/rails-4.0.13
gem path rails '< 4'
=> /home/cbliard/.rvm/gems/ruby-2.1.5/gems/rails-3.2.21

这非常方便,因为您可以使用它来grep或编辑文件:

grep -R 'Internal server error' "$(gem path thin)"
subl "$(gem path thin)"

33

您可以通过运行gem help commands然后选择适当的命令,从命令提示符处进行检查:

kirti@kirti-Aspire-5733Z:~$  gem help commands
GEM commands are:

    build                  Build a gem from a gemspec
    cert                   Manage RubyGems certificates and signing settings
    check                  Check a gem repository for added or missing files
    cleanup                Clean up old versions of installed gems in the local
                           repository
    contents               Display the contents of the installed gems
    dependency             Show the dependencies of an installed gem
    environment            Display information about the RubyGems environment
    fetch                  Download a gem and place it in the current directory
    generate_index         Generates the index files for a gem server directory
    help                   Provide help on the 'gem' command
    install                Install a gem into the local repository
    list                   Display gems whose name starts with STRING
    lock                   Generate a lockdown list of gems
    mirror                 Mirror all gem files (requires rubygems-mirror)
    outdated               Display all gems that need updates
    owner                  Manage gem owners on RubyGems.org.
    pristine               Restores installed gems to pristine condition from
                           files located in the gem cache
    push                   Push a gem up to RubyGems.org
    query                  Query gem information in local or remote repositories
    rdoc                   Generates RDoc for pre-installed gems
    regenerate_binstubs    Re run generation of executable wrappers for gems.
    search                 Display all gems whose name contains STRING
    server                 Documentation and gem repository HTTP server
    sources                Manage the sources and cache file RubyGems uses to
                           search for gems
    specification          Display gem specification (in yaml)
    stale                  List gems along with access times
    uninstall              Uninstall gems from the local repository
    unpack                 Unpack an installed gem to the current directory
    update                 Update installed gems to the latest version
    which                  Find the location of a library file you can require
    yank                   Remove a specific gem version release from
                           RubyGems.org

For help on a particular command, use 'gem help COMMAND'.

Commands may be abbreviated, so long as they are unambiguous.
e.g. 'gem i rake' is short for 'gem install rake'.
kirti@kirti-Aspire-5733Z:~$ 

现在从上面我可以看到该命令environment很有用。所以我会做:

kirti@kirti-Aspire-5733Z:~$ gem help environment
Usage: gem environment [arg] [options]


  Common Options:
    -h, --help                       Get help on this command
    -V, --[no-]verbose               Set the verbose level of output
    -q, --quiet                      Silence commands
        --config-file FILE           Use this config file instead of default
        --backtrace                  Show stack backtrace on errors
        --debug                      Turn on Ruby debugging


  Arguments:
    packageversion  display the package version
    gemdir          display the path where gems are installed
    gempath         display path used to search for gems
    version         display the gem format version
    remotesources   display the remote gem servers
    platform        display the supported gem platforms
    <omitted>       display everything

  Summary:
    Display information about the RubyGems environment

  Description:
    The RubyGems environment can be controlled through command line arguments,
    gemrc files, environment variables and built-in defaults.

    Command line argument defaults and some RubyGems defaults can be set in a
    ~/.gemrc file for individual users and a /etc/gemrc for all users. These
    files are YAML files with the following YAML keys:

      :sources: A YAML array of remote gem repositories to install gems from
      :verbose: Verbosity of the gem command. false, true, and :really are the
                levels
      :update_sources: Enable/disable automatic updating of repository metadata
      :backtrace: Print backtrace when RubyGems encounters an error
      :gempath: The paths in which to look for gems
      :disable_default_gem_server: Force specification of gem server host on
    push
      <gem_command>: A string containing arguments for the specified gem command

    Example:

      :verbose: false
      install: --no-wrappers
      update: --no-wrappers
      :disable_default_gem_server: true

    RubyGems' default local repository can be overridden with the GEM_PATH and
    GEM_HOME environment variables. GEM_HOME sets the default repository to
    install into. GEM_PATH allows multiple local repositories to be searched for
    gems.

    If you are behind a proxy server, RubyGems uses the HTTP_PROXY,
    HTTP_PROXY_USER and HTTP_PROXY_PASS environment variables to discover the
    proxy server.

    If you would like to push gems to a private gem server the RUBYGEMS_HOST
    environment variable can be set to the URI for that server.

    If you are packaging RubyGems all of RubyGems' defaults are in
    lib/rubygems/defaults.rb.  You may override these in
    lib/rubygems/defaults/operating_system.rb
kirti@kirti-Aspire-5733Z:~$ 

最后,向您展示您的要求,我会做:

kirti@kirti-Aspire-5733Z:~$ gem environment gemdir
/home/kirti/.rvm/gems/ruby-2.0.0-p0
kirti@kirti-Aspire-5733Z:~$ gem environment gempath
/home/kirti/.rvm/gems/ruby-2.0.0-p0:/home/kirti/.rvm/gems/ruby-2.0.0-p0@global
kirti@kirti-Aspire-5733Z:~$ 

1
谢谢!我不知道gem help commands。我应该写我正在使用rbenv。
ironsand

17

您可以欺骗gem open显示宝石路径:

VISUAL=echo gem open gem-name

例:

VISUAL=echo gem open rails
=> /usr/local/opt/asdf/installs/ruby/2.4.3/lib/ruby/gems/2.4.0/gems/rails-5.1.4

它可以正常工作,并且不需要第三方gem。


2
太棒了!可惜的是,这些有用的信息是副作用,而不是故意的命令。请注意,如果比设置环境变量更容易,还可以使用-e选项(-e echo)指定“编辑器” 。
史蒂夫

美丽!只需省略最后一部分,然后进入编辑器中的该目录并撕开宝石即可;-)
ARK

14

gem env就像gem environment。保存一些打字。

# gem env
RubyGems Environment:
  - RUBYGEMS VERSION: 2.0.14
  - RUBY VERSION: 2.0.0 (2014-02-24 patchlevel 451) [i686-linux]
  - INSTALLATION DIRECTORY: /usr/local/lib/ruby/gems/2.0.0
  - RUBY EXECUTABLE: /usr/local/bin/ruby
  - EXECUTABLE DIRECTORY: /usr/local/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86-linux
  - GEM PATHS:
     - /usr/local/lib/ruby/gems/2.0.0
     - /root/.gem/ruby/2.0.0
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - https://rubygems.org/

它不是“像”一样的命令,它是相同的命令。gem help commands返回“ 命令可以缩写,只要它们是模棱两可的。例如'gem i rake'是'gem install rake'的缩写。 ”换句话说gem environment,,gem env并且gem e所有工作gem environment都是自文档的,对于那些不知道的人它在做什么,因此我们应该将其用于初学者。
锡人

2

gem env名单,其中的宝石可以进行安装,但是这可能是10点或更多的位置。如果您想知道特定gem 安装位置,可以执行:

gem list -d <gemname>

输出示例:

tilt (2.0.9)
    Author: Ryan Tomayko
    Homepage: http://github.com/rtomayko/tilt/
    License: MIT
    Installed at: /opt/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0

    Generic interface to multiple Ruby template engines

0

如果您使用的是rvm工具,则可以运行以下命令以打印gem路径:

rvm gemdir

要么

echo $GEM_HOME

0

这可以正常工作,并为您提供每个gem的安装路径。这在尝试进行多阶段docker构建时非常有用。.您可以在捆绑后安装的特定目录中进行复制。

bash-4.4# gem list -d

输出:

aasm (5.0.6)
    Authors: Thorsten Boettger, Anil Maurya
    Homepage: https://github.com/aasm/aasm
    License: MIT
    Installed at: /usr/local/bundle

  State machine mixin for Ruby objects
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.