title: Capistrano/RVM rake not found
body: <p>I'm trying to deploy the first rails application using Capistrano.</p>
  
  <p>I'm getting an error while capistrano tries to run <code>rake db:migrate</code>.
  To be more clear this is the traceback of the exception:</p>
  
  <pre><code>[ip] executing command
  *** [err :: ip] bash: rake: command not found
  command finished in 124ms
  failed: "env RAILS_ENV=production bash -c 'cd /home/deployer/website/current &amp;&amp; rake RAILS_ENV=production  db:migrate'" on ip
  </code></pre>
  
  <p>And this is the <code>config/deploy.rb</code> I'm using actually. On the server I've installed RVM (single user installation).</p>
  
  <pre><code># config/deploy.rb 
  $:.unshift(File.expand_path('./lib', ENV['rvm_path']))
  
  require "rvm/capistrano"
  
  set :scm,             :git
  set :repository,      "git@bitbucket.org:user/project.git"
  set :branch,          "origin/master"
  set :migrate_target,  :current
  set :ssh_options,     { :forward_agent =&gt; true }
  set :rails_env,       "production"
  set :deploy_to,       "/home/deployer/website"
  set :normalize_asset_timestamps, false
  set :rvm_type, :user  # Don't use system-wide RVM
  set :rvm_ruby_string, "ruby-1.9.3-p194@global"
  
  set :user,            "deployer"
  set :group,           "staff"
  set :use_sudo,        false
  
  role :web,    "IP"
  role :app,    "IP"
  role :db,     "IP2", :primary =&gt; true
  
  set(:latest_release)  { fetch(:current_path) }
  set(:release_path)    { fetch(:current_path) }
  set(:current_release) { fetch(:current_path) }
  
  set(:current_revision)  { capture("cd #{current_path}; git rev-parse --short HEAD").strip }
  set(:latest_revision)   { capture("cd #{current_path}; git rev-parse --short HEAD").strip }
  set(:previous_revision) { capture("cd #{current_path}; git rev-parse --short HEAD@{1}").strip }
  
  default_environment["RAILS_ENV"] = 'production'
  
  default_run_options[:shell] = 'bash'
  
  namespace :deploy do
    desc "Deploy your application"
    task :default do
      update
      restart
    end
  
    desc "Setup your git-based deployment app"
    task :setup, :except =&gt; { :no_release =&gt; true } do
      dirs = [deploy_to, shared_path]
      dirs += shared_children.map { |d| File.join(shared_path, d) }
      run "#{try_sudo} mkdir -p #{dirs.join(' ')} &amp;&amp; #{try_sudo} chmod g+w #{dirs.join(' ')}"
      run "git clone #{repository} #{current_path}"
    end
  
    task :cold do
      update
      migrate
    end
  
    task :update do
      transaction do
        update_code
      end
    end
  
    desc "Update the deployed code."
    task :update_code, :except =&gt; { :no_release =&gt; true } do
      run "cd #{current_path}; git fetch origin; git reset --hard #{branch}"
      finalize_update
    end
  
    desc "Update the database (overwritten to avoid symlink)"
    task :migrations do
      transaction do
        update_code
      end
      migrate
      restart
    end
  
    task :finalize_update, :except =&gt; { :no_release =&gt; true } do
      run "chmod -R g+w #{latest_release}" if fetch(:group_writable, true)
  
      # mkdir -p is making sure that the directories are there for some SCM's that don't
      # save empty folders
      run &lt;&lt;-CMD
        rm -rf #{latest_release}/log #{latest_release}/public/system #{latest_release}/tmp/pids &amp;&amp;
        mkdir -p #{latest_release}/public &amp;&amp;
        mkdir -p #{latest_release}/tmp &amp;&amp;
        ln -s #{shared_path}/log #{latest_release}/log &amp;&amp;
        ln -s #{shared_path}/system #{latest_release}/public/system &amp;&amp;
        ln -s #{shared_path}/pids #{latest_release}/tmp/pids &amp;&amp;
        ln -sf #{shared_path}/database.yml #{latest_release}/config/database.yml
      CMD
  
      if fetch(:normalize_asset_timestamps, true)
        stamp = Time.now.utc.strftime("%Y%m%d%H%M.%S")
        asset_paths = fetch(:public_children, %w(images stylesheets javascripts)).map { |p| "#{latest_release}/public/#{p}" }.join(" ")
        run "find #{asset_paths} -exec touch -t #{stamp} {} ';'; true", :env =&gt; { "TZ" =&gt; "UTC" }
      end
    end
  
    desc "Zero-downtime restart of Unicorn"
    task :restart, :except =&gt; { :no_release =&gt; true } do
      run "kill -s USR2 `cat /tmp/unicorn.my_site.pid`"
    end
  
    desc "Start unicorn"
    task :start, :except =&gt; { :no_release =&gt; true } do
      run "cd #{current_path} ; bundle exec unicorn_rails -c config/unicorn.rb -D"
    end
  
    desc "Stop unicorn"
    task :stop, :except =&gt; { :no_release =&gt; true } do
      run "kill -s QUIT `cat /tmp/unicorn.my_site.pid`"
    end  
  
    namespace :rollback do
      desc "Moves the repo back to the previous version of HEAD"
      task :repo, :except =&gt; { :no_release =&gt; true } do
        set :branch, "HEAD@{1}"
        deploy.default
      end
  
      desc "Rewrite reflog so HEAD@{1} will continue to point to at the next previous release."
      task :cleanup, :except =&gt; { :no_release =&gt; true } do
        run "cd #{current_path}; git reflog delete --rewrite HEAD@{1}; git reflog delete --rewrite HEAD@{1}"
      end
  
      desc "Rolls back to the previously deployed version."
      task :default do
        rollback.repo
        rollback.cleanup
      end
    end
  end
  
  def run_rake(cmd)
    run "cd #{current_path}; #{rake} #{cmd}"
  end
  </code></pre>
  
  <p>How can I make this run properly?</p>
  
  <p>Of course I already tried to run <code>rake</code> on the server, and it is found properly.</p>
  
  <p>Thanks</p>