Capistrano部署Rails应用报错:bash: bundle: command not found(已装bundler)
解决Capistrano部署Rails时「bundle: command not found」的问题
嘿,这个坑我踩过好多次了!明明服务器上手动装了bundler,但Capistrano跑起来就找不到命令,核心原因就是Capistrano用的是非交互式shell,默认不会加载你平时登录时的Ruby/RVM环境变量配置。下面给你几个按优先级排序的解决办法:
1. 给Capistrano加上RVM支持(最推荐)
如果你的服务器用了RVM,先在项目的Capfile里引入RVM的Capistrano插件:
# Capfile中添加这行 require 'capistrano/rvm'
然后在config/deploy.rb里指定你项目用的Ruby版本(要和本地、服务器上的一致):
set :rvm_ruby_version, '2.7.6' # 替换成你实际的Ruby版本号
这样Capistrano会自动帮你加载RVM的环境,自然就能找到bundle命令了。
2. 手动指定bundle的绝对路径
如果第一种方法没生效,先登录服务器,用deployer用户执行which bundle,会得到类似/home/deployer/.rvm/gems/ruby-2.7.6/bin/bundle的绝对路径。然后把这个路径写到config/deploy.rb里:
set :bundle_cmd, '/home/deployer/.rvm/gems/ruby-2.7.6/bin/bundle' # 替换成你查到的路径
3. 让Capistrano加载登录shell的环境配置
非交互式shell默认不会加载~/.bashrc或~/.bash_profile里的内容,你可以在config/deploy.rb里手动把Ruby相关的路径加到环境变量里:
set :default_env, { path: '/home/deployer/.rvm/gems/ruby-2.7.6/bin:/home/deployer/.rvm/bin:$PATH' }
或者直接让Capistrano用登录shell执行命令:
set :ssh_options, { forward_agent: true, shell: 'bash -l' # 加载登录shell的配置文件 }
4. 确认Bundler是用deployer用户安装的
别犯低级错误!如果你之前用root用户装了bundler,deployer用户可能没权限访问。切换到deployer用户重新安装一次:
su - deployer gem install bundler
搞定上面的步骤后,再重新跑cap production deploy应该就没问题了。
内容的提问来源于stack exchange,提问作者Jais Anasrulloh Ja'fari
相关产品推荐
相关产品推荐

