为什么“捆绑安装”gem而不是“gem install”for rails 3 app?

时间:2020-12-18 20:06:19

I'm a beginner programmer going through the railstutorial by michael hartl, and notice that the process for using gems in the application is through adding it to the gemfile, and then doing a:

我是初学程序员,经过michael hartl的铁道教程,并注意到在应用程序中使用gems的过程是通过将它添加到gemfile,然后执行:

$ bundle install

Any reason why one wouldn't just do a:

为什么一个人不会这样做的任何理由:

$ [sudo] gem install [the_gem]

and then just add it to the Gem file? I know this was somewhat the process back in rails 2.

然后只需将其添加到Gem文件中?我知道这在轨道2中有点过程。

Thanks!

谢谢!

3 个解决方案

#1


37  

Using bundler instead of gem command to directly install your gems gives you a whole lot of benefits.

使用bundler而不是gem命令直接安装你的宝石可以带来很多好处。

In this specific case where you suggest using the gem command to install and adding it later to the Gemfile, bundler will resolve all the dependencies for you when you install a gem, which you might have to manually resolve otherwise.

在这种特殊情况下,您建议使用gem命令安装并稍后将其添加到Gemfile中,Bundler将在您安装gem时为您解析所有依赖项,否则您可能需要手动解析。

To give you an example, let's take the following dependencies:

举个例子,让我们采取以下依赖关系:

sunspot_rails
  nokogiri (>= 1.2.0)

webrat
  nokogiri (>= 1.3) 

Both webrat and sunspot_rails gems require different versions of nokogiri as a dependency. If you just use the gem command to install them, it might install both versions of nokogiri or worse complain about version conflicts. Bundler will be wise enough to resolve this dependency conflict and install the right version (say nokogiri 1.3) and make both sunspot_rails and webrat happy!

webrat和sunspot_rails宝石都需要不同版本的nokogiri作为依赖。如果您只是使用gem命令来安装它们,它可能会安装两个版本的nokogiri或更糟糕的抱怨版本冲突。 Bundler将足够明智地解决这种依赖冲突并安装正确的版本(比如说nokogiri 1.3)并使sunspot_rails和webrat都满意!

Sorry about the long explanation. But, hope you get the point! :)

很抱歉很长的解释。但是,希望你明白这一点! :)

And btw you should have a look at this file Gemfile.lock to see what bundler does behind the scenes for you.

顺便说一句,你应该看一下这个文件Gemfile.lock来看看Bundler在幕后为你做什么。

#2


3  

With the Gemfile, you can make sure that any other developers also working on your app have the same version of every gem. It also ensures that you didn't put a different [the_gem] in your gem commmand from what you have in your Gemfile.

使用Gemfile,您可以确保在您的应用程序上工作的任何其他开发人员都拥有相同版本的每个gem。它还确保您没有在Gemfile中的gem命令中添加不同的[the_gem]。

#3


2  

Some gem combinations only make sense in specific environments - e.g. a certain db in development and a different one in production.

一些宝石组合仅在特定环境中有意义 - 例如开发中的某个数据库和生产中的不同数据库。

The Gemfile is a record of what your application needs in each environment, and bundle install installs exactly what is needed, taking care of dependencies.

Gemfile是您的应用程序在每个环境中需要的记录,bundle install安装所需的内容,负责依赖。

If any of the gems in your Gemfile is already installed in your current gemset (via gem install), then the bundler will just use them (rather than reinstalling them).

如果Gemfile中的任何gem已经安装在当前的gemset中(通过gem install),那么bundle就会使用它们(而不是重新安装它们)。

#1


37  

Using bundler instead of gem command to directly install your gems gives you a whole lot of benefits.

使用bundler而不是gem命令直接安装你的宝石可以带来很多好处。

In this specific case where you suggest using the gem command to install and adding it later to the Gemfile, bundler will resolve all the dependencies for you when you install a gem, which you might have to manually resolve otherwise.

在这种特殊情况下,您建议使用gem命令安装并稍后将其添加到Gemfile中,Bundler将在您安装gem时为您解析所有依赖项,否则您可能需要手动解析。

To give you an example, let's take the following dependencies:

举个例子,让我们采取以下依赖关系:

sunspot_rails
  nokogiri (>= 1.2.0)

webrat
  nokogiri (>= 1.3) 

Both webrat and sunspot_rails gems require different versions of nokogiri as a dependency. If you just use the gem command to install them, it might install both versions of nokogiri or worse complain about version conflicts. Bundler will be wise enough to resolve this dependency conflict and install the right version (say nokogiri 1.3) and make both sunspot_rails and webrat happy!

webrat和sunspot_rails宝石都需要不同版本的nokogiri作为依赖。如果您只是使用gem命令来安装它们,它可能会安装两个版本的nokogiri或更糟糕的抱怨版本冲突。 Bundler将足够明智地解决这种依赖冲突并安装正确的版本(比如说nokogiri 1.3)并使sunspot_rails和webrat都满意!

Sorry about the long explanation. But, hope you get the point! :)

很抱歉很长的解释。但是,希望你明白这一点! :)

And btw you should have a look at this file Gemfile.lock to see what bundler does behind the scenes for you.

顺便说一句,你应该看一下这个文件Gemfile.lock来看看Bundler在幕后为你做什么。

#2


3  

With the Gemfile, you can make sure that any other developers also working on your app have the same version of every gem. It also ensures that you didn't put a different [the_gem] in your gem commmand from what you have in your Gemfile.

使用Gemfile,您可以确保在您的应用程序上工作的任何其他开发人员都拥有相同版本的每个gem。它还确保您没有在Gemfile中的gem命令中添加不同的[the_gem]。

#3


2  

Some gem combinations only make sense in specific environments - e.g. a certain db in development and a different one in production.

一些宝石组合仅在特定环境中有意义 - 例如开发中的某个数据库和生产中的不同数据库。

The Gemfile is a record of what your application needs in each environment, and bundle install installs exactly what is needed, taking care of dependencies.

Gemfile是您的应用程序在每个环境中需要的记录,bundle install安装所需的内容,负责依赖。

If any of the gems in your Gemfile is already installed in your current gemset (via gem install), then the bundler will just use them (rather than reinstalling them).

如果Gemfile中的任何gem已经安装在当前的gemset中(通过gem install),那么bundle就会使用它们(而不是重新安装它们)。