title: Capistrano fails to deploy to remote server
body: <p><strong>SOLUTION</strong></p>
  
  <p>The solution was to add the following to the top of production.rb:</p>
  
  <pre><code>$:.unshift(File.expand_path('./lib', ENV['rvm_path'])) # Add RVM's lib directory to the load path.
  require "rvm/capistrano"                  # Load RVM's capistrano plugin.
  set :rvm_ruby_string, '1.9.2@rails31dataserver'        # Or whatever env you want it to run in.
  set :rvm_bin_path, '/usr/local/rvm/bin'
  </code></pre>
  
  <p>The part I was missing before was <code>set :rvm_bin_path, '/usr/local/rvm/bin'</code> as I've never used that configuration setting in a Capistrano deployment before, and I've done a few.  Oh well, all in a days work.  Credit goes to tonyflint in the #rvm channel on irc.freenode.net!</p>
  
  <p><em><strong>Original Post</em></strong></p>
  
  <p><strong>Cliff notes</strong></p>
  
  <ul>
  <li>I'm running a system install of RVM</li>
  <li>I'm deploying via Capistrano</li>
  </ul>
  
  <p>I'm currently dumbfounded.  Basically what's happening is I'm trying to do a deployment via Capistrano and it's failing, hard. Here's the pastie of my deploy.rb and production.rb files (I'm doing a multi-stage environment and omitting the staging.rb file as that's working fine).  <a href="http://pastie.org/2523675" rel="nofollow">http://pastie.org/2523675</a></p>
  
  <p>Here is the output of <code>cap production deploy:setup</code>:</p>
  
  <pre><code>$ cap production deploy:setup
    * executing `production'
      triggering start callbacks for `deploy:setup'
    * executing `multistage:ensure'
    * executing `deploy:setup'
    * executing "sudo -p 'sudo password: ' mkdir -p /var/www/html/dataserver /var/www/html/dataserver/releases /var/www/html/dataserver/shared /var/www/html/dataserver/shared/system /var/www/html/dataserver/shared/log /var/www/html/dataserver/shared/pids"
      servers: ["omitted"]
  Password: 
      [omitted] executing command
  *** [err :: omitted] sudo
  *** [err :: omitted] :
  *** [err :: omitted] sorry, you must have a tty to run sudo
  *** [err :: omitted] 
      command finished in 60ms
  failed: "sh -c 'sudo -p '\\''sudo password: '\\'' mkdir -p /var/www/html/dataserver /var/www/html/dataserver/releases /var/www/html/dataserver/shared /var/www/html/dataserver/shared/system /var/www/html/dataserver/shared/log /var/www/html/dataserver/shared/pids'" on omitted
  </code></pre>
  
  <p>Now, I get why this is happening.  I'm remotely trying to use sudo and that's a security no-no.  So, that means I need to use <code>default_run_options[:pty] = true</code>.  So I uncomment it and run <code>cap production deploy:setup</code> again and all runs nice and dandy.  The directory structure for the deployment get's setup and everything.  Awesome.  Now I try to run <code>cap production deploy</code> and here's the output:</p>
  
  <pre><code>$ cap production deploy
    * executing `production'
      triggering start callbacks for `deploy'
    * executing `multistage:ensure'
    * executing `deploy'
    * executing `deploy:update'
   ** transaction: start
    * executing `deploy:update_code'
      updating the cached checkout on all servers
      executing locally: "hg log -r default --template '{node|short}'"
      command finished in 52ms
    * executing "if [ -d /var/www/html/dataserver/shared/cached-copy ]; then hg pull --repository /var/www/html/dataserver/shared/cached-copy https://cowfish.unh.edu/hg/DataServerApp &amp;&amp; hg update --repository /var/www/html/dataserver/shared/cached-copy --clean 6979cec4fc00; else hg clone --noupdate https://cowfish.unh.edu/hg/DataServerApp /var/www/html/dataserver/shared/cached-copy &amp;&amp; hg update --repository /var/www/html/dataserver/shared/cached-copy --clean 6979cec4fc00; fi"
      servers: ["omitted"]
  Password: 
      [omitted] executing command
   ** [omitted :: out] warning: certificate for omitted can't be verified (Python too old)
   ** [omitted :: out] http authorization required
   ** [omitted :: out] realm: Mercurial Project Repository
   ** [omitted :: out] user:
   ** [omitted :: out] d
   ** [omitted :: out] e
   ** [omitted :: out] p
   ** [omitted :: out] l
   ** [omitted :: out] o
   ** [omitted :: out] y
   ** [omitted :: out]
   ** [omitted :: out] password:
   ** [omitted :: out]
   ** [omitted :: out] warning: certificate for omitted can't be verified (Python too old)
   ** [omitted :: out] warning: certificate for omitted can't be verified (Python too old)
   ** [omitted :: out] warning: certificate for omitted can't be verified (Python too old)
   ** [omitted :: out] requesting all changes
   ** [omitted :: out] warning: certificate for omitted can't be verified (Python too old)
   ** [omitted :: out] warning: certificate for omitted can't be verified (Python too old)
   ** [omitted :: out] adding changesets
   ** [omitted :: out] adding manifests
   ** adding file changes
   ** [omitted :: out] added 11 changesets with 187 changes to 125 files
   ** [omitted :: out] warning: certificate for omitted can't be verified (Python too old)
   ** [omitted :: out] warning: certificate for omitted can't be verified (Python too old)
   ** [omitted :: out] 114 files updated, 0 files merged, 0 files removed, 0 files unresolved
      command finished in 1332ms
      copying the cached version to /var/www/html/dataserver/releases/20110912233835
    * executing "cp -RPp /var/www/html/dataserver/shared/cached-copy /var/www/html/dataserver/releases/20110912233835 &amp;&amp; (echo 6979cec4fc00 &gt; /var/www/html/dataserver/releases/20110912233835/REVISION)"
      servers: ["omitted"]
      [omitted] executing command
      command finished in 23ms
    * executing `deploy:finalize_update'
    * executing "chmod -R g+w /var/www/html/dataserver/releases/20110912233835"
      servers: ["omitted"]
      [omitted] executing command
      command finished in 7ms
    * executing "rm -rf /var/www/html/dataserver/releases/20110912233835/log /var/www/html/dataserver/releases/20110912233835/public/system /var/www/html/dataserver/releases/20110912233835/tmp/pids &amp;&amp;\\\n      mkdir -p /var/www/html/dataserver/releases/20110912233835/public &amp;&amp;\\\n      mkdir -p /var/www/html/dataserver/releases/20110912233835/tmp &amp;&amp;\\\n      ln -s /var/www/html/dataserver/shared/log /var/www/html/dataserver/releases/20110912233835/log &amp;&amp;\\\n      ln -s /var/www/html/dataserver/shared/system /var/www/html/dataserver/releases/20110912233835/public/system &amp;&amp;\\\n      ln -s /var/www/html/dataserver/shared/pids /var/www/html/dataserver/releases/20110912233835/tmp/pids"
      servers: ["omitted"]
      [omitted] executing command
      command finished in 8ms
    * executing "find /var/www/html/dataserver/releases/20110912233835/public/images /var/www/html/dataserver/releases/20110912233835/public/stylesheets /var/www/html/dataserver/releases/20110912233835/public/javascripts -exec touch -t 201109122338.35 {} ';'; true"
      servers: ["omitted"]
      [omitted] executing command
   ** [out :: omitted] find: /var/www/html/dataserver/releases/20110912233835/public/images: No such file or directory
   ** [out :: omitted] find: /var/www/html/dataserver/releases/20110912233835/public/stylesheets: No such file or directory
   ** [out :: omitted] find: /var/www/html/dataserver/releases/20110912233835/public/javascripts: No such file or directory
      command finished in 6ms
    * executing `deploy:symlink'
    * executing "rm -f /var/www/html/dataserver/current &amp;&amp; ln -s /var/www/html/dataserver/releases/20110912233835 /var/www/html/dataserver/current"
      servers: ["omitted"]
      [omitted] executing command
      command finished in 6ms
   ** transaction: commit
    * executing `deploy:restart'
    * executing "sudo -p 'sudo password: ' touch /var/www/html/dataserver/current/tmp/restart.txt"
      servers: ["omitted"]
      [omitted] executing command
      command finished in 10ms
      triggering after callbacks for `deploy'
    * executing `deploy:migrate'
      triggering before callbacks for `deploy:migrate'
    * executing `sqlite3:link_configuration_file'
    * executing "ln -nsf /var/www/html/dataserver/shared/sqlite_config.yml /var/www/html/dataserver/releases/20110912233835/config/database.yml"
      servers: ["omitted"]
      [omitted] executing command
      command finished in 6ms
    * executing "cd /var/www/html/dataserver/releases/20110912233835 &amp;&amp; rake RAILS_ENV=production  db:migrate"
      servers: ["omitted"]
      [omitted] executing command
   ** [out :: omitted] sh: rake: command not found
      command finished in 5ms
  failed: "sh -c 'cd /var/www/html/dataserver/releases/20110912233835 &amp;&amp; rake RAILS_ENV=production  db:migrate'" on omitted
  </code></pre>
  
  <p>So it appears that whatever environment it has loaded cannot see <code>rake</code>.  To see what is loaded in the path I created a deploy task, here's the output:</p>
  
  <pre><code>$ cap production test_path
    * executing `production'
      triggering start callbacks for `test_path'
    * executing `multistage:ensure'
    * executing `test_path'
    * executing "echo $PATH"
      servers: ["omitted"]
  Password: 
      [omitted] executing command
   ** [out :: omitted] /usr/local/bin:/bin:/usr/bin
      command finished in 11ms
    * executing "echo $rvm_path"
      servers: ["omitted"]
      [omitted] executing command
   ** [out :: omitted] 
      command finished in 5ms
  </code></pre>
  
  <p>Now you'll notice that path is definitely not correct.  For the user that is establishing the session, it should be the following:</p>
  
  <pre><code>/usr/local/rvm/gems/ruby-1.9.2-p290@rails31dataserver/bin:/usr/local/rvm/gems/ruby-1.9.2-p290@global/bin:/usr/local/rvm/rubies/ruby-1.9.2-p290/bin:/usr/local/rvm/bin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/lpeabody/bin
  </code></pre>
  
  <p>And again, I get why this isn't working.  It's because since I'm running <code>default_run_options[:pty] = true</code> my environment is getting loaded, at least that is my current theory.  So, after all that, I'm left with the conclusion - <strong>I need to somehow get my environment loaded so that rake can run without enabling sudo remotely.</strong></p>
  
  <p>Now, I know I'm not the only person to ever have needed to do this, so any pointers or tips will be greatly appreciated.  I've racked my brain for the greater portion of 8 hours now and I need to get this app deployed ASAP.</p>