osx - Can't run PHP executable script. OS X 10.6

02
2013-08
  • r4.

    Execting php-scripts from the command line now does not work. When executing a script starting with #!/usr/bin/env php. I get this error message:

    env: php: No such file or directory.

    So it seems I can not run php as an executable script with /usr/bin/php.

    I think what's causing my problem is that I followed these instructions for allowing MAMP’s copy of PHP to run from the terminal.

    sudo mv /usr/bin/php /usr/bin/php-old
    

    appears not to be working. php is not renamed.

    If I should add something to my path. What should I add? When running echo $PATH:

    /opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/usr/local/git/bin:/usr/X11/bin 
    

    Btw (don't know if it is related): MySQL is running but I can't access http://localhost/ when running:

    sudo apachectl start
    
  • Answers
  • slhck

    That's why you should never mess around with /usr/bin, but keep local installations of binaries.

    If you did the following

    sudo mv /usr/bin/php /usr/bin/php-old
    

    you will be able to revert by just doing

    sudo mv /usr/bin/php-old /usr/bin/php
    

    then /usr/bin/env php should work.


    If you decide to continue with the MAMP installation, you can of course move your default php to php-old. But then you also need to execute the following after moving php to php-old:

    sudo ln -s /Applications/MAMP/bin/php5/bin/php /usr/bin/php
    

    in order to have the php shipped with MAMP available under /usr/bin/phpagain.

    But I can't promise that will work as expected at all. Again, you shouldn't move stuff in /usr/bin.

  • Daniel Beck

    You can just use the path of your PHP installation directly in the script.

    #!/Applications/wherever/it/is/you/installed/php
    

  • Related Question

    osx - Where does $PATH get set in OS X 10.6 Snow Leopard?
  • Andrew

    I type echo $PATH on the command line and get

    /opt/local/bin:/opt/local/sbin:/Users/andrew/bin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/pear/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/opt/local/bin:/usr/local/git/bin

    I'm wondering where this is getting set since my .bash_login file is empty.

    I'm particularly concerned that, after installing MacPorts, it installed a bunch of junk in /opt. I don't think that directory even exists in a normal Mac OS X install.

    Update: Thanks to jtimberman for correcting my echo $PATH statement


  • Related Answers
  • slhck

    When bash starts it reads the following files every time you login. For the purposes of OS X, this means every time you open a new Terminal window.

    /etc/profile
    ~/.bash_profile
    ~/.bash_login   (if .bash_profile does not exist)
    ~/.profile      (if .bash_login does not exist)
    

    When you start a new shell by typing bash on the command line, it reads .bashrc

    Finally, OS X also uses ~/.MacOSX/environment.plist to set more environment variables, including paths if necessary.


    /opt/local/bin etc. are added in ~/.tcshrc by MacPorts. Also be sure to look in ~/.cshrc.

  • leanne

    Take a look at the file /etc/paths, which is used by /usr/libexec/path_helper, which is used by /etc/profile. For MacPorts, use "sudo" to append "/opt/local/bin" to the end of the /etc/paths file and reopen the terminal window.

  • Vidar Kongsli

    An eye-opener for me was that /etc/profile calls the path_helper tool to generate the PATH. See the man pages: http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/man8/path_helper.8.html

  • jtimberman

    To show your path, echo $PATH.

    echo $PATH
    

    To set your path, edit ~/.bash_profile, not ~/.bash_login.

  • ricbax

    Actually it is stored in your .profile file instead of .bash_login and it is common that MacPorts will use this instead of the .bash_login file.

    Also The /opt directory is usually created by MacPorts and it stores its files in this folder.

  • Keith Bentrup

    There's also the path as determined by ssh.

    Compare echo $PATH to ssh localhost 'echo $PATH'. Since ssh does not read /etc/profile, /usr/libexec/path_helper doesn't run and thus /etc/paths is skipped. Now try ssh localhost 'source /etc/profile; echo $PATH'. The paths should be closer. The remaining differences will likely be due to path modification in your .bash_profile (which is also skipped by ssh) and .bashrc (which is read by ssh).

    If you want your ssh path to be similar to your normal terminal path, you could add source /etc/profile to your .bashrc.

  • A.B

    Regarding docs for /usr/libexec/path_helper utility, initial components for $PATH was taken from /etc/paths and by default looks like

    /usr/bin
    /bin
    /usr/sbin
    /sbin
    /usr/local/bin
    

    for OS-X Snow Leopard

  • fuzzybee

    It could be defined in either:

    • System variables - /etc/paths
    • User variables - see @Steve Folly's explanation