How to use composer with Plesk (use a different version of PHP to run Composer)

With a Plesk based server, the default installation of composer will use the System installed PHP version, not any of the installed by Plesk. Although Composer requires at least PHP 5.3.2+, the problem comes out when you try to install dependencies in a project that requires a specific version of PHP. For example, if you have a project that needs at least PHP 7.2 and your default PHP installation of your system is PHP 7.1, then the warning of composer that says that your system doesn't satisfies all the dependencies because you are running composer with PHP 7.1, but you need PHP 7.2. This is very problematic, knowing that you have as well PHP 7.2 installed with Plesk !

Normally, every developer would be able to run the commands from the command line using the PHP version of the system with the following commands:

# For example install dependencies
composer install

# Or update them 
composer update

However, if you are working in a Plesk server and you need to run composer with another version, you will need to follow some extra steps in order to run it without any issue.

Running composer with another Plesk PHP version

As first you need to know which PHP versions are installed on your Plesk based server, so run the following command to retrieve a short review about all the versions:

plesk bin php_handler --list

This will generate an output similar to:

Plesk PHP CLI Versions

In this server we have multiple PHP versions, whose paths are:

/opt/plesk/php/5.2/bin/php
/opt/plesk/php/5.3/bin/php
/opt/plesk/php/5.4/bin/php
/opt/plesk/php/5.6/bin/php
/opt/plesk/php/7.1/bin/php
/opt/plesk/php/7.2/bin/php

So now that you know which PHP versions are available, you can run the composer.phar file from the command line using the binary. Before you need to know however which is the composer.phar location in Plesk. According to the architecture of your System, you will find the file at:

# For 32-bits systems
/usr/lib/plesk-9.0/composer.phar

# For 64-bits systems
/usr/lib64/plesk-9.0/composer.phar

After knowing this 2 factors, you can now run the composer commands using the following trick. Just cast the PHP Cli binary and provide as second argument the file to run that in this case is the composer.phar file, then add the instructions that you would normally use:

/opt/plesk/php/<PHP VERSION>/bin/php /usr/lib/plesk-9.0/composer.phar [options] [arguments]

For example with the most common tasks:

# Running "composer install" with PHP 7.2
/opt/plesk/php/7.2/bin/php /usr/lib/plesk-9.0/composer.phar install

# Running "composer update" with PHP 5.6
/opt/plesk/php/5.6/bin/php /usr/lib/plesk-9.0/composer.phar update

For more information, visit the question on the Plesk support website here.

Happy coding !

This could interest you
Become a more social person