使用Mina部署Rails应用时遇bundle命令未找到错误求助
Rails应用Mina部署时bundle命令找不到的问题解决
问题原因
直接登录服务器用的是交互式shell,会自动加载~/.bashrc、~/.bash_profile这类配置文件,里面包含了Ruby版本管理工具(rvm、rbenv、chruby等)的路径配置,所以能找到bundle命令。而Mina部署时使用的是非交互式非登录shell,默认不会加载这些配置文件,导致系统PATH中没有bundle的所在路径,因此报错bash: line 147: bundle: command not found。
解决方法
方案1:在Mina配置中加载环境变量
修改项目根目录下的config/deploy.rb文件,添加环境初始化任务,确保部署时加载Ruby版本管理工具的配置:
- 若使用rvm:
task :environment do command 'source ~/.rvm/scripts/rvm' # 若使用gemset,可添加下面一行,替换为你的Ruby版本和gemset # command 'rvm use ruby-3.2.2@your_app_gemset' end
- 若使用rbenv:
task :environment do command 'export PATH="$HOME/.rbenv/bin:$PATH"' command 'eval "$(rbenv init -)"' end
- 若使用chruby:
task :environment do command 'source /usr/local/share/chruby/chruby.sh' command 'chruby ruby-3.2.2' # 替换为你的Ruby版本 end
方案2:直接指定bundle的绝对路径
先在服务器上执行which bundle,得到bundle的绝对路径(例如/home/deploy/.rvm/gems/ruby-3.2.2@global/bin/bundle),然后在config/deploy.rb中添加:
set :bundle_command, '/home/deploy/.rvm/gems/ruby-3.2.2@global/bin/bundle'
将路径替换为你自己服务器上的bundle绝对路径即可。
方案3:修改服务器shell配置,让非交互式shell加载环境变量
把Ruby版本管理工具的配置代码移到~/.bashrc文件中(非交互式shell默认会加载~/.bashrc,而~/.bash_profile仅在登录shell时加载):
- 对于rvm,在
~/.bashrc末尾添加:
source ~/.rvm/scripts/rvm
- 对于rbenv,在
~/.bashrc末尾添加:
export PATH="$HOME/.rbenv/bin:$PATH" eval "$(rbenv init -)"
内容的提问来源于stack exchange,提问作者Diogo Amaral
相关产品推荐
相关产品推荐

