7
votes

I run the following commands to install the Composer:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('SHA384', 'composer-setup.php') === 'aa96f26c2b67226a324c27919f1eb05f21c248b987e6195cad9690d5c1ff713d53020a02ac8c217dbf90a7eacc9d141d') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php --install-dir=/usr/local/bin
php -r "unlink('composer-setup.php');"

Afterwards, when I run composer, I see that it is working. However, whenever I run sudo composer, I always receive

Command Not Found.

The output ofthe echo $PATH is:

/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/aws/bin:/home/ec2-user/.local/bin:/home/ec2-user/bin

What can I do to enable composer under sudo?

4

4 Answers

14
votes

First you need to SSH into your EC2 instance

ssh -i ~/.ssh/<your_group_security>.pem ec2-user@<ec2-public-ip>

Run below command on your EC2 instance

$ cd ~
$ sudo curl -sS https://getcomposer.org/installer | sudo php
$ sudo mv composer.phar /usr/local/bin/composer
$ sudo ln -s /usr/local/bin/composer /usr/bin/composer

Then you can run verify composer installation

$ sudo composer install

4
votes

If you can run composer "alone" then you should have composer installed in /usr/local/bin/composer so you can just link it to /usr/bin/composer with

sudo ln -s /usr/local/bin/composer /usr/bin/composer

and then run

sudo composer install
0
votes

Hard to answer questions with limited info.

  1. See if you can run sudo composer.phar. That is default name that composer installs itself into. Unless you rename it it by mv /usr/local/bin/composer.phar /usr/local/bin/composer.
  2. Run ls /usr/local/bin/ to verify that composer got installed. Unless you are root you might not have correct permissions to access /usr/local/bin.
-1
votes

Improving on the previous answers - SSH into terminal & type :

$ cd ~
$ sudo curl -sS https://getcomposer.org/installer | sudo php
$ sudo mv composer.phar /usr/local/bin/composer
$ sudo ln -s /usr/local/bin/composer /usr/bin/composer

If you face an error that says: PHP Fatal error: Uncaught exception 'ErrorException' with message 'proc_open(): fork failed - Cannot allocate memory' in phar

/bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024
/sbin/mkswap /var/swap.1
/sbin/swapon /var/swap.1