1

I have installed and working PHP53 from source.

PATH is set in ~/.profile like this: export PATH=$PATH:/usr/local/php53/bin

$ php -v # works good

BUT:

$ sudo php -v # sudo: php: command not found

I needed to run sudo composer self-update, and it gave me a message:

/usr/bin/env: php: No such file or directory

$ composer -v # works good (installed globally by this manual https://getcomposer.org/doc/00-intro.md#globally

In short:

curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer

I have similar problem with Apache22 apachectl, but I am using full path to run with sudo. But it is a workaround, and I'd like to know, how to make it work in common.

The problem is that $ sudo php -v and sudo composer self-update DOES NOT WORK.

UPDATE: I added to /root/.profile new path to PHP53:

# ~/.profile: executed by Bourne-compatible login shells.


export PATH=$PATH:/usr/local/php53/bin


if [ "$BASH" ]; then
  if [ -f ~/.bashrc ]; then
    . ~/.bashrc
  fi
fi

mesg n

Then I rebooted OS, and here are results for various tests:

$ echo $PATH # /usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/apache2_2/bin:/usr/local/php53/bin
$ php -v # works, as before
$ sudo php -v # "sudo: php: command not found"


$ sudo su
# echo $PATH # /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games , i.e. "export PATH=$PATH:/usr/local/php53/bin" did not have effect on $PATH.
# php -v # "The program 'php' is currently not installed. You can install it by typing:
apt-get install php5-cli"
# . /root/.profile 
# php -v # "PHP 5.3.29 (cli) (built: Sep 24 2015 13:05:17) 
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2014 Zend Technologies

BUT:

$ sudo php -v # still, "sudo: php: command not found"

As a little relief,

$ sudo /usr/local/php53/bin/php -v # is working 
PHP 5.3.29 (cli) (built: Sep 24 2015 13:05:17) 
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2014 Zend Technologies

Seems like we have at least two problems:

  1. export $PATH in /root/.profile does not have effect (tested after OS reboot!)
  2. even when I do # . /root/.profile, sudo php -v still does not work.

OS: Ubuntu 12.04 with last updates.

1 Answer 1

3

This is down to how sudo defines its PATH. Basically, it will either take the PATH of the user calling it, or if so configured, it will read what is set in the secure_path variable of /etc/sudoers.

So, you have two choices:

  1. Add the directory where the php binary is located to the secure path. Run sudo visudo, find this line:

    Defaults   secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:$
    

    and change it to

    Defaults   secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/php53/bin:$
    
  2. Simply comment out the secure_path line and have sudo share the same path as the invoking user. This is not the safest of choices of course, but it's up to you. To do this, run sudo visudo and add a # to the beginning of the line:

    # Defaults   secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:$
    

Save the file and exit visudo and you should be able to run sudo php -v as expected.

2
  • terdon, +1, thanks! Tested both options, decided to stick with second one for now.
    – bannndi
    Sep 26, 2015 at 19:49
  • need to open a lots of link to find the solution then came to this answer. you're just saved my day! thank you! I prefer solution #1
    – adadion
    Dec 14, 2016 at 18:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .