bundle exec与直接运行的差异

Created: 06 Jan 2026

Author:  Chen Xie

摘要

jekyll serve​为例,它默认使用的系统的相关包和环境。而bundle exec Jekyll serve​是使用Gemfile.lock里申明的包的版本来运行。

当系统的包的版本与Gemfile.lock​中不一致时,特别是系统的版本比Gemfile.lock里的版本高时,会报错。

所以,推荐使用bundle exec Jekyll serve

案例:

通过bundle install生成的Gemfile.lock bigdecimal (3.3.1),运行jekyll serve提示现在bigdecimal 版本为4.0.1,能怎么提升bundle install 生成bigdecimal的版本吗?

AI答案:

通过 bundle update​ 后,Gemfile.lock

中的 bigdecimal​ 仍然是 3.3.1,因为它是间接依赖(通过 sass-embedded​ → google-protobuf​ → bigdecimal​),而 google-protobuf​ 的当前版本(4.33.2)要求 bigdecimal (~> 3.1),不支持 4.x 版本。

原因分析

解决方案

  1. 始终使用 bundle exec

    • 运行 bundle exec jekyll serve​ 而非 jekyll serve,确保使用 bundle 安装的 gem 版本(3.3.1),避免与系统 gem 冲突。
  2. 如果必须升级到 4.x

    • 等待上游依赖(如 google-protobuf​ 或 sass-embedded​)更新以支持 bigdecimal​ 4.x。目前无法通过 bundle install​ 或 bundle update 强制升级,因为会违反依赖约束。
    • 如果坚持,可以在 Gemfile

      中添加 gem ‘bigdecimal’, ‘>= 4.0.0’

      ,但这可能导致 bundle 失败或兼容性问题,不推荐。

  3. 验证版本

    • 运行 bundle exec gem list bigdecimal 检查 bundle 使用的版本。
    • 如果仍有提示问题,请提供完整的错误日志以进一步诊断。

使用 bundle exec 后,项目应正常运行,无需担心版本冲突。

# jekyll bundle ruby