181

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.

Boom
  • 2,465
  • 4
  • 19
  • 21
  • 2
    The obvious remedy for this is to ditch 1and1 and get yourself a server to run it on. – John Corry Sep 23 '15 at 23:18
  • I'd love to, but that's not currently an option. This is a server we're using at work, and I can't make that call. I'd love to ditch these guys and use GoDaddy, like I do for my personal domains. – Boom Sep 24 '15 at 15:57

26 Answers26

227

Ubuntu 18.04 case ... this run for me.

/usr/bin/php7.1 /usr/local/bin/composer update
Florin
  • 5,781
  • 2
  • 20
  • 30
  • 2
    I have use sudo /opt/lampp/bin/php /usr/bin/composer install and it works for me Thank you +1 – Bhavesh Prajapati Jun 17 '20 at 10:10
  • 2
    How is this fundamentally different from what the OP did and which did not work for him (php5.5 composer.phar install) because the commands invoked by Composer will still use the default PHP version? Test this yourself, add a "scripts" : "php-version": "php --version" and call e.g. /user/bin/php7.4 /usr/bin/composer php-version. – Sybille Peters Jun 27 '21 at 11:57
  • 2
    @SybillePeters, change your script to be e.g. "php-version": "/user/bin/php7.4 --version" I suppose you could configure your entire machine to use a specific version for `php` using `sudo update-alternatives --set php /usr/bin/php7.4` – Anony Mous Mar 02 '22 at 17:30
  • 1
    f**** crazy solution. Thank you – Shahid Karimi Sep 13 '22 at 08:50
202

Maybe this can't solve exactly your issue but probably it will help others who comes here from web search.

Just run the command to add below code to your composer.json file to set different PHP version:

$ composer config platform.php 8.0.7
"config": {
    "platform": {
        "php": "8.0.7"
    }
}
hakre
  • 193,403
  • 52
  • 435
  • 836
Shaig Khaligli
  • 4,955
  • 5
  • 22
  • 32
  • 44
    I guess this isn't really the answer the OP was looking for, but it matches the title of the question very well and might be of great help to people arriving here from web search as it was to me. – Gregory Cosmo Haun Sep 10 '19 at 00:52
  • 5
    This won't run Composer with a different PHP version, but configure Composer to run the dependency resolver for another PHP version. IMHO, this does not answer the given question properly – Nico Haase Feb 02 '22 at 16:59
  • 1
    This is actually the only way you should do it especially when you have multiple projects running different PHP versions! Thank you! – George G Aug 03 '22 at 01:41
  • this does not work. I put that into my composer.json but composer -vvv about still shows using 8.1 – user6329530 Jun 01 '23 at 09:35
41

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
Darius.V
  • 737
  • 1
  • 13
  • 29
  • 1
    Isn't this the same thing the OP was doing which failed for him because "but it still fails because somewhere along the line, PHP is called again, but it fails, as it's using 4.4.6."? – Sybille Peters Jun 27 '21 at 11:43
  • maybe no, because he done in not exactly same way – Darius.V Jul 11 '21 at 08:15
38

If you just need to get composer to ignore the version requirements, you can bypass using:

composer install --ignore-platform-reqs
Jon Gibbins
  • 887
  • 7
  • 14
  • 2
    This won't run Composer with a different PHP version, but configure Composer to run the dependency resolver for another PHP version. IMHO, this does not answer the given question properly – Nico Haase Feb 02 '22 at 17:00
  • However, when I was having the problem that the OP described, this command was the solution for me. The description of my solution is accurate. – Jon Gibbins Jul 11 '22 at 09:55
38

You always can use that way.

In project folder where you has composer.json file. Run the command

 php7.4 /usr/local/bin/composer install

or

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

where php7.4 your specific version can be(php7.0, php5.5, php7.3 etc...) where /usr/local/bin/composer path for system folder with composer

!!! you should have php7.4-cli

sudo apt-get install -y php7.4-cli

That way for linux/ubuntu user

Cheers ;-)

Galaxy IT
  • 696
  • 6
  • 7
16

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

root@home:~# 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:

root@home:~# /usr/bin/php7.1 /usr/local/bin/composer update

Or just

root@home:~# php7.1 /usr/local/bin/composer
root@home:~# php7.2 /usr/local/bin/composer
root@home:~# php7.3 composer
...

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

root@home:~# 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
Hieu Huynh
  • 1,005
  • 11
  • 18
  • 1
    That's right, although it's not recommended to change the Shebang in the head of `composer`, you could break the original `composer.phar` file. – Ibrahim.H Jan 03 '21 at 13:32
12

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.

harrisoftware
  • 153
  • 1
  • 7
12

In my case, Composer detected PHP version 7.2.6 but my actual PHP Version was 8.0.2 .So , I did the following steps and It was the solution for me.

Step 01:- Add below code to your composer.json file

"config": {
    "platform": {
        "php": "7.2.6"
    }
}

Step 02:- Then, run the below command.

C:\xampp\htdocs\your project name>composer update
Yousha Aleayoub
  • 4,532
  • 4
  • 53
  • 64
Lahiru Kumara
  • 307
  • 3
  • 6
  • This won't run Composer with a different PHP version, but configure Composer to run the dependency resolver for another PHP version. IMHO, this does not answer the given question properly – Nico Haase Feb 02 '22 at 16:59
  • But this sometimes can help. For example, when you build your app in docker, and have another php version on local machine where develops – wapmorgan Mar 05 '22 at 09:59
11

The existing answers partly do not answer the question, give suggestions that do not work or give bad advice.

The question stated:

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.

Instead, I am here showing a way to test and verify the suggestions.

I wish I had a better answer myself but I am unsure what is the best solution. Personally I would go with a solution which make sure PHP is called with the correct version on the host (e.g. via an alias or setting the PHP environment correctly, running the entire operation inside a container or some other solution), regardless of Composer and add a platform php constraint.

If you don't run any scripts and just need the requirements to resolve the dependencies correctly config:platform:php would probably be your friend.

Let's say our default php version is 7.4 but our project requires 7.2.

  1. Check your default PHP version

    command line:

    # shows version of default PHP
    php --version
    # show path of default PHP
    which php
    
    # show version of /usr/bin/env php
    # /usr/bin/env php --version
    
  2. Create a test script in your composer.json

    "scripts": {
        "php-version": "php --version"
    
    }
    

    Now you can test what version of PHP is used for the commands Composer calls, e.g.

    composer php-version
    /usr/bin/php7.2 /usr/bin/composer php-version
    

    In my case, both of these use my default php script (which is version 7.4). So, while the 7.2 version is used to run Composer, the scripts called by Composer use the default command (unless you use @php instead of php1).

  3. Now let's add the platform setting as suggested in some of the answers

    "config": {
        "platform": {
           "php": "7.2.0"
        }
    }
    

    Does not change anything for the script execution:

    /usr/bin/php7.2 /usr/bin/composer php-version
    
    # result is still 7.4
    

    Actually, what the platform option is important for is resolving the requirements, so you should add it (in your project composer.json). It just won't solve the above problem of composer using PHP with the correct version. See also Composer docs for platform.


  1. If you use @php instead of php, then Composer uses the PHP_BINARY2 path which is the php binary path that Composer tried to infer being executed with. See Executing PHP scripts. In this example commonly $(command -v php7.4) different to $(command -v php7.2), here $(command -v php).
  2. PHP_BINARY is an environment variable Composer sets for its own environment (ref), similar to the predefined PHP_BINARY constant in PHP.
hakre
  • 193,403
  • 52
  • 435
  • 836
Sybille Peters
  • 2,832
  • 1
  • 27
  • 49
  • 1
    Thank you very much. This is very helpful for me especially into our transition to an arch-based linux platform due to php got updated to ver. 8 – leipzy Jul 09 '22 at 03:30
  • To use composer PHP version in scripts: `"php-version": "@php --version"` -- mind the `@` in front of `php` in the script line. See [_Executing PHP scripts_](https://getcomposer.org/doc/articles/scripts.md#executing-php-scripts)- – hakre Dec 22 '22 at 20:59
10

Try this approach

ea-php72 /opt/cpanel/composer/bin/composer
Erich García
  • 1,648
  • 21
  • 30
10

I had the same issue and this is how I was able to figure out how to run composer using a different PHP version on a shared hosting without breaking the system :)

Luckily, solving this issue with composer using different PHP version is dead simple.

Findings:

Default PHP was not run from /usr/local/bin or similar folder. Using the

which php

or

whereis php

would not give you the best answer.


Follow the short guide below:

First find the actual path to your composer binary itself. On my shared hosting server, it was at

/opt/cpanel/composer/bin/composer

I believe most hosting providers using cPanel would have such path to composer.

Next, create an alias to the PHP version you want to use. In my project, I needed php 7.4 or newer.

As mentioned earlier, the host was using cPanel and the actual default PHP version that the server uses is in the path /opt/cpanel/

You should see different versions of php in the /opt/cpanel/ (prefixed by ea-phpxx) for example, for php 7.4, it would be a folder called ea-php74 and for PHP 8.0, it would be a folder called ea-php80.

Now, be sure that php exists in a path similar to this:

/opt/cpanel/ea-php80/root/usr/bin

If you find php in the folder, then you can now alias it as folows:

alias php='/opt/cpanel/ea-php80/root/usr/bin/php'

Test that the alias is working by running

php --version 

and check the php version as shown in the screenshot below:

enter image description here


Now, run composer as normal. For example

 php /opt/cpanel/composer/bin/composer update
 php /opt/cpanel/composer/bin/composer install
 php /opt/cpanel/composer/bin/composer install

Remember how we found the composer binary at /opt/cpanel/composer/bin/composer. If this does not match the path to the compser binary on your server, use the appropriate one.

I hope this helps. Happy Coding!

Ernest Elikem
  • 316
  • 2
  • 12
5

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"
bernie
  • 9,820
  • 5
  • 62
  • 92
4

I'm on a Hostgator shared account, the following worked for me:

  1. First, find out where your desired version of PHP lives (you can start typing ea-php and hit TAB to list all available executables starting with ea-php):

    which ea-php73

    /usr/local/bin/ea-php73
    
  2. Then, find out where composer lives:

    which composer

    /opt/cpanel/composer/bin/composer
    
  3. Then use those to run your composer command:

    /usr/local/bin/ea-php73 /opt/cpanel/composer/bin/composer install

Magnus
  • 17,157
  • 19
  • 104
  • 189
  • just in case someone wants to verify if it works or not, try this. https://stackoverflow.com/a/56379908/2190689 – ssi-anik Mar 07 '22 at 15:06
4

For people using Plesk, you need the different pieces:

  1. Get the PHP version required from /opt/plesk/php/<version/bin/php, eg /opt/plesk/php/7.4/bin/php
  2. Get the real path of composer since /usr/local/bin/composer is a custom Plesk script by vieweing the script contents ( cat /usr/local/bin/composer ). In my case, it was /usr/local/psa/var/modules/composer/composer.phar
  3. Put it all together:
# /opt/plesk/php/7.4/bin/php /usr/local/psa/var/modules/composer/composer.phar install

... or to make it simpler, just create a new file ( I like composer74 ) in /usr/local/bin:

  1. cat > /usr/local/bin/composer74 to create the file
  2. paste the following:
#!/bin/bash
/opt/plesk/php/7.4/bin/php /usr/local/psa/var/modules/composer/composer.phar

insto the terminal

  1. Hit CTRL+D to save the file
  2. chmod +x /usr/local/bin/composer74 to make it executable
  3. Use it anywhere:
# composer74 install
Nick Andriopoulos
  • 10,313
  • 6
  • 32
  • 56
3

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

Arshad Syed
  • 403
  • 4
  • 9
3

Ubuntu 18.04 case ... this works for me. Here, Composer picks the required PHP version automatically.

/opt/lampp/bin/php /usr/bin/composer install

OR

 /opt/lampp/bin/php /usr/bin/composer update
Vishal Thakur
  • 1,564
  • 16
  • 25
3

While trying to install a Laravel application on my SiteGround shared hosting using SSH, I had to specify the adequate PHP bin constant prior to executing composer :

PHP_BIN=php81 composer install

Other solutions here would not work.

wlarcheveque
  • 894
  • 1
  • 10
  • 28
2

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

Carlo Espino
  • 1,354
  • 1
  • 15
  • 21
  • I don't have a `post-root`package-install` field. Should I add one? – Boom Feb 04 '16 at 21:22
  • Is not needed, you can achieve same effect running `cp .env.example .env`, it just copies contents of `.env.example` to a new file called `.env` – Carlo Espino Feb 04 '16 at 21:47
2

I had no luck with any of above answers, I had auto scripts in composer.json so just ignoring or faking platforms just caused failed scripts etc. Setting php version in command just didn't work!

I did notice however that although running which php and php -v returned correct version which /usr/bin/env php returned the problematic version of php that composer was using. So found a very good answer here and outlined below:

Issue:

/usr/bin/env php looks for an executable named php in the current $PATH; it pays no attention to aliases, shell functions, etc. If you want it to execute php v5.3, you have to put that version in a directory under the name php, and put that directory in your PATH somewhere before the directory that contains the problematic version.

Fix:

Create new php executable folder like below with your specific php executable linked inside:

mkdir /usr/local/bin/php-overrides
ln -s /usr/local/bin/php7 /usr/local/bin/php-overrides/php

Add below to your ~/.bash_profile (or other appropriate startup script):

PATH="/usr/local/bin/php-overrides:$PATH"

https://serverfault.com/questions/579956/alias-doesnt-work-in-script-with-usr-bin-env-php

harri
  • 494
  • 11
  • 26
  • This is the correct answer. As [Sybille outlined with the example](https://stackoverflow.com/a/68151169/3894752) the `platform` setting does *not change* any local PHP paths. Instead Composer will use this value to _fetch dependencies_ matching this version only. No more, they still run with the current PHP version. Only a change of the `$PATH` variable will change the PHP version used to run subscripts. – pixelbrackets Aug 06 '21 at 14:48
  • 1
    One more comment - there are several tools available to change the PHP version via the `$PATH` env variable. One example is https://github.com/webit-de/php-version-pickup. It picks up the version from a `.php-version` file and continues to use it in the current shell session, also for all subprocesses like the ones initiated by Composer. – pixelbrackets Apr 04 '22 at 14:22
2

There are two ways to do it.

  1. Tell the composer to ignore the platform requirements while running the command in the terminal.
composer update --ignore-platform-reqs
  1. Tell the composer to ignore the platform requirements in the composer.json file in the project root directory.
"config": {  
    "platform-check": false, 
}, 
Karl Hill
  • 12,937
  • 5
  • 58
  • 95
ov_rezoy
  • 51
  • 1
  • 6
  • This won't run Composer with a different PHP version, but configure Composer to run the dependency resolver for another PHP version. IMHO, this does not answer the given question properly – Nico Haase Feb 02 '22 at 16:59
2

We can tell Composer, what version of PHP we are supporting with our app by using the platform configuration in our composer.json file by adding the following configuration,

{
"config": {
    "platform": {
        "php": "5.5"
    }
},
"require": {
    ...
 }
}

Or from the command-line

composer config platform.php 5.5

Prasad Gayan
  • 1,424
  • 17
  • 26
  • 1
    Legend, just what I needed :) – lky Oct 07 '21 at 14:52
  • 1
    This won't run Composer with a different PHP version, but configure Composer to run the dependency resolver for another PHP version. IMHO, this does not answer the given question properly – Nico Haase Feb 02 '22 at 16:59
2

i have a method to slove this problem.


    export PATH=/usr/local/Cellar/php/8.0.12/bin:$PATH && composer -vvv

Temporarily change environment variables and use composer

Cao Vison
  • 47
  • 5
  • Despite being down voted, this did work for me :) Fresh Rocky linux install with composer from remi repo, composer kept trying to use the core system PHP 7.2 and not my PHP 7.4 from remi required by composer.json packages. – Neek Jun 09 '22 at 07:28
  • This is best when you need to temporary switch. – Harry Oct 02 '22 at 15:25
2

With PHP 8 and 8.1 coming out, I have to work with a couple of versions regularly in the CLI, and the best way I found to do this was to create an alias.

Assuming you installed using brew, or already know where your version of PHP is.

Add the following to your bash profile.

alias php7='/usr/local/opt/php@7.4/bin/php'
alias php8='/usr/local/opt/php@8.0/bin/php'

If you want you can install PHP 8.1 as well with brew install php@8.1 and add another alias.

This will allow you to run php8 composer, you can do a quick test by creating an index.php file and adding phpinfo() to it.

php7 index.php | grep "PHP Version"
php8 index.php | grep "PHP Version"
Adam Patterson
  • 651
  • 5
  • 11
0

Must add in two places in your composer.json file to set different php version:

"config": {
    "platform": {
        "php": "7.4"
    }
}
"require": {
    "php": "^7.4",
}
Bira
  • 4,531
  • 2
  • 27
  • 42
  • 1
    This won't run Composer with a different PHP version, but configure Composer to run the dependency resolver for another PHP version. IMHO, this does not answer the given question properly – Nico Haase Feb 02 '22 at 16:59
-1

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.

Dave
  • 9
  • Please share more details. What do you mean by "SIP"? Also, on shared hosting (as given in the question), you cannot edit anything in `/usr/local/bin` – Nico Haase Feb 02 '22 at 17:01
-1

On AlmaLinux with php74 my default PHP version, php81 installed to /usr/bin/php81 and composer in location /usr/bin/composer, the following works one time: running composer using php81.

/usr/bin/php81 /usr/bin/composer install

Composer flags can be added as following:

/usr/bin/php81 /usr/bin/composer install --no-dev --no-interaction --ignore-platform-reqs

Notice these commands need to be executed from you working folder.

Bitpatroon
  • 19
  • 2