Change PHP version used by Composer on Windows - php

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

Related

How to force creating a new Symfony application for a specific PHP version?

I want to generate a Symfony application which can be compatible with PHP version up to 7.4.22.
On my local machine, PHP version is 8.0.2 but on the hosting is 7.4.22.
Symfony application is generated from local machine using standard composer command
composer create-project symfony/website-skeleton my_project_name
I look over create-project arguments to see if I could add some constrains but none seems to be useful.(or didn't figure out)
If try to upload to host the project I get this when trying to install it:
Your Composer dependencies require a PHP version ">= 8.0.0". You are running 7.4.22.
Need just a default empty project to check something on hosting.
I do not want to alter local PHP and doing the same thing on hosting could be troublesome.
You cannot add a "dependency constraint" to create-project.
E.g. Something like an imaginary create-project symfony/skeleton --dep php: "^7".
What you could do is:
first create-project
Add the config.platform key to your composer.json adjusted to your server's version (Docs.)
Run composer update
Either some packages will be downgraded if needed, or you'll get a message telling you that the new, target version is incompatible.
E.g. after doing a create-project symfony/skeleton and changing config.platform.php to 7.2, on executing composer update I get:
Your requirements could not be resolved to an installable set of packages.
Running composer create-project symfony/website-skeleton my_project_name in an example project using PHP 8 installs psr/cache in v2 and psr/link in v1.1.1. These package versions require PHP 8, and they will not work using any older version of PHP.
To avoid this, just don't run composer create-project with any later version than the one you want to use on your production system (as usually, running it with a lower PHP version than on production yields a set of packages that is compatible with the production system, while the chance is lower the other way around).
Otherwise, check whether there are such package versions using composer why-not php 7.4 before deploying and downgrade them

Laravel composer install giving error "Your lock file does not contain a compatible set of packages please run composer update"

I have been writing laravel code for quite sometime. Currently, I tried cloning a project from github and editing locally. I installed composer in my project directory but a vendor folder was not included, I tried to run composer install but I gives me this error
Your lock file does not contain a compatible set of packages. Please run composer update
How do I resolve this?
Note: I have tried running composer update on previous clones and that didn't work.
Run this command:
composer install --ignore-platform-reqs
or
composer update --ignore-platform-reqs
Disclaimer, this solution will not fix the issue for PHP 8 projects.
In most cases this happens because of PHP 8 (In my case it was GitHub CI actions automatically started using PHP 8 even though my project is php 7.4)
If you have multiple PHP installations (E.g. 7.4 and 8 on the same server), this is how you can fix it.
Specify your php version in your composer.json file
"config": {
"platform": {
"php": "7.3"
}
},
If you have the lock file already committed, run composer update after you adding above line in to the composer.json and then commit the new lock file. (Please be aware composer update will upgrade your packages to latest versions)
I solved this problem with this command:
composer self-update --1
It probably works because at time that the project was developed, composer was on another version and when change the Major version from 1 to 2 the compatibility was broke. With this command you downgrade composer and probably going to solve this
You should try running composer update --lock that will update all packages and recreate the compose.lock file.
Either you can delete the composer.lock file and run composer install that will also recreate the .lock file.
This resolved my issue.
I had this error with Github Actions trying to deploy a Laravel app, this is probably different than the OP's case but none of the suggestions worked for me. Adding my answer here just in case there is someone else out there with a similar problem to mine.
I had to disable -q in Github Actions and see that it was complaining about extensions not being installed.
Make sure your require section of composer's php extensions matches the extensions: in your github action file for shivammathur/setup-php#v2 and it will deploy again
Recently I've just come across of this error when I tried to run my Laravel 7 project which required php v7.* with php v8. As I forgot my php version I just tried bunch of composer command, but just got error after error.
Anyway, to solve this just downgrade/upgrade php version as required. Just search how to do that in youtube.
you can see your project required php version in composer.json file (just if you wonder)
Also you can try following way (But though it didn't worked for me, seems it helped quite some people)
-- Open composer.json file and change php version to something like this: "php": "^7.3|^8.1"
-- Then run composer update
I faced this problem with my cakephp project in garuda linux (arch based)
Fix :
Install php-intl using sudo pacman -S php-intl
Enable php intl by editing php config ( in my case /etc/php/php.ini ) .
add extension=intl or uncomment the existing one
restart apache or whatever you are using
I had the same error deploying another project with composer, but the problem was a missing php extension.
I understand you solve your problem but for anyone seeing the same error message, here is a general guidance :
The error message Your lock file does not contain a compatible set of packages. Please run composer update is shown each time there is a conflict during the dependency solving step of composer install. (see the relevant part in composer source code)
It doesn't inform on the real problem though, and it could be hard to guess.
To get the exact explanation you can add --verbose option to composer install command (the option is available to any composer command (see the doc)) : composer install --verbose
It will give you the full message explaining what exactly is preventing composer install from completing (package version conflict, missing php extension, etc.), then you'll be able to fix the problem.
Hope this could help.
In my case this problem is occuring in Ubuntu 20.04 Desktop. This is due to some missing packages.
I ran the following commands to install some packages then rerun Composer install and its working properly. The commands are:
sudo apt-get install php-mbstring
sudo apt-get install php-xml
Then rerun composer install

How to change PHP version in a Symfony 4 project?

I want to change the PHP version because my version is 7.1.23 and I need to upgrade to 7.2 for install a package that I want to use.
I'm using MAMP on Macbook Pro.
Here's what I've done :
I've add this line in this .bash_profile file :
export PATH=/Applications/MAMP/bin/php/php7.2.14/bin:$PATH
In the composer.json file I've update the version to
"require": {
"php": "^7.2.14"
I've create a .php-version in the project directory. It contains :
7.2
If I do "php -v" in my terminal I've that :
~ php -v
PHP 7.2.14 (cli) (built: Feb 1 2019 12:25:00) ( NTS )
When I launch my project with "symfony serve", that give me :
The Web server is using PHP CGI 7.2.14
https://127.0.0.1:8000
But when I want to make "composer update" or "symfony local:php:list" it return me that I'm still using PHP version (7.1.23).
How can I finally upgrade the version ?
Here's a screen from my terminal :
okay, so as far as I can tell, your symfony command already uses 7.2.14 (highlighted version in first column, ignore the * in the last column, it only tells what is the system's default), since it says so on symfony serve.
composer however doesn't, which is most likely due to the fact that composer is a php script that is made executable via a hash bang at the very first line of the file (and the x chmod):
#!/usr/bin/php
which essentially says, "run me with the interpreter/command /usr/bin/php". There are other variants of composer around, some have #!/usr/bin/env php which will look for a php in the PATH variable and use that instead.
However, to fix this, I propose these reasonable options
run composer with an explicit php (check composer path with which composer):
php /usr/bin/composer update
which obviously is a bit inconvenient, or
just install another composer, which is most likely more recent anyway and add it to your PATH before /usr/bin like you did with your alternative php version, which will allow you to run it standalone/independent, or
use the symfony script to run composer:
symfony composer update
disclaimer. I haven't actually tried this, but I would be surprised if this didn't run composer with the correct php version...
I had the same problem and I solved it by creating a file ".php-version" that contains the php version number (ex : 8.1.2) in the project directory

error using composer to install cakePHP 3

i am lost...
i am trying to do it the right way and am following the Quick Start Guide for installing cakePHP 3 on my ubuntu machine.
Got composer installed. Directory is /var/www/cakephp
and am trying to issue:
php composer.phar create-project --prefer-dist cakephp/app mayapp
or
composer create-project --prefer-dist cakephp/app may app
then i am simply getting:
[InvalidArgumentException]
Could not find package cakephp/app with stability stable.
i do not have a clue what i am doing wrong. Any suggestions?
cakephp/app exists on https://packagist.org, and the command looks ok to me.
Which PHP version are you using? That package is only available with PHP 5.4.16 or later, so you must update if you want to use CakePHP.
However, it is a good idea to only use the same PHP version of your public hosting, using a newer version will lead to problems when deploying your code. If your webspace or public server does not have the latest PHP 5.6 running, ask for an update.
I got same issue. But, as previous answer, myapp does not exists on packagist.org, so, you must use laravel/laravel instead, and, after created, rename the new laravel folder to your desired name and move to htdocs (for xampp) or www (for wamp), then, and I do not know why, go to : localhost/laravel/public/
and replace laravel with the name you gave to your project folder instead the laravel one.
You will see Laravel 5 fonts will appear on screen.
That's means Laravel is running.
Hey Please try these steps.
Download XAMPP Server.
Install XAMPP server.
Go to ..\xampp\php\php.ini and open.
Add this line (extension=php_intl.dll) or if exist uncomment.
Download Composer setup from https://github.com/composer/windows-setup/releases/
Install composer and give php.exe file in path
Open cmd and check now php version using command php -v. if its showing php version its means its working.
Now open cmd and cd on xampp\htdocs folder & run command composer create-project --prefer-dist cakephp/app app_name
Finished folder created in htdocs.
Hope this is helpful for you.

Problems installing laravel with composer [duplicate]

This question already has answers here:
How to place the ~/.composer/vendor/bin directory in your PATH?
(22 answers)
Closed 6 years ago.
Problem: I'm wanting to explore laravel 5, and failing miserably at installing it. I'm using this guide: http://laravel.com/docs/5.0 and need someone to help me understand the instructions.
Background and What I've Tried
I'm running Mac OSX 10.10.2 (Yosemite) and MAMP.
So far, I've downloaded Composer to my home folder using terminal. There is just a composer.phar file sitting there.
When I run:
composer global require "laravel/installer=~1.1"
I get the message:
Changed current directory to /Users/MYUSERNAME/.composer
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Generating autoload files
I assume that is ok because when I run the following in terminal, I get the composer logo and a list of options
~ MYUSERNAME$ composer
I'm not 100% sure what the following means, from the Laravel Docs:
"Make sure to place the ~/.composer/vendor/bin directory in your PATH so the
laravel executable can be located by your system."
Because I can't figure it out, the following steps throw errors, such as:
-bash: laravel: command not found
I've been going through a few forums, and it's suggested that I need to update my PHP.ini file - this seems more related to Composer install, and not specifically Laravel. Because composer is working, this seems to be a dead end.
Ideally, I want to install Laravel 5 to the directory
HomeFolder/sites/test
because Composer.phar is in my home folder, I think the command should be:
php composer laravel new sites/test
or just
composer laravel new sites/test
As mentioned, it just (correctly) throws errors.
Question:
If anyone can help solve my total user error, by explaining what "Make sure to place the ~/.composer/vendor/bin directory in your PATH so the laravel executable can be located by your system." means to a n00b, that'd be really appreciated.
Many thanks!
Laravel is a PHP framework (makes writing PHP applications easy)
Composer is a PHP package and dependency manager. (makes installing and updating third party code libraries easy)
When you run
$ composer global require "laravel/installer=~1.1"
You're using composer to install the laravel/installer=~1.1 package into composer's "global" project folder (usually ~/.composer). This is what installed the command line program named laravel.
The command line program named laravel is a shell script for installing the PHP framework (Also named Laravel).
Your "Unix Path" is a list of folders where a command line script will search for an executable. Usually is has folders like /usr/bin, /usr/local/bin, etc. This is why when you run ls, you're actually running /usr/bin/ls -- the shell knows to check each folder in the path for a location. You can view your current path by typing
$ echo $PATH
So, the problem is composer installed the laravel command line program to a folder that's not in your unix path. You need to add this folder to your unix path. You can do this by running the following (assuming you're using bash, which is OS X's default shell)
$ PATH=$PATH:~/.composer/vendor/bin
If you run that, you should be able to run the laravel command line program and continue your installation.
Most people add this to their .bash_profile or .bashrc files. The Unix Stack Exchange has a lot of good information if you're interested in learning how to do this.
You can add the directory to the PATH variable by editing /etc/paths.
Here's a tutorial on how to do that.
Just add a line with:
~/.composer/vendor/bin
Then the laravel new command should work fine
If everything fails you can still use the composer create-project command to make a new laravel instance:
composer create-project laravel/laravel sites/test --prefer-dist
I added C:\Users\Leon\AppData\Roaming\Composer\vendor\bin instead of ~/.composer/vendor/bin to the Path variable.
Here is instructions on changing the path variable on Windows 10:
http://windowsitpro.com/systems-management/how-can-i-add-new-folder-my-system-path

Categories