Home » Php » linux – Tell Composer to use Different PHP Version

linux – Tell Composer to use Different PHP Version

Posted by: admin April 23, 2020 Leave a comment

Questions:

I’ve been stuck at this for a few days. I’m using 1and1 hosting, and they have their PHP set up a bit weird.

If I use just php composer.phar install, then I’m using PHP 4.4.6, which is horribly insufficient. However, I can run php5.5 composer.phar install, get a little bit further, but it still fails because somewhere along the line, PHP is called again, but it fails, as it’s using 4.4.6.

Is there any way to tell Composer to use the php5.5 command? Has anyone successfully gotten Composer configured on 1and1 hosting?

I’m trying to get Laravel up and running (which uses Composer). I’ve been able to do it on my GoDaddy domain, but not 1and1.

How to&Answers:

Ubuntu 18.04 case … this run for me.

/usr/bin/php7.1 /usr/local/bin/composer update

Answer:

On xubuntu I had php 7.2 as default. But needed to run composer with php 5.6.

So this worked for me:

php5.6 /usr/bin/composer

Answer:

Just add below code to your composer.json file to set different php version.

"config": {

    "platform": {

        "php": "7.1.3"

    }

}

Answer:

Try this approach

ea-php72 /opt/cpanel/composer/bin/composer

Answer:

I too have a shared hosting account on 1&1 (ionos) and here’s what I have had to do:

if you login as the root ssh account, you can create a ~/.bash_profile and add

alias php="php7.1"
alias composer="php7.1 composer.phar"

to it so that you can now use the commands you would normally use and it just works. (put composer.phar in your project folder root)

Then, make sure your laravel composer.lock file from your dev machine gets up to your project folder on 1and1 and run

composer install

Using this tip from @tobymackenzie: on shared hosts you really should just run composer install (not update!) from a composer.lock file you created on your own machine. That way the memory usage remains very low.

I still had STDIN issues with artisan commands so make sure you change the .env file to

APP_ENV=local

because having it set to production throws infinite STDIN warnings as it waits for you to type yes. At least on my account it does.

Hope this helps somebody.

Answer:

You could change your PATH to override the php version.

PATH="/home/user/bin:$PATH"

In /home/user/bin (or any other directory really) make a symlink named php to the php5 binary.

Another option, use a shell alias:

alias php="/path/to/php5"

Answer:

composer is a PHP binary script and it’s using Unix Shebang to define which interpreter for executing.

[email protected]:~# head /usr/local/bin/composer -n 5

#!/usr/bin/env php
<?php
/*
 * This file is part of Composer.
 *


/usr/bin/php7.1 /usr/local/bin/composer update

So if you’re running composer without any specific option, it will use your PHP version at /usr/bin/env php (which is your default PHP)

We can have many versions of PHP inside your Linux and you can flexible use which PHP version for executing as you want like this way:

[email protected]:~# /usr/bin/php7.1 /usr/local/bin/composer update

Or just

[email protected]:~# php7.1 /usr/local/bin/composer
[email protected]:~# php7.2 /usr/local/bin/composer
[email protected]:~# php7.3 composer
...

To check & find all installed PHP packages, goto /usr/bin/

[email protected]:~# ls /usr/bin/ | grep php
php
php5.6
php7.1
php7.2
php7.3
php-config
php-config7.1
phpdbg
phpdbg5.6
phpize
phpize7.1

Answer:

php5.5 composer.phar install fails because your composer.json is telling it to run php and not php5.5, edit the file composer.json replace the php scripts references to php5.5. Example:

From:

"post-root-package-install": [
    "php -r \"copy('.env.example', '.env');\""
],

To:

"post-root-package-install": [
    "php5.5 -r \"copy('.env.example', '.env');\""
],

Even when doing this, 1and1 has a memory limit for scripts execution so php5.5 composer.phar install won’t fail but it won’t complete its execution either. You can still run the scripts post install manually.

I have deployed Laravel webapps to 1and1 without commit vendor directory, following this guide: Deploy Laravel Webapp to 1and1

Answer:

Know this question is a bit old… but if you pull down composer into your app root:

https://getcomposer.org/download/

Instead of relying on global composer, then you can run:

php56 composer.phar {your command}

or I believe newer homstead versions would be like:

php7.1 composer.phar {your command}

https://laracasts.com/discuss/channels/laravel/run-composer-commands-with-different-php-versions

Answer:

You could try and copy the php and php-config files into /usr/local/bin instead. That path has a higher order of precedence and should override the /usr/bin folder without needing to disable SIP. That is what I did.