为什么要“捆绑安装” gem 而不是“gem install”对于 Rails 3 应用程序?
我是一名初学者程序员,正在学习 railstutorial 作者:michael hartl,请注意,在应用程序中使用 gems 的过程是通过将其添加到 gemfile,然后执行 a:
$ bundle install
任何人不只是执行 a: 的原因
$ [sudo] gem install [the_gem]
,然后将其添加到宝石文件?我知道这在某种程度上是 Rails 2 中的过程。
谢谢!
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:
$ 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.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用bundler而不是gem命令来直接安装gems会给您带来很多好处。
在这种特定情况下,您建议使用 gem 命令进行安装并稍后将其添加到 Gemfile 中,bundler 将在您安装 gem 时为您解析所有依赖项,否则您可能需要手动解析。
举个例子,我们采用以下依赖项:
webrat 和 sunspot_rails gem 都需要不同版本的 nokogiri 作为依赖项。如果您只是使用 gem 命令来安装它们,它可能会安装 nokogiri 的两个版本,或更糟糕的是会抱怨版本冲突。 Bundler 将足够明智地解决此依赖性冲突并安装正确的版本(例如 nokogiri 1.3)并使 sunspot_rails 和 webrat 都满意!
抱歉这么长的解释。但是,希望你明白这一点! :)
顺便说一句,您应该查看这个文件
Gemfile.lock
以了解捆绑程序在幕后为您做了什么。Using bundler instead of gem command to directly install your gems gives you a whole lot of benefits.
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.
To give you an example, let's take the following dependencies:
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!
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,您可以确保也在开发您的应用程序的任何其他开发人员都拥有相同版本的每个 gem。它还确保您没有在 gem 命令中放置与 Gemfile 中不同的 [the_gem]。
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.
一些 gem 组合仅在特定环境中有意义 - 例如开发中的某个数据库和生产中的不同数据库。
Gemfile 记录了您的应用程序在每个环境中所需的内容,而
bundle install
会准确安装所需的内容,并处理依赖关系。如果 Gemfile 中的任何 gem 已经安装在当前的 gemset 中(通过 gem install),那么捆绑器将只使用它们(而不是重新安装它们)。
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.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).