Ruby相当于virtualenv吗?

问题:Ruby相当于virtualenv吗?

是否有类似于Python实用程序virtualenv的东西?

基本上,它允许您将Python软件包安装到沙盒环境中,因此easy_install django它不会进入系统范围的site-packages目录中,而是会进入virtualenv创建的目录中。

例如:

$ virtualenv test
New python executable in test/bin/python
Installing setuptools...cd .........done.
$ cd test/
$ source bin/activate
(test)$ easy_install tvnamer
Searching for tvnamer
Best match: tvnamer 0.5.1
Processing tvnamer-0.5.1-py2.5.egg
Adding tvnamer 0.5.1 to easy-install.pth file
Installing tvnamer script to /Users/dbr/test/bin

Using /Library/Python/2.5/site-packages/tvnamer-0.5.1-py2.5.egg
Processing dependencies for tvnamer
Finished processing dependencies for tvnamer
(test)$ which tvnamer 
/Users/dbr/test/bin/tvnamer

RubyGems是否有类似的东西?

Is there something similar to the Python utility virtualenv?

Basically it allows you to install Python packages into a sandboxed environment, so easy_install django doesn’t go in your system-wide site-packages directory, it would go in the virtualenv-created directory.

For example:

$ virtualenv test
New python executable in test/bin/python
Installing setuptools...cd .........done.
$ cd test/
$ source bin/activate
(test)$ easy_install tvnamer
Searching for tvnamer
Best match: tvnamer 0.5.1
Processing tvnamer-0.5.1-py2.5.egg
Adding tvnamer 0.5.1 to easy-install.pth file
Installing tvnamer script to /Users/dbr/test/bin

Using /Library/Python/2.5/site-packages/tvnamer-0.5.1-py2.5.egg
Processing dependencies for tvnamer
Finished processing dependencies for tvnamer
(test)$ which tvnamer 
/Users/dbr/test/bin/tvnamer

Is there something like this for RubyGems?


回答 0

RVM的工作方式与virtualenv的工作方式更为接近,因为它可以让您沙盒化不同的红宝石版本及其宝石等。

RVM works closer to how virtualenv works since it lets you sandbox different ruby versions and their gems, etc.


回答 1

沙箱,RVM和rbenv都不管理应用程序的gem依赖项的版本。该工具是bundler

  • 使用Gemfile作为应用程序的依赖项声明
  • 用于bundle install将这些依赖项的显式版本安装到隔离的位置
  • 用于bundle exec运行您的应用程序

Neither sandbox, RVM, nor rbenv manage the versions of your app’s gem dependencies. The tool for that is bundler.

  • use a Gemfile as your application’s dependency declaration
  • use bundle install to install explicit versions of these dependencies into an isolated location
  • use bundle exec to run your application

回答 2

似乎没有人提到rbenv

No one seems to have mentioned rbenv.


回答 3

我想您会喜欢沙盒

I think you’ll like sandbox.


回答 4

我将提到使用Bundler进行此操作的方式(我将其与RVM一起使用-RVM用于管理红宝石和一组默认的全局宝石,Bundler则用于处理项目特定的宝石)

bundler install --binstubs --path vendor

在项目的根目录中运行此命令将安装Gemfile中列出的gems,将libs放入中./vendor./bin并且所有requires 中的所有可执行文件(如果使用bundle console或Bundler要求)将引用这些exes和libs。

为我工作。

I’ll mention the way I do this with Bundler (which I use with RVM – RVM to manage the rubies and a default set of global gems, Bundler to handle project specific gems)

bundler install --binstubs --path vendor

Running this command in the root of a project will install the gems listed from your Gemfile, put the libs in ./vendor, and any executables in ./bin and all requires (if you use bundle console or the Bundler requires) will reference these exes and libs.

Works for me.


回答 5

如果仅需要以非root用户身份安装gem,请尝试设置GEM_HOME环境变量。然后运行gem

例如:

$ export GEM_HOME=$HOME/local/gems
$ gem install rhc

If you only need to install gems as non-root, try setting the GEM_HOME environment variable. Then just run gem.

For example:

$ export GEM_HOME=$HOME/local/gems
$ gem install rhc

回答 6

我推荐direnv。它是外壳的环境切换器。

在每个提示之前,它会检查当前目录和父目录中是否存在“ .envrc”文件。如果文件存在(并已授权),则将其加载到bash子shell中,然后所有导出的变量都由direnv捕获,然后使当前shell可用。

这是如何在 ruby-install中使用direnv

+红宝石安装

将此添加到 ~/.direnvrc

use_ruby() {
  local ruby_root=$HOME/.rubies/$1
  load_prefix "$ruby_root"
  layout_ruby
}

安装ruby-install(brew install ruby-install)并安装一堆Ruby 。

ruby-install ruby 1.9.3
ruby-install ruby 2.0.0
ruby-install ruby 2.2.0

为了方便起见,然后进行几个符号链接:

ln -s .rubies/1.9 ruby-1.9.3-p*
ln -s .rubies/2.0 ruby-2.0.0
ln -s .rubies/2.2 ruby-2.2.0

最后在任何项目中.envrc

use ruby 2.0

这会将所有gem放置在项目.direnv/ruby目录下(使打开gem更加容易)。捆绑程序将放入包装二进制文件 .direnv/bin(不再bundle exec!)。

+ rbenv

也可以通过use rbenv在任何.envrc文件中添加命令来使用rbenv 。这将激活rbenv,从而将红宝石包装器放入PATH中。

请注意,不必在.bashrc或.zshrc中安装rbenv即可起作用。

+ RVM

这是我在ruby项目上使用的最复杂的.envrc:

rvm use 1.8.7
layout ruby
PATH_add .direnv/bundler-bin

rvm用于为您选择正确的红宝石版本

布局命令会自动设置一些常用的环境变量。目前仅存在红宝石布局。它的作用是设置GEM_HOME环境变量,并将它放在bin目录中。因为它取决于Ruby版本,所以请确保在“ rvm”之后调用它。由于每个ruby布局目录都有其自己的GEM_HOME,因此您无需使用rvm的gemset。

PATH_add前置并扩展给定的相对路径。在这种情况下,我使用它来将捆绑程序binstub与我自己的bin脚本隔离,bundle install --binstubs .direnv/bundler-bin

如果您想了解这些命令的确切功能,现在:cat direnv stdlib| 减

I recommend direnv. It is an environment switcher for the shell.

Before each prompt it checks for the existence of an “.envrc” file in the current and parent directories. If the file exists (and authorized), it is loaded into a bash sub-shell and all exported variables are then captured by direnv and then made available the current shell.

Here is how to use direnv with ruby-install

+ ruby-install

Add this to the ~/.direnvrc

use_ruby() {
  local ruby_root=$HOME/.rubies/$1
  load_prefix "$ruby_root"
  layout_ruby
}

Install ruby-install (brew install ruby-install) and install a bunch of rubies.

ruby-install ruby 1.9.3
ruby-install ruby 2.0.0
ruby-install ruby 2.2.0

And then make a couple of symlinks for convenience:

ln -s .rubies/1.9 ruby-1.9.3-p*
ln -s .rubies/2.0 ruby-2.0.0
ln -s .rubies/2.2 ruby-2.2.0

And finally in any project’s .envrc:

use ruby 2.0

This will put all gems under the project’s .direnv/ruby directory (makes opening gems easier). bundler will put wrapper binaries in .direnv/bin (no more bundle exec!).

+ rbenv

It’s also possible to use rbenv by adding the use rbenv command in any .envrc file. This will activate rbenv which in turn will put the ruby wrappers in the PATH.

Note that it’s not necessary to install rbenv in the .bashrc or .zshrc for this to work.

+ RVM

Here is the most complicated .envrc that I use on ruby projects:

rvm use 1.8.7
layout ruby
PATH_add .direnv/bundler-bin

rvm is used to select the right ruby version for you

layout commands automatically set some of the usual environment variables. For now only the ruby layout exists. What it does is set the GEM_HOME environment variable and it’s bin directory to your path. Because it depends on the ruby version, make sure to call it after “rvm”. Since each ruby layout directories have their own GEM_HOME, you don’t need to use rvm’s gemsets.

PATH_add prepends and expands the given relative path. In that case, I use this to segregate the bundler binstubs from my own bin scripts with bundle install --binstubs .direnv/bundler-bin

If you want to find out what those commands exactly do, for now: cat direnv stdlib | less


回答 7

Mineshaft是一个我已经从事了一段时间的项目,并将继续进行开发工作。

它提供了创建类似于virtualenv工作原理的虚拟环境的能力,并且还可以全局安装Ruby。

Mineshaft is a project that I’ve been working on for some time and am continuing development work on.

It offers the ability to both create virtual environments akin to how virtualenv works and can also install Ruby globally as well.