/usr/bin/env ruby​​ 没有这样的文件或目录:使用 capistrano 3、capistrano/rbenv、capistrano/bundler 和 capistrano/rail

Posted

技术标签:

【中文标题】/usr/bin/env ruby​​ 没有这样的文件或目录:使用 capistrano 3、capistrano/rbenv、capistrano/bundler 和 capistrano/rails(使用 rails 4)【英文标题】:/usr/bin/env ruby no such file or directory: Using capistrano 3, capistrano/rbenv, capistrano/bundler and capistrano/rails (using rails 4) 【发布时间】:2013-11-12 00:37:56 【问题描述】:

我正在使用 capistrano、capistrano/rbenv、capistrano/bundler 和 capistrano/rails。我在 capistrano 编译资产的步骤中收到此错误:

DEBUG [49a50df6]    /usr/bin/env:
DEBUG [49a50df6]    ruby
DEBUG [49a50df6]    : No such file or directory
DEBUG [49a50df6]

在生产服务器中/usr/bin/env ruby -v 是正确的。 我知道这一点:why-does-something-work-in-my-ssh-session-but-not-in-capistrano 但我无法让它工作。

这是我的Capfile

# Load DSL and Setup Up Stages
require 'capistrano/setup'

# Includes default deployment tasks
require 'capistrano/deploy'

# Includes tasks from other gems included in your Gemfile
#
# For documentation on these, see for example:
#
#   https://github.com/capistrano/rvm
#   https://github.com/capistrano/rbenv
#   https://github.com/capistrano/chruby
#   https://github.com/capistrano/bundler
#   https://github.com/capistrano/rails/tree/master/assets
#   https://github.com/capistrano/rails/tree/master/migrations
#
# require 'capistrano/rvm'
require 'capistrano/rbenv'
# require 'capistrano/chruby'
require 'capistrano/bundler'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'

# Loads custom tasks from `lib/capistrano/tasks' if you have any defined.
Dir.glob('lib/capistrano/tasks/*.cap').each  |r| import r 

这是我的deploy.rb

# rbenv
set :rbenv_type, :user
set :rbenv_ruby, '2.0.0-p247'

# bundler
set :bundle_gemfile, ->  release_path.join('Gemfile') 
set :bundle_dir, ->  shared_path.join('bundle') 
set :bundle_flags, '--deployment --quiet'
set :bundle_without, %wdevelopment test.join(' ')
set :bundle_binstubs, ->  shared_path.join('bin') 
set :bundle_roles, :all

# rails
set :rails_env, 'production'

set :application, 'MY APP'
set :repo_url, 'MY_REPO.git'

# ask :branch, proc  `git rev-parse --abbrev-ref HEAD`.chomp 

# set :deploy_to, '/var/www/my_app'
# set :scm, :git

# set :format, :pretty
# set :log_level, :debug
# set :pty, true

set :linked_files, %w.env config/database.yml
# set :linked_dirs, %wbin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system

# set :default_env,  path: "/opt/ruby/bin:$PATH" 
set :keep_releases, 5

namespace :deploy do

  desc 'Restart application'
  task :restart do
    on roles(:app), in: :sequence, wait: 5 do
      # Your restart mechanism here, for example:
      # execute :touch, release_path.join('tmp/restart.txt')
    end
  end

  after :restart, :clear_cache do
    on roles(:web), in: :groups, limit: 3, wait: 10 do
      # Here we can do anything such as:
      # within release_path do
      #   execute :rake, 'cache:clear'
      # end
    end
  end

  after :finishing, 'deploy:cleanup'

end

这是我的staging.rb

set :stage, :staging

# Simple Role Syntax
# ==================
# Supports bulk-adding hosts to roles, the primary
# server in each group is considered to be the first
# unless any hosts have the primary property set.
#role :app, %wdeploy@example.com
#role :web, %wdeploy@example.com
#role :db,  %wdeploy@example.com

# Extended Server Syntax
# ======================
# This can be used to drop a more detailed server
# definition into the server list. The second argument
# something that quacks like a has can be used to set
# extended properties on the server.
#server 'example.com', user: 'deploy', roles: %wweb app, my_property: :my_value
server 'my_server', user: 'my_user', roles: %wweb app db

# you can set custom ssh options
# it's possible to pass any option but you need to keep in mind that net/ssh understand limited list of options
# you can see them in [net/ssh documentation](http://net-ssh.github.io/net-ssh/classes/Net/SSH.html#method-c-start)
# set it globally
#  set :ssh_options, 
#    keys: %w(/home/rlisowski/.ssh/id_rsa),
#    forward_agent: false,
#    auth_methods: %w(password)
#  
# and/or per server
# server 'example.com',
#   user: 'user_name',
# roles: %wweb app,
#   ssh_options: 
#     user: 'user_name', # overrides user setting above
#     keys: %w(/home/user_name/.ssh/id_rsa),
#     forward_agent: false,
#     auth_methods: %w(publickey password)
#     # password: 'please use keys'
#   
# setting per server overrides global ssh_options

# fetch(:default_env).merge!(rails_env: :staging)

set :deploy_to, '/home/my_user'

这是完整的跟踪:

 INFO [c24b8f94] Running RBENV_ROOT=~/.rbenv RBENV_VERSION=2.0.0-p247 ~/.rbenv/bin/rbenv exec bundle exec rake assets:precompile on 162.243.16.201
DEBUG [c24b8f94] Command: cd /home/my_app/releases/20131101193513 && ( RAILS_ENV=production RBENV_ROOT=~/.rbenv RBENV_VERSION=2.0.0-p247 ~/.rbenv/bin/rbenv exec bundle exec rake assets:precompile )
DEBUG [c24b8f94]    /usr/bin/env:
DEBUG [c24b8f94]    ruby
DEBUG [c24b8f94]    : No such file or directory
DEBUG [c24b8f94]
cap aborted!
rake stdout: Nothing written
rake stderr: Nothing written
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/sshkit-1.1.0/lib/sshkit/command.rb:94:in `exit_status='
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/sshkit-1.1.0/lib/sshkit/backends/netssh.rb:125:in `block (4 levels) in _execute'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/net-ssh-2.7.0/lib/net/ssh/connection/channel.rb:551:in `call'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/net-ssh-2.7.0/lib/net/ssh/connection/channel.rb:551:in `do_request'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/net-ssh-2.7.0/lib/net/ssh/connection/session.rb:561:in `channel_request'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/net-ssh-2.7.0/lib/net/ssh/connection/session.rb:465:in `dispatch_incoming_packets'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/net-ssh-2.7.0/lib/net/ssh/connection/session.rb:221:in `preprocess'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/net-ssh-2.7.0/lib/net/ssh/connection/session.rb:205:in `process'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/net-ssh-2.7.0/lib/net/ssh/connection/session.rb:169:in `block in loop'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/net-ssh-2.7.0/lib/net/ssh/connection/session.rb:169:in `loop'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/net-ssh-2.7.0/lib/net/ssh/connection/session.rb:169:in `loop'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/net-ssh-2.7.0/lib/net/ssh/connection/channel.rb:269:in `wait'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/sshkit-1.1.0/lib/sshkit/backends/netssh.rb:147:in `block (2 levels) in _execute'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/net-ssh-2.7.0/lib/net/ssh/connection/channel.rb:514:in `call'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/net-ssh-2.7.0/lib/net/ssh/connection/channel.rb:514:in `do_open_confirmation'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/net-ssh-2.7.0/lib/net/ssh/connection/session.rb:545:in `channel_open_confirmation'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/net-ssh-2.7.0/lib/net/ssh/connection/session.rb:465:in `dispatch_incoming_packets'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/net-ssh-2.7.0/lib/net/ssh/connection/session.rb:221:in `preprocess'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/net-ssh-2.7.0/lib/net/ssh/connection/session.rb:205:in `process'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/net-ssh-2.7.0/lib/net/ssh/connection/session.rb:169:in `block in loop'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/net-ssh-2.7.0/lib/net/ssh/connection/session.rb:169:in `loop'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/net-ssh-2.7.0/lib/net/ssh/connection/session.rb:169:in `loop'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/sshkit-1.1.0/lib/sshkit/backends/netssh.rb:149:in `block in _execute'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/sshkit-1.1.0/lib/sshkit/backends/netssh.rb:106:in `tap'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/sshkit-1.1.0/lib/sshkit/backends/netssh.rb:106:in `_execute'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/sshkit-1.1.0/lib/sshkit/backends/netssh.rb:54:in `execute'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/capistrano-rails-1.0.0/lib/capistrano/tasks/assets.rake:61:in `block (6 levels) in <top (required)>'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/sshkit-1.1.0/lib/sshkit/backends/abstract.rb:89:in `with'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/capistrano-rails-1.0.0/lib/capistrano/tasks/assets.rake:60:in `block (5 levels) in <top (required)>'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/sshkit-1.1.0/lib/sshkit/backends/abstract.rb:81:in `within'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/capistrano-rails-1.0.0/lib/capistrano/tasks/assets.rake:59:in `block (4 levels) in <top (required)>'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/sshkit-1.1.0/lib/sshkit/backends/netssh.rb:42:in `instance_exec'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/sshkit-1.1.0/lib/sshkit/backends/netssh.rb:42:in `run'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/sshkit-1.1.0/lib/sshkit/runners/parallel.rb:12:in `block (2 levels) in execute'
Tasks: TOP => deploy:assets:precompile

顺便说一句,我认为它与$PATH 无关,因为bundle install 运行良好。

有人知道怎么回事吗?

谢谢!

【问题讨论】:

能否提供您的Capfiledeploy.rbproduction/staging.rb 嘿@freemanoid,完成!谢谢! 能否提供完整的错误跟踪信息? @Kir:完成!谢谢! 【参考方案1】:

好的,因为我遇到了同样的问题,我花了很长时间才解决它:

您可能想知道为什么当您使用 ssh 使用 shell 登录时,此命令无法正常工作。

我不是专家(所以当我错了时请纠正我):问题是使用 ssh 在远程服务器上登录有几个不同之处。 引用 capistrano 的文档:

这真的是 Capistrano 使用哪种外壳的问题, 这是一个关于登录、非登录的可能性矩阵, 交互式或非交互式。

他们有一个精美的图形here(在底部),其中说明了在登录过程中执行的文件的路径。 - 如果你想继续阅读这个主题,给定的来源也很有趣。

出了什么问题?

当您使用 ssh 登录时,您(在登录时)正在执行多个文件(例如,当您使用 bash 时是 .bash_profile)。通常,您的 $PATH 变量会扩展为您的环境中重要功能的多个路径。但是不同的登录“样式”包含不同的文件。

正如文档再次所说:

默认情况下,Capistrano 始终分配一个非登录、非交互 贝壳。

在我的情况下,这会导致我的登录文件 .bash_profile 不包括在内,但它包括我的 rvm 的设置。 capistrano 登录会话不知道我的 rvm 路径的结果。

我该如何解决这个问题?

对我来说,这只是 Capfile(位于您的 rails-project 的根目录中。

希望对某人有所帮助。

编辑:

并且一定要在 Gemfile 中包含 gem 'capistrano-rvm'

【讨论】:

+1 这为我解决了问题。很好,详细的回复。比将路径添加到deploy.rb! +1 人,取消注释 require 'capistrano/rvm' 并将 gem 'capistrano-rvm' 添加到 gemfile,为我做了魔法! 这很有帮助,但我还必须在我的服务器上安装 rvm 和 ruby​​。这对其他人来说可能很明显,但为了完整起见,我添加了这个细节。谢谢你的提示。这对我有用。【参考方案2】:

好的,解决了。

我添加了这个:

set :default_env,  path: "~/.rbenv/shims:~/.rbenv/bin:$PATH" 

致我的deploy.rb

我不确定是不是最好的解决方案,但它确实有效。

【讨论】:

是的,在 Capistrano 中修改 PATH。是相当肮脏的黑客。在cd /home/my_app/releases/20131101193513 &amp;&amp; ( RAILS_ENV=production RBENV_ROOT=~/.rbenv RBENV_VERSION=2.0.0-p247 ~/.rbenv/bin/rbenv exec bundle exec rake assets:precompile ) 上失败了您是否尝试手动运行相同的命令? 是的,我尝试手动运行它并且成功了。我即将在github上创建一个PR。 你的服务器操作系统是什么? @Kir 我也有同样的问题。部署到 Ubuntu 10.04。这个建议的解决方案对我不起作用,但我可以手动完成。 我们最近更新了 capistrano-rbenv,你可以用最新的 Capistrano 试试。它对你有用吗?【参考方案3】:

我认为你应该执行

apt-get install git-core

在你的服务器上

【讨论】:

你是怎么知道要添加 git-core 的?或者是什么泄露了它? 该软件包已安装在我的服务器上,但我仍然遇到上述问题。我不明白这个答案有多少赞成票! ?️【参考方案4】:

我在使用 rbenv 时遇到了同样的问题。为我解决的问题是在 bundler 之前包含 rbenv 库——而不是在它之后。事实证明,订单很重要。这是我的 Capfile 中的内容:

require 'capistrano/rbenv'
require 'capistrano/bundler'

如果你做对了,你应该会看到使用 rbenv 前缀执行的 bundle 命令,如下所示:

XX RBENV_ROOT=$HOME/.rbenv RBENV_VERSION=2.3.0 $HOME/.rbenv/bin/rbenv exec bundle install ...

【讨论】:

【参考方案5】:

确保您的 Capfile 中需要所有这些 gem。

【讨论】:

我在Capfile 中也需要它们。 @Kir 这可能是对问题的评论,而不是答案【参考方案6】:

我的问题是:

DEBUG [3132b2c2]    /usr/bin/env: ‘bundle’: No such file or directory
DEBUG [3132b2c2] 

我是这样解决的:

在 Debian 中,您的 ~/.bashrc 中有一段代码表示:

# If not running interactively, don't do anything

case $- in
   *i*) ;;
     *) return;;
esac

当您运行 Capistrano 时,它实际上会使用您的 ~/.bashrc,但不会运行该代码块下方的任何命令。

因此,我修复了在该代码块上方添加加载 ruby​​ 环境所需的代码行的问题,以便 Capistrano 可以加载包。

source /usr/local/share/chruby/chruby.sh
source /usr/local/share/chruby/auto.sh

PS:我的服务器使用的是chruby。

【讨论】:

【参考方案7】:

错误的解决方法:

/usr/bin/env git 没有这样的文件或目录 capistrano

在远程服务器上安装 git,要进行部署的机器。

apt-get install git

【讨论】:

【参考方案8】:

当我在终端上以交互方式输入bundle install 时,它起作用了。现在,当我通过 Capistrano 使用它时,它不起作用。

我是这样解决的:

在 Ubuntu 中,您的 ~/.bashrc 中有一段代码:

# If not running interactively, don't do anything
case $- in
   *i*) ;;
     *) return;;
esac

当您运行 Capistrano 时,它实际上会使用您的 ~/.bashrc 但它不会运行该代码块下方的任何命令。

因此,我解决了移动 bundle install 所需的代码行的问题,高于该代码块,因此 Capistrano 可以加载它们。

您可以通过运行 which bundle 来提示要移动哪些行。

【讨论】:

【参考方案9】:

我遇到了同样的错误。在努力解决后,我记得我从部署代码的服务器上随机删除了几个包。

所以我也意外删除了 git。运行sudo apt-get install git 修复了它。

【讨论】:

以上是关于/usr/bin/env ruby​​ 没有这样的文件或目录:使用 capistrano 3、capistrano/rbenv、capistrano/bundler 和 capistrano/rail的主要内容,如果未能解决你的问题,请参考以下文章

#!/usr/bin/env ruby​​ 在 cron 中找不到

詹金斯 - /usr/bin/env: 节点:没有这样的文件或目录

gradlew 出错:/usr/bin/env: bash: 没有这样的文件或目录

如何解决“/usr/bin/env: ruby​​_executable_hooks: No such file or directory”?

ruby的if判断

browserify error /usr/bin/env: node: No such file or directory