2

I try to run from PHP my ruby script. When I run it from command line it's OK but when I run it from PHP code I can't use gems.

here is my PHP script

$rubyBin = '/usr/local/rvm/rubies/ruby-2.1.1/bin/ruby';
echo (shell_exec("$rubyBin server.rb 2>&1"));

and this is a response from server

file exists/usr/local/rvm/rubies/ruby-2.1.1/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- pivotal-tracker (LoadError)
    from /usr/local/rvm/rubies/ruby-2.1.1/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from server.rb:5:in `<main>'

but require 'rubygems' runs OK

Sergei Tsibulchenko
  • 297
  • 1
  • 5
  • 17

1 Answers1

0

The output shows your environment is using Ruby Version Manager (rvm). Your PHP environment is probably using a different Ruby version and/or Gemset than your commandline enviroment.

The solution is to wrap your Ruby script in a RVM wrapper by issuing command like the following:

rvm wrapper 2.1.1@commandline_gemet php server.rb

Be sure to specify the correct Ruby version (see rvm list), Gemset (see rvm gemset list) and path to server.rb This creates a php_server exectubale in /usr/local/rvm/bin. The wrapper prepares the requested Ruby environment for your script, so if you invoke that from your PHP script everything should work as expected.

Martijn
  • 1,662
  • 15
  • 21