# Calling Ruby from PHP fails on "requires 'mysql'"

**URL:** <https://www.ruby-forum.com/t/calling-ruby-from-php-fails-on-requires-mysql/115891>\
**Category:** Ruby\
**Created:** [September 29, 2007, 10:53pm UTC](https://www.ruby-forum.com/t/calling-ruby-from-php-fails-on-requires-mysql/115891 "2007-09-29T22:53:21Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [September 29, 2007, 10:53pm UTC](https://www.ruby-forum.com/t/calling-ruby-from-php-fails-on-requires-mysql/115891/1 "2007-09-29T22:53:21Z")

</div>

I’m calling Ruby from PHP using system().  
This works fine until I add “require ‘mysql’”.

I think it’s likely missing a path for the Ruby MySQL gem.

I can call the same script successfully from crontab by adding the line  
$LOAD\_PATH \<\< File.expand\_path(File.dirname( **FILE** ))

Any experience or ideas?

cheers

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [September 30, 2007, 12:00am UTC](https://www.ruby-forum.com/t/calling-ruby-from-php-fails-on-requires-mysql/115891/2 "2007-09-30T00:00:18Z")

</div>

On 9/29/07, Mark M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Any experience or ideas?  
> If the problem is in gems, try adding require “rubygems” before require  
> “mysql”.  
> (the same may be achieved by setting RUBYOP«=rubygems in the  
> environment)

If it doesn’t help, please post the exact error message with exception  
trace.

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [September 30, 2007, 12:27am UTC](https://www.ruby-forum.com/t/calling-ruby-from-php-fails-on-requires-mysql/115891/3 "2007-09-30T00:27:05Z")

</div>

Thanks Jano,

I am already including rubygems, here’s my requires list…

$LOAD\_PATH \<\< File.expand\_path(File.dirname( **FILE** ))  
require ‘rubygems’  
require ‘mysql’

I’m not seeing an error message when I call my Ruby script from PHP with

$s = sprintf(“ruby /Users/mark/scripts/myscript.rb \>  
/Users/mark/scripts/myscript.log &”);  
system($s);

I see the puts statements from my script in the log file, no errors.

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [September 30, 2007, 1:33am UTC](https://www.ruby-forum.com/t/calling-ruby-from-php-fails-on-requires-mysql/115891/4 "2007-09-30T01:33:51Z")

</div>

Execution of my script ends at “require ‘mysql’”.

puts statements before it are printed to the log file, then nothing.

How can I see the error there?

thanks again

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [September 30, 2007, 9:01pm UTC](https://www.ruby-forum.com/t/calling-ruby-from-php-fails-on-requires-mysql/115891/5 "2007-09-30T21:01:54Z")

</div>

On 9/30/07, Mark M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Execution of my script ends at “require ‘mysql’”.
> 
> puts statements before it are printed to the log file, then nothing.
> 
> How can I see the error there?

1. try adding -w switch to ruby to see the warnings (ruby -w …)
2. try saving stderr as well : (… 2\>/Users/…/myscript.err)
3. I’m not sure what will that & do – especially what will happen if  
the calling php script stops.

Jano

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [October 1, 2007, 9:54pm UTC](https://www.ruby-forum.com/t/calling-ruby-from-php-fails-on-requires-mysql/115891/6 "2007-10-01T21:54:26Z")

</div>

On 10/1/07, Mark M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> `gem\_original\_require’: No such file to load – mysql (LoadError)  
> I’m able to call the same script from crontab by adding  
> $LOAD\_PATH \<\< File.expand\_path(File.dirname( **FILE** ))
> 
> Any ideas on massaging the path?
> 
> cheers

What came to my mind is whether are you using the same ruby in those two  
cases?

Other than that, 1. look up where the mysql lib/gem resides, 2. print  
out $LOAD\_PATH in both cases and compare.

You could do system(“gem list”) as well to check whether gems can find  
the mysql gem.

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [October 1, 2007, 9:35pm UTC](https://www.ruby-forum.com/t/calling-ruby-from-php-fails-on-requires-mysql/115891/7 "2007-10-01T21:35:06Z")

</div>

Thanks again!

Saving the stderr gave me

/usr/lib/ruby/site\_ruby/1.8/rubygems/custom\_require.rb:27:in  
`gem_original_require': No such file to load -- mysql (LoadError) from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in`require’  
from /Users/mark/scripts/myscript.rb:31

Line 31 is  
require ‘mysql’

So, looks like it can’t find the gem in the path.

I’m able to call the same script from crontab by adding  
$LOAD\_PATH \<\< File.expand\_path(File.dirname( **FILE** ))

Any ideas on massaging the path?

cheers

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [October 2, 2007, 12:17am UTC](https://www.ruby-forum.com/t/calling-ruby-from-php-fails-on-requires-mysql/115891/8 "2007-10-02T00:17:10Z")

</div>

Ok, getting close.

The Ruby version called from PHP is indeed different from the Ruby  
called from command line or crontab…

from PHP  
/usr/bin/ruby  
ruby 1.8.2 (2004-12-25) [universal-darwin8.0]

otherwise  
/usr/local/bin/ruby  
ruby 1.8.6 (2007-03-13 patchlevel 0) [i686-darwin8.8.2]

I tried adding the path to a system call but it doesn’t help  
the ruby call and I don’t get anything written to the logs…

$s = sprintf(“echo $PATH \> /Users/…/path0.log 2\> /Users/…/path1.log”);  
system($s);  
$s = sprintf(“export PATH=/usr/local/bin:/usr/local/mysql/bin:$PATH”);  
system($s);  
$s = sprintf(“echo $PATH \> /Users/…/path2.log 2\> /Users/…/path3.log”);  
system($s);

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [October 2, 2007, 1:32am UTC](https://www.ruby-forum.com/t/calling-ruby-from-php-fails-on-requires-mysql/115891/9 "2007-10-02T01:32:18Z")

</div>

On Oct 1, 2007, at 5:17 PM, Mark M. wrote:

> system($s);  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

\*nix trouble!

PHP probably doesn’t run as your user account, so it might use a  
different PATH. The path you’re using for Ruby is for YOUR user’s  
shell login.  
one way to verify it is to check the user id and process id. you can  
use tail for that.  
PHP probably uses something set in /env  
You would have the same path to Ruby if you didn’t have the path  
variable set by the dot file in your user account directory when you  
do a shell login.  
PHP often runs as whatever Apache runs as for user id, and it’s not  
your user id.  
The PHP script may be owned by you, but the process is owned by PHP  
or Apache.  
The PHP CLI works differently. It runs under your uid, just as Ruby  
would.  
Anything run by Apache or another server will have a different uid.
