You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Capistrano部署Rails时无法运行/etc/profile.d/chruby.sh的问题

解决Capistrano部署Rails到EC2时chruby找不到的问题

从你给出的报错信息来看:

01 /bin/sh: 2: /etc/profile.d/chruby.sh:
01 source: not found
01
01 /bin/sh: 3: /etc/profile.d/chruby.sh:
01 source: not found
01
01 /bin/sh: 1:
01 chruby: not found

问题核心出在Capistrano默认使用非交互式的sh shell,这带来两个直接问题:

  1. 非交互式shell不会自动加载/etc/profile.d下的配置脚本
  2. sh不支持source命令(这是bash专属的内置命令,sh环境下要改用.代替)

下面是针对性的解决步骤:

1. 修正服务器端chruby.sh的命令

登录EC2服务器,编辑/etc/profile.d/chruby.sh,把所有source替换成.:

. xx/xx/xx/chruby.sh
. xx/xx/xx/auto.sh

这样即使在sh环境下也能正确加载chruby的配置脚本。

2. 让Capistrano使用bash并确保chruby环境加载

在本地的config/deploy.rb中添加以下配置,强制Capistrano使用bash,同时手动确保chruby环境在部署任务前加载:

# 强制使用bash作为执行shell
set :shell, '/bin/bash'

# 在bundler安装前加载chruby配置
before 'bundler:install', :load_chruby
task :load_chruby do
  on roles(:all) do
    execute '. /etc/profile.d/chruby.sh'
  end
end

或者更直接的方式,跳过profile脚本,直接指定chruby-exec的绝对路径:

set :chruby_ruby, 'ruby-2.3.0'
# 明确指定chruby-exec的绝对路径,避免环境变量缺失问题
set :chruby_exec, '/usr/local/bin/chruby-exec'

3. 验证服务器端的非交互式shell环境

在EC2服务器上,用deploy用户执行以下命令,模拟Capistrano的执行环境,验证配置是否生效:

/bin/sh -c '/usr/local/bin/chruby-exec ruby-2.3.0 -- ruby -v'

如果能正常输出Ruby 2.3.0的版本信息,说明环境配置没问题;如果仍报错,检查chruby的安装路径是否正确,以及deploy用户是否有对应文件的访问权限。

4. 额外检查点

  • 确认EC2服务器上确实安装了ruby-2.3.0,可以用chruby命令查看已安装的版本列表
  • 确保deploy用户拥有应用目录的完整读写权限
  • 核对deploy.rb中:chruby_ruby的配置和服务器上的Ruby版本完全一致(注意版本号的格式和大小写)

按照这些步骤调整后,重新执行cap production deploy应该就能解决bundler安装阶段的chruby找不到问题了。

内容的提问来源于stack exchange,提问作者alchemist95

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:04:05