title: /usr/bin/env ruby no such file or directory: Using capistrano 3, capistrano/rbenv, capistrano/bundler and capistrano/rails (using rails 4)
body: <p>I'm using capistrano, capistrano/rbenv, capistrano/bundler and capistrano/rails. I get this error in the step where capistrano compiles the assets:</p>
  
  <pre><code>DEBUG [49a50df6]    /usr/bin/env:
  DEBUG [49a50df6]    ruby
  DEBUG [49a50df6]    : No such file or directory
  DEBUG [49a50df6]
  </code></pre>
  
  <p>In the production server <code>/usr/bin/env ruby -v</code> is correct.
  I'm aware of this: <a href="http://www.capistranorb.com/documentation/faq/why-does-something-work-in-my-ssh-session-but-not-in-capistrano/" rel="noreferrer">why-does-something-work-in-my-ssh-session-but-not-in-capistrano</a>
  But I can't get it to work.</p>
  
  <p>This is my <code>Capfile</code>:</p>
  
  <pre><code># 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 }
  </code></pre>
  
  <p>This is my <code>deploy.rb</code>:</p>
  
  <pre><code># rbenv
  set :rbenv_type, :user
  set :rbenv_ruby, '2.0.0-p247'
  
  # bundler
  set :bundle_gemfile, -&gt; { release_path.join('Gemfile') }
  set :bundle_dir, -&gt; { shared_path.join('bundle') }
  set :bundle_flags, '--deployment --quiet'
  set :bundle_without, %w{development test}.join(' ')
  set :bundle_binstubs, -&gt; { 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, %w{bin 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
  </code></pre>
  
  <p>And this is my <code>staging.rb</code>:</p>
  
  <pre><code>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, %w{deploy@example.com}
  #role :web, %w{deploy@example.com}
  #role :db,  %w{deploy@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: %w{web app}, my_property: :my_value
  server 'my_server', user: 'my_user', roles: %w{web 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: %w{web 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'
  </code></pre>
  
  <p>And this is the full trace:</p>
  
  <pre><code> 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 &amp;&amp; ( 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 &lt;top (required)&gt;'
  /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 &lt;top (required)&gt;'
  /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 &lt;top (required)&gt;'
  /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 =&gt; deploy:assets:precompile
  </code></pre>
  
  <p>By the way, I think it's not related to <code>$PATH</code>, because <code>bundle install</code> runs just fine.</p>
  
  <p>Does anyone know what's going on?</p>
  
  <p>Thank you!</p>