如何使用rbenv为不同的Ruby版本保留不同的gem副本

pxyaymoc  于 2023-10-18  发布在  Ruby
关注(0)|答案(2)|浏览(137)

首先,我安装了Ruby 2.6.3的Fluentd。

$ rbenv versions
* system (set by /Users/jing/.rbenv/version)
  2.7.5
$ gem
gem env
RubyGems Environment:
  - RUBYGEMS VERSION: 3.0.3
  - RUBY VERSION: 2.6.3 (2019-04-16 patchlevel 62) [universal.x86_64-darwin19]
  - INSTALLATION DIRECTORY: /Users/jing/.gem
  - USER INSTALLATION DIRECTORY: /Users/jing/.gem/ruby/2.6.0
  - RUBY EXECUTABLE: /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/bin/ruby
  - GIT EXECUTABLE: /usr/bin/git
  - EXECUTABLE DIRECTORY: /Users/jing/.gem/bin
  - SPEC CACHE DIRECTORY: /Users/jing/.gem/specs
  - SYSTEM CONFIGURATION DIRECTORY: /Library/Ruby/Site
  - RUBYGEMS PLATFORMS:
    - ruby
    - universal-darwin-19
  - GEM PATHS:
     - /Users/jing/.gem
     - /Users/jing/.gem/ruby/2.6.0
     - /Library/Ruby/Gems/2.6.0
     - /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/gems/2.6.0
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - https://rubygems.org/
  - SHELL PATH:
     - /usr/local/Cellar/rbenv/1.2.0/libexec
     - /Users/jing/.rbenv/shims
     - /Users/jing/.tiup/bin
     - /Users/jing/opt/anaconda3/condabin
     - /usr/local/sbin
     - /Users/jing/code/github/zk-code/scripts
     - /Users/jing/tools/apache-zookeeper-3.5.5-bin/bin
     - /Users/jing/code/github/trace/sky/skywalking-cli/bin
     - /Users/jing/tools/apache-skywalking-apm-bin-es7/bin
     - /usr/local/opt/ruby/bin
     - /Users/jing/tools/apache-maven-3.6.3/bin
     - /Users/jing/.gem/bin
     - /Users/jing/.cargo/bin
     - /Users/jing/code/github/rtc/depot_tools
     - /Users/jing/.gem/ruby/2.3.0/bin
     - /usr/local/opt/coreutils/libexec/gnubin
     - /Users/jing/tools/apache-skywalking-apm-bin-es7/bin
     - /Users/jing/tools/confluent-6.1.0/bin
     - /usr/local/opt/findutils/libexec/gnubin
     - /usr/local/opt/gnu-sed/libexec/gnubin
     - /Users/jing/tools/mongodb-osx-x86_64-4.0.2/bin
     - /usr/local/opt/[email protected]/bin
     - /Users/jing/tools/spark-2.2.0-bin-hadoop2.7/bin
     - /Users/jing/bin
     - /usr/local/bin
     - /usr/local/bin
     - /usr/bin
     - /bin
     - /usr/sbin
     - /sbin
     - /Library/TeX/texbin
     - /Library/Apple/usr/bin
     - /Applications/Wireshark.app/Contents/MacOS
     - /Users/jing/go/bin
     - /Users/jing/.pub-cache/bin
     - /Applications/Visual Studio Code.app/Contents/Resources/app/bin
     - /usr/local/opt/fzf/bin
$ gem install fluentd
Successfully installed fluentd-1.14.3
Parsing documentation for fluentd-1.14.3
Done installing documentation for fluentd after 3 seconds
1 gem installed
$ ls ~/.gem/gems -lrt | tail -n1
drwxr-xr-x 28 jing staff 896 Dec  7 10:33 fluentd-1.14.3
$ head -n1 ~/.gem/bin/fluentd
#!/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/bin/ruby

然后我选择了Ruby 2.7.5 with rbenv,并再次安装了fluentd。

$ rbenv shell 2.7.5
$ gem env
RubyGems Environment:
  - RUBYGEMS VERSION: 3.1.6
  - RUBY VERSION: 2.7.5 (2021-11-24 patchlevel 203) [x86_64-darwin19]
  - INSTALLATION DIRECTORY: /Users/jing/.gem
  - USER INSTALLATION DIRECTORY: /Users/jing/.gem/ruby/2.7.0
  - RUBY EXECUTABLE: /Users/jing/.rbenv/versions/2.7.5/bin/ruby
  - GIT EXECUTABLE: /usr/bin/git
  - EXECUTABLE DIRECTORY: /Users/jing/.gem/bin
  - SPEC CACHE DIRECTORY: /Users/jing/.gem/specs
  - SYSTEM CONFIGURATION DIRECTORY: /Users/jing/.rbenv/versions/2.7.5/etc
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86_64-darwin-19
  - GEM PATHS:
     - /Users/jing/.gem
     - /Users/jing/.gem/ruby/2.7.0
     - /Users/jing/.rbenv/versions/2.7.5/lib/ruby/gems/2.7.0
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - https://rubygems.org/
  - SHELL PATH:
     - /Users/jing/.rbenv/versions/2.7.5/bin
     - /usr/local/Cellar/rbenv/1.2.0/libexec
     - /Users/jing/.rbenv/shims
     - /Users/jing/.tiup/bin
     - /Users/jing/opt/anaconda3/condabin
     - /usr/local/sbin
     - /Users/jing/code/github/zk-code/scripts
     - /Users/jing/tools/apache-zookeeper-3.5.5-bin/bin
     - /Users/jing/code/github/trace/sky/skywalking-cli/bin
     - /Users/jing/tools/apache-skywalking-apm-bin-es7/bin
     - /usr/local/opt/ruby/bin
     - /Users/jing/tools/apache-maven-3.6.3/bin
     - /Users/jing/.gem/bin
     - /Users/jing/.cargo/bin
     - /Users/jing/code/github/rtc/depot_tools
     - /Users/jing/.gem/ruby/2.3.0/bin
     - /usr/local/opt/coreutils/libexec/gnubin
     - /Users/jing/tools/apache-skywalking-apm-bin-es7/bin
     - /Users/jing/tools/confluent-6.1.0/bin
     - /usr/local/opt/findutils/libexec/gnubin
     - /usr/local/opt/gnu-sed/libexec/gnubin
     - /Users/jing/tools/mongodb-osx-x86_64-4.0.2/bin
     - /usr/local/opt/[email protected]/bin
     - /Users/jing/tools/spark-2.2.0-bin-hadoop2.7/bin
     - /Users/jing/bin
     - /usr/local/bin
     - /usr/local/bin
     - /usr/bin
     - /bin
     - /usr/sbin
     - /sbin
     - /Library/TeX/texbin
     - /Library/Apple/usr/bin
     - /Applications/Wireshark.app/Contents/MacOS
     - /Users/jing/go/bin
     - /Users/jing/.pub-cache/bin
     - /Applications/Visual Studio Code.app/Contents/Resources/app/bin
     - /usr/local/opt/fzf/binio Code.app/Contents/Resources/app/bin
     - /usr/local/opt/fzf/bin
$ gem install fluentd
Successfully installed fluentd-1.14.3
Parsing documentation for fluentd-1.14.3
Done installing documentation for fluentd after 3 seconds
1 gem installed
$ ls ~/.gem/gems -lrt | tail -n1
drwxr-xr-x 28 jing staff 896 Dec  7 10:37 fluentd-1.14.3
$ head -n1 ~/.gem/bin/fluentd
#!/Users/jing/.rbenv/versions/2.7.5/bin/ruby

从上面的控制台会话中,我知道fluent被重新安装在同一位置。
我不想让一个Ruby版本的gem覆盖另一个Ruby版本的gem。有没有办法为一个Ruby版本单独保留一个gem?

  • 操作系统:macOS 10.15.5
  • rbenv:1.2.0
dgsult0t

dgsult0t1#

从设计上讲,你不能。本项目将此视为一项功能:
One thing well. rbenv is concerned solely with switching Ruby versions.
Buuuuut,你可以使用一个插件来创建Gemsets,你可以在每个应用程序的基础上切换。
我没有使用过它,但它似乎rbenv-gemset插件会给你给予你所追求的:https://github.com/jf/rbenv-gemset
安装完成后,您可以在项目的根目录中创建默认gemset:
$rbenv gemset init

z9gpfhce

z9gpfhce2#

我个人为此创建了这个shell函数:

function switch-ruby-gems() {
    rm -rf "${GEM_HOME}"
    gem install bundler
    bundle install
}

相关问题