I have multiple projects that I work on.
Some are on php 7.3, most of 7.4 and a single one on 8.1
Having to switch php everytime when I switch projects is a solution I have now, but sometimes I need to do a quick check on another project, and that breaks code running in another project because the global php are updated.
All my projects are composer based, but composer doesn't "spit out" what the most desired php version is.
How can I based on composer.json/lock detect the correct php version to use?
This script is made with php installed via ppa:ondrej/php. If your php versions are self compiled, and not linked in for example /usr/bin/php7.4 you'll need to modify the paths to the correct paths. This code however does assume the binaries are named php7.x or php8.x
This also requires that there is a composer installed on your system.
Add the following code to your .bashrc file.(nano ~/.bashrc)
Explanations of what does what are in the comments in the code.
function do_php() {
# Path where to find the php binaries
PHPPATH='/usr/bin'
# PHP versions in order of most used
declare -a phpversions=("7.4" "7.3" "8.1" "8.0" "7.2" "7.1" "7.0");
# Get a direct path to composer
composer_path="$(whereis composer | cut -d ' ' -f 2)"
# Loop through all php versions
for phpversion in "${phpversions[#]}"
do
PHP=$PHPPATH/php$phpversion
# If the given php version is installed
if [ -f $PHP ]; then
# Check if it matches the requirements
SUCCESS=$($PHP $composer_path check-platform-reqs 2>/dev/null | grep php | tr -s ' ' | cut -d ' ' -f 3)
# If we're matching
if [ "$SUCCESS" == "success" ]; then
# Run command with given binary
$PHP "$#"
# exit function
return
fi
fi
done
#Nothing matched, just run with default php and let the programmer deal with the issue
\php "$#"
}
# Set the alias for php to do_php
alias php='do_php'
All my projects are composer based, but composer doesn't "spit out" what the most desired php version is.
How can I based on composer.json/lock detect the correct php version to use?
Well composer itself can not magically tell you what your intend (per project) is, however you can configure this easily on a per-project basis with platform packages. For the PHP version it is the php package:
{
"config": {
"platform": {
"php": "7.0.3"
}
}
}
(Compare: Composer config platform, Run composer using different version of PHP without changing the environment variables)
You can also add extensions and libraries. Now you have configured the information in your project and composer will use that version when installing and updating dependencies (e.g. for 7.0.3 above, package versions that require >= 7.0.4 won't install).
7.0.3 is a bad example (just copied the version from Composer docs), let's say you're in a PHP 7.4 project:
$ composer config platform.php '7.4.30'
It is also now easy to map on the Sury packages binary names, just keep the head:
$ composer config platform.php | head -c3
7.4
For Composer invocations itself it won't be necessary any longer, however you may want to do other things, so here some hints, that already work within Composer:
The #php script shortcut (Composer, see Executing PHP scripts)
The PHP_BINARY environment variable (Composer, see same)
Those are all based on the php binary composer is running with. So if you need to inject it, call composer with the adequate php version:
$ composer config platform.php '8.1.11'
$ php$(composer config platform.php | head -c3) $(which composer) diagnose
Checking composer.json: OK
...
PHP version: 8.1.11 - Package overridden via config.platform, same as actual
PHP binary path: /usr/bin/php8.1
...
(Example: Composer invocation with Ondřej Surý php8.1 package on Ubuntu)
So eventual composer scripts/hooks should be covered.
For invocation in general I'd suggest to use a build manager to define targets, their dependencies and the rules how they are made. You should have the make utility already on your box and then it is just make and done for most of the time. You can add the php executable name to configuration there, control the platform version in composer.json and have this even re-useable across projects. no need to add it to the environment when you can have it in the project IMHO (Compare Automation and Make).
When it comes the day you start to migrate your project to another php version, you have only one environment. With a build manager you can easily run the same setup with different php versions to check the upgrade path (and even can do this in parallel). Just for the outlook. Keep it simple and stupid, then you have the best options in the long run (and also portability across projects).
So i just updated composer using the command composer self-update --2,
However, now my web application shows the error Composer detected issues in your platform: Your Composer dependencies require a PHP version ">= 7.3.0".
I dont understand this because doing php -v gives me PHP 7.4.13
How can I fix this?
Your terminal user's PHP version may differ from the server's version.
You may have 7.4.13 on terminal while having a completely different PHP version in apache2 or whatever server you are using.
Use phpinfo(); in a PHP file and access it via browser to see the actual PHP version.
Here is quick solution that work for me
In you project you can check the platform check file
projec-path/vendor/composer/platform_check.php
$issues = array();
after this Remove or comment the extra code
Add the platform check option in the composer.json config section like this.
"config": {
"platform-check": false
},
After that, you need to run
composer update
After the composer update platform_check.php will be deleted and project work fine.
Please run this command:
composer install --ignore-platform-reqs
If you add the --ignore-platform-reqs option when running Composer update, it will ignore restrictions. Click here for more information.
I simply change the file platform_check.php which was showing the PHP_VERSION_ID >= 70400. change it to 70300 (This is for PHP >=7.3) This solve my issue.
I ran into this issue after installing a new version of PHP on a server using IIS. Not realizing that IIS didn't support verbs like UPDATE/PATCH out of the box, and these methods were being utilized by the website. When a route was accessed via PATCH for instance, the new version of PHP was not handling that, so it fell-back to the old version of PHP -- and that's when this error presented itself.
Resolving it was simply a matter of editing the Handler Mappings in IIS. I found the mapping that would point *.php files to my new version of PHP, and customized it so that it would support other verbs like PATCH
Just update your version of PHP in cpanel.
I ran into the same issues and this helped me.
Problem is sometimes we forget to change the php version at server(platform) level, which is usually different from the dependency level or cli after an update or upgrade or even sometimes on a fresh installation.
For anyone using cpanel, ensure to update your php on the cpanel to the one used on the application or during development.
For anyone using apache, check your apache conf file for the project to make sure it has the same php version running in your cli i.e used by composer.
For anyone using nginx, check your nginx conf file for the project to make sure it has the same php version running in your cli i.e used by composer.
For anyone using valet check your current project' valet php version, to make sure its the same as the one used by composer, to change php version: valet use php[x.x]
Summary.
Check your server configuration to make sure it has the same php version required by your application.
inform required "require" version if desired or remove from composer.json
always put the ^ to indicate that the version can be equal or greater
then run composer update
in my case, it worked
{
"require": {
"php": "^7.4"
},
"autoload": {
"psr-4": {
"App\\": "App"
}
}
}
I have faced the same issue on my server. And it is mainly happened due to miss-match of php version between server version and your system version. I am using Nginx and checked on all my project configuration files, it uses php8.0 but on my terminal it shows php.8.1. Actually my application needs php8.0. The following steps resolved my issue.
To see all my running php versions
sudo update-alternatives --config php
It shows the following page
Selecting php8.0 for my application. Run the command to see Nginx scripts are ok
sudo nginx -t
Then reload the Nginx
sudo systemctl reload nginx
Finally go to your project directory and update or install your composer
composer update
It has been resolved my issue, I expect same will resolve yours.
Problem Statement: "Composer detected issues in your platform: Your Composer dependencies require a PHP version ">= 7.4.0"
It'll solve your problem 100% .
Use this fragment " --ignore-platform-reqs " while adding other pacakge without disturbing other package dependency .
for example
composer require giggsey/libphonenumber-for-php --ignore-platform-reqs
I have already use WAMP 2.5 with PHP 5.5.12, and with Composer.
The php is on:
C:\wamp\bin\php\php5.5.12
For new project, I need to use nginx and installed PHP 7.
The php is on:
C:\nginx\php
Now, using GitBash MINGW32, I tried to install laravel 5.3 using Composer create-project but it said
[InvalidArgumentException]
Could not find package laravel/laravel with version 5.3 in a version
installable using your PHP version 5.5.12.
I already put both C:\wamp\bin\php\php5.5.12 and C:\nginx\php on Windows System PATH variable.
How do I change the PHP version used by Composer?
Three ways to do this, really.
Create an alias in .bashrc to always run composer with the corresponding version
Something like alias ncomposer=`/path/to/php /path/to/composer.phar `
Specify the path to PHP version inside composer.phar itself
This is specified at the start of the file: #!/path/to/php php. Then composer should run with composer.phar
NB! The line will disappear upon self-update, so it's not a reliable solution.
Move up the path with the newest PHP version
If you place C:\nginx\php first, it should be used by default when using composer.
Hope this helps!
Although this question was solved, the answer didn't help me. I will explain how I managed to make composer to work in a version of PHP different from the one which is installed by default on my OS (PHP 7.1.1) as well as in my environment variables (these will not be changed !). Note that I'm using Xampp, but the principle remains the same for Wamp.
Starting from this answer :
Start up Git Bash
Type cd ~/ to go to your home folder
Type touch .bash_profile to create your new file.
Edit .bash_profile with your favorite editor
In my case I have a folder named php733 inside xampp folder which corresponds to PHP 7.3.3. This is this other answer that helped me in creating the alias :
alias composer733='/c/[xampp folder]/php733/php.exe /c/ProgramData/ComposerSetup/bin/composer.phar '
Then, type . .bash_profile to reload .bash_profile and update any functions you add. Notice the space between the two dots !
Finally, type this command in Git Bash :
composer733 [whatever you wan]
Example : in the project that requires at least PHP 7.1.3
Using composer :
$ composer update
This package requires php ^7.1.3 but your HHVM version does not satisfy that requirement.
Using composer733 (the alias I created) :
$ composer733 update
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 98 installs, 0 updates, 0 removals
- Installing [...] (v1.11.0): Loading from cache
It works, without having to change the environment variables
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.
Ubuntu 18.04 case ... this run for me.
/usr/bin/php7.1 /usr/local/bin/composer update
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"
}
}
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
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 ;-)
If you just need to get composer to ignore the version requirements, you can bypass using:
composer install --ignore-platform-reqs
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
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.
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
Try this approach
ea-php72 /opt/cpanel/composer/bin/composer
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.
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
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).
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.
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).
PHP_BINARY is an environment variable Composer sets for its own environment (ref), similar to the predefined PHP_BINARY constant in PHP.
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:
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!
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"
I'm on a Hostgator shared account, the following worked for me:
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
Then, find out where composer lives:
which composer
/opt/cpanel/composer/bin/composer
Then use those to run your composer command:
/usr/local/bin/ea-php73 /opt/cpanel/composer/bin/composer install
For people using Plesk, you need the different pieces:
Get the PHP version required from /opt/plesk/php/<version/bin/php, eg /opt/plesk/php/7.4/bin/php
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
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:
cat > /usr/local/bin/composer74 to create the file
paste the following:
#!/bin/bash
/opt/plesk/php/7.4/bin/php /usr/local/psa/var/modules/composer/composer.phar
insto the terminal
Hit CTRL+D to save the file
chmod +x /usr/local/bin/composer74 to make it executable
Use it anywhere:
# composer74 install
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
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
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
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
There are two ways to do it.
Tell the composer to ignore the platform requirements while running the command in the terminal.
composer update --ignore-platform-reqs
Tell the composer to ignore the platform requirements in the composer.json file in the project root directory.
"config": {
"platform-check": false,
},
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
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
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"
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.
Must add in two places in your composer.json file to set different php version:
"config": {
"platform": {
"php": "7.4"
}
}
"require": {
"php": "^7.4",
}
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.
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.