Question

Composer install not working with user_data setup script

Hi,

I’m trying to setup an automated script that should basically be able to automatically clone a project from my Github and install its dependencies via Composer.

Here is a simple version of the user_data that I’m sending through the API (supposing that all needed packages have correctly been installed):

#!/bin/bash

[...]
git clone git@github.com[...]
/usr/bin/php -d memory_limit=-1 composer.phar install

The problem is that the composer install command does nothing.

Everything else from my script works. I redirect all my stdin and stderr to a log file while my script is running, but when it comes to this command, nothing appears in this file, just like if it didn’t run.

The weird thing is that when I perform this command manually through ssh, it works as expected and install my dependencies correctly.

Please note that I already tried many alternatives, like installing composer via apt or via the official installer with that kind of script, without success:

#!/bin/bash

[...]
git clone git@github.com[...]
apt-get -y install composer
composer install

Does anyone have any idea to solve this issue?

Greetings,


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Accepted Answer

I finally found my solution

It turns out that while running the composer-setup.php through a setup script, this message will appear:

The HOME or COMPOSER_HOME environment variable must be set for 
composer to run correctly

So the solution is simple, according to this answer on StackOverflow, simply add this line after completing the composer setup: export COMPOSER_HOME="$HOME/.config/composer";

The complete setup script now looks like this:


#!/bin/bash

[...]
apt-get -y install curl php-cli php-mbstring git unzip
curl -sS https://getcomposer.org/installer -o composer-setup.php
php composer-setup.php --install-dir=/usr/local/bin --filename=composer
export COMPOSER_HOME="$HOME/.config/composer";
git clone git@github.com[...] html/
cd html/
php /usr/local/bin/composer install

Hope it will help you if you are facing the same issue.

for my solution i do :

apt-get -y install  php-cli php-mbstring  composer -y
export COMPOSER_HOME="$HOME/.config/composer";
curl -sS https://getcomposer.org/installer -o composer-setup.php
php composer-setup.php --install-dir=/usr/local/bin --filename=composer

echo "Symlink /usr/bin/composer so that both refer to same composer"
ln -sfn /usr/local/bin/composer /usr/bin/composer
KFSys
Site Moderator
Site Moderator badge
October 16, 2019

Hi @MartinMarx,

I see you mentioned you’ve tried to install composer via the normal ways but did you try the following :

download the Composer installer

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"

Install PHP Composer

Installing PHP Composer requires curl, unzip, and a few other utilities. Install them by entering the following:

apt-get install curl php-cli php-mbstring git unzip

Set Composer as a command accessible from the whole system

php composer-setup.php --install-dir=/usr/local/bin --filename=composer

This should be enough for the installation. Please excuse me if you actually tried these steps.

Regards, KDSys

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel