Ruby on Rails 中的 Gemfile 和 Gemfile.lock 有什么区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6927442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 01:42:05  来源:igfitidea点击:

What is the difference between Gemfile and Gemfile.lock in Ruby on Rails

ruby-on-railsrubybundlergemfilegemfile.lock

提问by Shamith c

I am a beginner to Ruby on Rails and I am using Rails 3.0.9.

我是 Ruby on Rails 的初学者,我使用的是 Rails 3.0.9。

What is the difference between Gemfileand Gemfile.lockin Rails?

RailsGemfileGemfile.lockRails 中的区别是什么?

回答by Dylan Markow

The Gemfileis where you specify which gems you want to use, and lets you specify which versions.

Gemfile是您指定要使用哪些 gem 的地方,并允许您指定哪些版本。

The Gemfile.lockfile is where Bundler records the exact versions that were installed. This way, when the same library/project is loaded on another machine, running bundle installwill look at the Gemfile.lockand install the exact same versions, rather than just using the Gemfileand installing the most recent versions. (Running different versions on different machines could lead to broken tests, etc.) You shouldn't ever have to directly edit the lock file.

Gemfile.lock文件是 Bundler 记录已安装的确切版本的地方。这样,当在另一台机器上加载相同的库/项目时,运行bundle install将查看Gemfile.lock并安装完全相同的版本,而不是仅仅使用Gemfile和安装最新版本。(在不同的机器上运行不同的版本可能会导致测试中断等)您不应该直接编辑锁定文件。

Check out Bundler's Purpose and Rationale, specifically the Checking Your Code into Version Control section.

查看Bundler 的目的和基本原理,特别是将代码检查到版本控制部分。

回答by Fatih Arslan

Usually we write dependencies in Gemfile as:

通常我们在 Gemfile 中写依赖为:

gem "nokogiri", "~> 1.4.4"
gem 'bcrypt-ruby', '~> 3.0.0'
gem 'uglifier', '>= 1.2.3'
..

Here you basically say: "I want nokogiri as long as it's greater than version 1.4.4", etc. Now suppose that I have set up my Gemfile8 months agoand I successful setup my app with this requirement. 8 months ago nokogiri version was 1.4.4. My rails apps was running perfectly without any problems with this version.

在这里你基本上会说:“我想要 nokogiri,只要它大于 1.4.4 版”等等。现在假设我已经在Gemfile8 个月前设置了我的应用程序并且我成功地设置了我的应用程序并满足这个要求。8 个月前 nokogiri 版本是1.4.4。我的 rails 应用程序运行完美,这个版本没有任何问题。

Now think I'm trying to build with the same Gemfile. But if we look at nokogiri versionswe see that the current stable version has changed to 1.4.9. That means if we try to build, bundler will install version 1.4.9of nokogiri (suppose we don't have Gemfile.lock).

现在想我正在尝试使用相同的Gemfile. 但是,如果我们查看nokogiri 版本,我们会发现当前的稳定版本已更改为1.4.9。这意味着如果我们尝试构建,bundler 将安装nokogiri 的1.4.9版本(假设我们没有Gemfile.lock)。

What does it mean ?

这是什么意思 ?

As you see if you don't have any Gemfile.lockand run:

如您所见,如果您没有Gemfile.lock并运行:

bundle install

then the currently used gems can be different at any time. Your app used the version 1.4.4and it works 8 months agowithout any problems, but if you try to build it nowyou get the version 1.4.9. Maybe it's broken with the latest version of nokogiri, the awesome feature you used with 1.4.4is not more available, etc..

那么当前使用的宝石可以随时不同。您的应用程序使用了1.4.4版本,它在8 个月前可以正常运行没有任何问题,但是如果您现在尝试构建它,您将获得1.4.9版本。也许这与最新版本打破nokogiri,你所使用的真棒功能1.4.4没有更多的可用等。

To prevent this kind of problem Gemfile.lockis used. In Gemfile.lockonly the exact versionsare written and thus only these will be installed. That means if you distribute your app with a Gemfile.lock, every machine will have the same gems installed and most important they all get the same version. This will give you a stable and common deployment stack.

为了防止这种问题Gemfile.lock被使用。在Gemfile.lock只有确切的版本被写入,因此只有这些将被安装。这意味着如果您使用 分发您的应用程序Gemfile.lock,每台机器都将安装相同的 gems,最重要的是它们都获得相同的版本。这将为您提供稳定且通用的部署堆栈。

How is Gemfile.lock created?

Gemfile.lock 是如何创建的?

It is automatically created with the first:

它是用第一个自动创建的:

bundle install

command. After that everytime you run bundle install, bundle will first look up Gemfile.lockand install the gems specified there. It's an habit to distribute this file among your projects to provide consistently and stability.

命令。之后每次运行时bundle install,bundle 将首先查找Gemfile.lock并安装那里指定的 gem。在您的项目之间分发此文件以提供一致和稳定性是一种习惯。

How to update Gemfile.lock?

如何更新 Gemfile.lock?

If you're happy with the the latest version of your apps than you can update Gemfile.lock. Just reflect your changes to Gemfile. That means change the dependencies to the new exact versions in Gemfile. After that run:

如果您对应用程序的最新版本感到满意,那么您可以更新Gemfile.lock. 只需将您的更改反映到Gemfile. 这意味着将依赖项更改为Gemfile. 在那之后运行:

bundle install

This will update you Gemfile.lockwith your newest version of apps.

这将为您更新Gemfile.lock最新版本的应用程序。

回答by Ajey

The Gemfile.lock

Gemfile.lock

When you run bundle install, Bundler will persist the full names and versions of all gems that you used (including dependencies of the gems specified in the Gemfile(5)) into a file called Gemfile.lock.

当您运行 bundle install 时,Bundler 会将您使用的所有 gem 的全名和版本(包括 Gemfile(5) 中指定的 gem 的依赖项)保存到一个名为 Gemfile.lock 的文件中。

Bundler uses this file in all subsequent calls to bundle install, which guarantees that you always use the same exact code, even as your application moves across machines.

Bundler 在所有后续调用 bundle install 中使用此文件,这保证您始终使用完全相同的代码,即使您的应用程序在机器之间移动。

Because of the way dependency resolution works, even a seemingly small change (for instance, an update to a point-release of a dependency of a gem in your Gemfile(5)) can result in radically different gems being needed to satisfy all dependencies.

由于依赖项解析的工作方式,即使是看似很小的更改(例如,对 Gemfile(5) 中 gem 依赖项的点发布更新)也可能导致需要完全不同的 gem 来满足所有依赖项。

As a result, you SHOULD check your Gemfile.lock into version control. If you do not, every machine that checks out your repository (including your production server) will resolve all dependencies again, which will result in different versions of third-party code being used if any of the gems in the Gemfile(5) or any of their dependencies have been updated.

因此,您应该将 Gemfile.lock 签入版本控制。如果不这样做,每台检出您的存储库(包括您的生产服务器)的机器将再次解析所有依赖项,这将导致使用不同版本的第三方代码,如果 Gemfile(5) 中的任何 gem 或任何他们的依赖项已更新。