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

发布于 2024-11-27 14:06:16 字数 121 浏览 0 评论 0原文

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

Rails 中的 GemfileGemfile.lock 有什么区别?

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

What is the difference between Gemfile and Gemfile.lock in Rails?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟花易冷人易散 2024-12-04 14:06:16

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

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

请查看 Bundler 的目的和基本原理,特别是“将代码签入版本控制”部分。

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

The Gemfile.lock file is where Bundler records the exact versions that were installed. This way, when the same library/project is loaded on another machine, running bundle install will look at the Gemfile.lock and install the exact same versions, rather than just using the Gemfile and 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.

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

瑾夏年华 2024-12-04 14:06:16

通常我们在 Gemfile 中编写依赖项如下:

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

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

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

这是什么意思 ?

正如您所看到的,如果您没有任何 Gemfile.lock 并运行:

bundle install

那么当前使用的 gem 可能随时不同。您的应用使用了版本 1.4.48 个月前可以正常运行,没有任何问题,但如果您现在尝试构建它,您会得到版本1.4.9。也许最新版本的 nokogiri 已损坏,您在 1.4.4 中使用的出色功能不再可用,等等。

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

Gemfile.lock 是如何创建的?

它是使用第一个:

bundle install

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

如何更新 Gemfile.lock?

如果您对最新版本的应用感到满意,则可以更新 Gemfile.lock。只需将您的更改反映到 Gemfile 即可。这意味着将依赖项更改为 Gemfile 中新的确切版本。运行后:

bundle install

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

Usually we write dependencies in Gemfile as:

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 Gemfile 8 months ago and 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.

Now think I'm trying to build with the same Gemfile. But if we look at nokogiri versions we 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.9 of nokogiri (suppose we don't have Gemfile.lock).

What does it mean ?

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

bundle install

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

To prevent this kind of problem Gemfile.lock is used. In Gemfile.lock only the exact versions are 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.

How is Gemfile.lock created?

It is automatically created with the first:

bundle install

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

How to update 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:

bundle install

This will update you Gemfile.lock with your newest version of apps.

假扮的天使 2024-12-04 14:06:16

Gemfile.lock

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

Bundler 在所有后续的捆绑安装调用中都使用此文件,这保证了您始终使用相同的代码,即使您的应用程序跨计算机移动也是如此。

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

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

The 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.

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.

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文