Search code examples
ruby-on-railsrubybundler

Cannot run bundle command on remote server from local computer


I've create a script that copies a tar file to a remote server, do some stuffs then runs:

[..]

kill $(lsof -i :3000 -t)

# Bundle up
cd $DIR && \
  bundle install && \
  RAILS_ENV=production bundle exec rake assets:precompile && \
  rails s -e production -p 3000 -d

Should I run this on the remote server directly ./my-script.sh param, all is well. It does not work when I call this script from my local computer. It complains about

./my-script.sh: line 18: bundle: command not found

When I ran which bundle on remote server I got back:

/usr/share/rvm/gems/ruby-3.0.0/bin/bundle

Using the above in my script I get tons of errors. How to get pass that error when calling script from local computer?


Solution

  • You should set all ruby and bundler env variables to use bundle exec

    if rvm is used you should load it:

    [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
    

    try to know all env for rvm on your server under ruby user by: rvm env

    it runs like ssh example.com < ./bundle.sh

    #!/bin/bash +ex
    # bundle.sh
    
    export PATH="/home/user/.rvm/gems/ruby-2.6.4/bin:/home/user/.rvm/gems/ruby-2.6.4@global/bin:/home/user/.rvm/rubies/ruby-2.6.4/bin:$PATH"
    export GEM_HOME='/home/user/.rvm/gems/ruby-2.6.4'
    export GEM_PATH='/home/user/.rvm/gems/ruby-2.6.4:/home/user/.rvm/gems/ruby-2.6.4@global'
    export MY_RUBY_HOME='/home/user/.rvm/rubies/ruby-2.6.4'
    export IRBRC='/home/user/.rvm/rubies/ruby-2.6.4/.irbrc'
    unset MAGLEV_HOME
    unset RBXOPT
    export RUBY_VERSION='ruby-2.6.4'
    source "$HOME/.rvm/scripts/rvm"
    
    export RAILS_ENV=production
    
    cd /path/ && bundle exec rails db:migrate:status
    cd $DIR && \
      bundle install && \
      bundle exec rake assets:precompile && \
      rails s -e production -p 3000 -d