title: How to use Bundler with non-interactive remote SSH sessions
body: <p>I am having an issue running a command remotely via a non-interactive SSH session to a Ubuntu 14.04. server.</p>
  
  <p>I want to run a bundle command to execute a Jekyll build. If I SSH into an interactive shell on my server using <code>ssh username@address</code>, and run:</p>
  
  <pre><code>cd /jekyll/site/dir ; /root/gems/bin/bundle exec jekyll build --incremental
  </code></pre>
  
  <p>It works successfully. However, If I try to run this from my local machine:</p>
  
  <pre><code>ssh username@address "cd /jekyll/site/dir ; /root/gems/bin/bundle exec jekyll build --incremental"
  </code></pre>
  
  <p>it fails. I have seen other issues, such as </p>
  
  <ul>
  <li>"<a href="https://stackoverflow.com/questions/10562722/command-not-found-via-ssh-with-single-command-found-after-connecting-to-termina">Command not found via ssh with single command, found after connecting to terminal</a>" </li>
  <li>"<a href="https://stackoverflow.com/questions/216202/why-does-an-ssh-remote-command-get-fewer-environment-variables-then-when-run-man">Why does an SSH remote command get fewer environment variables then when run manually?</a>" </li>
  </ul>
  
  <p>which explain that, in a non-interactive session, the environment might not be set up in the same way as an interactive session by default. </p>
  
  <p>I tried adding some sourcing to the remote command to account for this:</p>
  
  <pre><code>ssh username@address "source /root/.profile ; source /root/.bashrc ; cd /jekyll/site/dir ; /root/gems/bin/bundle exec jekyll build --incremental"
  </code></pre>
  
  <p>Still, the <code>bundle</code> command doesn't work correctly. The actual error that I get is:</p>
  
  <pre><code>/root/gems/bin/bundle:15:in `&lt;main&gt;': undefined method `b' for "exec":String (NoMethodError)
      from /root/gems/bin/ruby_executable_hooks:24:in `eval'
      from /root/gems/bin/ruby_executable_hooks:24:in `&lt;main&gt;'
  </code></pre>
  
  <p>Thus, the issue seems to have something to do with bundle/RVM, and perhaps there's some environment thing that I am still missing. I tried searching the web to determine why that error occurs, but I have not been able to pinpoint the issue.</p>
  
  <p>If I <code>cd</code> into the Jekyll site directory in an interactive session, it indicates that</p>
  
  <pre><code>"RVM used your Gemfile for selecting Ruby, it is all fine - Heroku does that too,"
  </code></pre>
  
  <p>and so on. Perhaps this has something to do with the way that RVM/Ruby/whatever overrides the <code>cd</code> command to trigger that process, and maybe it isn't doing the same thing in the non-interactive session?</p>