Note: If you want to install and use PHP 7 and Composer within the Windows Subsystem for Linux (WSL) using Ubuntu, I wrote a guide for that, too!
I am working a lot on Composer-based Drupal projects lately (especially gearing up for DrupalCon Nashville and my joint workshop on Drupal and Composer with Matthew Grasmick), and have been trying to come up with the simplest solutions that work across macOS, Linux, and Windows. For macOS and Linux, getting PHP and Composer installed is fairly quick and easy. However, on Windows there seem to crop up little issues here and there.
Since I finally spent a little time getting the official version of PHP for native Windows installed, I figured I'd document the process here. Note that many parts of this process were learned from the concise article Install PHP7 and Composer on Windows 10 from the website KIZU 514.
Install PHP 7 on Windows 10
- Install the Visual C++ Redistributable for Visual Studio 2015—this is linked in the sidebar of the PHP for Windows Download page, but it's kind of hidden. If you don't do this, you'll run into a rather cryptic error message,
VCRUNTIME140.DLL was not found
, andphp
commands won't work. - Download PHP for Windows. I prefer to use 7.1.x (current release - 1), so I downloaded the latest Non-thread-safe 64-bit version of 7.1.x. I downloaded the .zip file version of the
VC14 x64 Non Thread Safe
edition, under the PHP 7.1 heading. - Expand the zip file into the path
C:\PHP7
. - Configure PHP to run correctly on your system:
- In the
C:\PHP7
folder, rename the filephp.ini-development
tophp.ini
. - Edit the
php.ini
file in a text editor (e.g. Notepad++, Atom, or Sublime Text). - Change the following settings in the file and save the file:
- Change
memory_limit
from128M
to1G
(because Composer can use lots of memory!) - Uncomment the line that reads
; extension_dir = "ext"
(remove the;
so the line is justextension_dir = "ext"
). - In the section where there are a bunch of
extension=
lines, uncomment the following lines:extension=php_gd2.dll
extension=php_curl.dll
extension=php_mbstring.dll
extension=php_openssl.dll
extension=php_pdo_mysql.dll
extension=php_pdo_sqlite.dll
extension=php_sockets.dll
- Change
- In the
- Add
C:\PHP7
to your Windows system path:- Open the System Control Panel.
- Click 'Advanced System Settings'.
- Click the 'Environment Variables...' button.
- Click on the
Path
row under 'System variables', and click 'Edit...' - Click 'New' and add the row
C:\PHP7
. - Click OK, then OK, then OK, and close out of the System Control Panel.
- Open PowerShell or another terminal emulator (I generally prefer cmder), and type in
php -v
to verify PHP is working.
At this point, you should see output like:
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
PS C:\Users\jgeerling> php -v
PHP 7.0.29 (cli) (built: Mar 27 2018 15:23:04) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
This means PHP is working, yay!
Install Composer on Windows 10
Next, we're going to install Composer by downloading it and moving it into place so we can run it with just the composer
command:
- Download the Windows Installer for Composer and run it.
- Note that the Windows Installer for Composer might ask to make changes to your
php.ini
file. That's okay; allow it and continue through the setup wizard. - Close out of any open PowerShell or other terminal windows, and then open a new one.
- Run the
composer
command, and verify you get a listing of the Composer help and available commands.
That's it! Now you have PHP 7 and Composer running natively on your Windows 10 PC. Next up, dominate the world with some new PHP projects!
Comments
Or you could install the Linux Subsystem for Windows and do it the Linux way!
Quite true! I may do another post showing how quick/easy it is to do it in the WSL, and also compare and contrast the two methods (there are downsizes to either technique, mostly having to due with integration with other native Windows tooling, like IDEs like PHPStorm or other GUI tools or local environments developers may want to use with their code.
Good points, look forward to reading that post if you decide on it. I've been quite impressed with the WSL, but I haven't tried much tooling.
I did end up posting the article on doing the same things in WSL: Installing PHP 7 and Composer on Windows 10, Using Ubuntu in WSL
Did you do a perfomance comparison?
Or not ... you prefer Linux, I prefer Windows. Why don't we all keep it that way?
I'd prefer to use the easiest way, and given it's a one command install on Linux/WSL it's an appealing option.
Brilliant. Thank you. Worked as of 06/18 :)
I successfully completed installing the php but couldn't install composer, it says openssl missing. But i did uncomment the extension for openssl. How can I resolve this? I am using VC15 x64 Non Thread Safe (2018-May-24 20:14:11) version. Thanks.
Uncomment line ;extensions=php_openssl and should be good
Works here !! THX
Activate the path c:\php\ or where it is installed in the variable, in the system and user path
Thank you so much.
Thanks worked perfect!
Thank you so much for this. The PHP doc for installing on windows is trash. You are amazing.
Thank you for this
This made things so much simpler! Thank you so much for taking the time to make this available. Very much appreciated.
This is great. I have PHP 7.2 running in both the Windows 10 powershell side, and WSL under Ubuntu. Now the issues really start when you start working with your source code managed with Git. Permissions are JACKED. Windows powershell project folder git status shows 1000s of files have changed permissions. A large but different collection of files are showing as modified under WSL. What a mess. A third collection of changes are listed when viewing via git-bash.exe. Friggin windows man, what do we have now, four shells, and they still all suck compared to Terminal on Mac OS X? So depressing. I guess I'll throw down another $3,500 for a Macbook Pro, even though the touchbar is the dumbest thing ever, they took away the magnetic power jack and the keyboard sucks even more than before.
Tomorrow I'm going to buy an SDD so I can dual boot linux. Windows 10 permissions are an absolute mess. Such a waste of time.
Absolutely perfect (even got an added bonus: cmder!)
Thanks!
I followed every step of your guideline but in the Composer Setup I'm getting the following error: "The "https://getcomposer.org/versions" file could not be downloaded: failed to open stream.". Do you know how to fix this?
Thanks in advance!
Amazing article!
Well, with PhpManager you can install PHP and Composer with just these three PowesShell commands:
Install-Php -Version 7.2 -Architecture x64 -ThreadSafe $false -Path C:\PHP -TimeZone UTC -InitialPhpIni Production -InstallVC -Verbose
Enable-PhpExtension -Path C:\PHP -Extension mbstring,curl,openssl,gd,exif,zlib -Verbose
Install-Composer -PhpPath C:\PHP -Verbose
Amazing!!! excelent work on this doc!!!! TKS!
Thank you for this very helpful article!
I can add that you must specify the extensior_dir in php.ini: https://stackoverflow.com/questions/38642682/php-ini-default-dir-extens…
(just uncomment it)
Nice post!
Thank you so much for this thorough and helpful post!! I love when I don't have to think about how to set up my environment. :)
On php 7.2.x:
Note : The syntax used in previous PHP versions ('extension=.so' and
; 'extension='php_.dll') is supported for legacy reasons and may be
; deprecated in a future PHP major version. So, when it is possible, please
; move to the new ('extension=) syntax.
Thank you! Very helpful for a beginner.
Thank you so much.
Excellent video Jeff. I thought I was going to spend the rest of my life trying to get php and composer working. Many thanks!
My php.ini file didn't have any of these:
extension=php_gd2.dll
extension=php_curl.dll
etc..
but it does have these:
extension=gd2
extension=curl
etc..
However, if I comment out these ones I get an error when I try and run php in command prompt. The error says:
PHP Warning: PHP Startup: Unable to load dynamic library php_gd2.dll
PHP Warning: PHP Startup: Unable to load dynamic library php_curl.dll
etc..
any tips?
You are a great blessing - thank you for these instructions.
I have the same problem, what to do?
instead of removing the semicolon from the line
extension_dir = "./"
remove it from the line below
extension_dir = "ext"
This did the trick for me
AMZING tutorial !
Thanks a lot.
Thank you so much!!! I have been trying to get php properly installed for 2 months now and I haven't been able to get it. Now, FINALLY, I have it properly installed!
Great page. Super instruction video. Really nice work. Thanks much!
It’s ok sir if used visual studio 2010 ?
Hey, I tried installing php and composer using the instructions written above,php and composer installation worked well. But when I tried to install laravel, I encountered this error:
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Your requirements could not be resolved to an installable set of packages.
Problem 1
- Installation request for league/flysystem 1.0.50 -> satisfiable by league/flysystem[1.0.50].
- league/flysystem 1.0.50 requires ext-fileinfo * -> the requested PHP extension fileinfo is missing from your system.
Problem 2
- league/flysystem 1.0.50 requires ext-fileinfo * -> the requested PHP extension fileinfo is missing from your system.
- laravel/framework v5.8.2 requires league/flysystem ^1.0.8 -> satisfiable by league/flysystem[1.0.50].
- Installation request for laravel/framework v5.8.2 -> satisfiable by laravel/framework[v5.8.2].
To enable extensions, verify that they are enabled in your .ini files:
- C:\PHP7\php.ini
You can also run `php --ini` inside terminal to see which files are used by PHP in CLI mode.
Application ready! Build something amazing.
I un-commented
;extension=php_fileinfo.dll
in php.ini in php7 folder. Then laravel was installed successfully. Otherwise amazing article , thanks a lot <3
tks, it work good for me :D
Thanks it worked for me !!!
Thank you for instructions, it worked 100% for me. :-)
really helpful , it worked fine
Thankyou sir!
Food guide, it works for php. But I have a question: where I can put my php files and see in my localhost? So where is the root folder?
Thanks
For that you would also need to install a webserver like Apache, or use PHP's built-in webserver.
Thank you. It works perfectly!
Thanks man ! Your better than my teacher :P
when i used php in powershell it was showing
php : The term 'php' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ php -v
+ ~~~
+ CategoryInfo : ObjectNotFound: (php:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
make sure you downloaded VC15 x64 Non Thread Safe (2019-Jul-03 17:49:08) and not the source code
A really great post. It got easier to setup PHP
Set the reboot point of the computer to 6)!
After hours looking for simple explainations I found this ... GOD BLESS YOU
Thank you for that great tutorial! I needed an newer C++ Package 2015-2019 and choose PHP 7.3. But ist still works!
This was so helpful, thanks so much!
Careful using Choco. It put a version of PHP7 in my c:\tools\php7 directory without my knowledge. The ENV var was hitting this version first and still failing the CURL command during Composer Update. Once I realized this, the followed the instructions above, it worked fine.
As an aside the extension= may only have the gd2 or the application they are referring to not the php or the dll part of the sting.
Thank you! Clear and useful
Thank you for this very useful guide. Worked like a charm. Thumbs up !
This might be a dumb question, but what are the benefits of installing these programs natively as opposed to using something like Xampp? I'm new to a lot of this and trying to set that foundational understanding.
Como puedo instalarle la extension json al php, uso windows 10, parece ser que se necesita un dll, en fin si fueran tan amables de explicarme por favor
Thanks for the tutorial!
Why install just PHP and Composer and not a server setup ? How could you work with php but not Mysql/Mariadb and an apache sever? Would've been great if you had first identified the purpose of this article
Sometimes you need to run and/or develop simple PHP apps or scripts on your computer, and for those times it's nice to just have PHP installed. There are a zillion other ways to install PHP, MySQL, MariaDB, Postgres, etc., and I typically recommend Docker or Vagrant for local development, but the fastest method is usually a native install (which this post covers).
Thanks so much
Its works. Thank you so much. Having headache while trying to develop using symfony framework. My Hello World possible now! Thanks.
nice article keep it up, i hope theres info also about your dev workflow, thanks!
Very useful post.!
Today I tryied on windows 10 family and it still version PHP 5.6.40.
But when I did open php.ini, I had to look for different files like: extension=gd2 instead of extension=php_gd2.dll.
So I did all the operations and I can observe in the PHP7 file, there is php.ini-production which contains similar code than development. I will try to find another way because I still cannot run composer and Laravel today...
Turns out PHP 7.4.x requires Microsoft Visual C++ Redistributable for Visual Studio 2019 which can be downloaded here:
https://visualstudio.microsoft.com/downloads/
Then scroll down to Other Tools and Frameworks
Thank you! :)
Thanks too much, That's really helpful to me thank you :)
I tried as per the steps. But I am getting the below error.
php : The term 'php' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ php -v
+ ~~~
+ CategoryInfo : ObjectNotFound: (php:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Thanks, straightforward and beautiful.
Hi Jeff.... thank you so very much for making available the most lucid, comprehensible instructions anywhere on the internet. Thanks to you I now have Composer set up and functioning and can start working with Drupal Commerce 2. All the best, david h.
Thank you so much! I've only ever needed to use PHP at work, where it's always been Linux, but I lost my job (had a baby and no longer sleep which made it really hard to cope) and am trying to get something part time and they all want technical tests and the offical docs are useless when you're sleep deprived, whereas this was so easy to follow.
Whenever I try to install Composer on my machine (Windows 10), regardless of the environement I'm attempting to set up, composer always fails to run, which results in the following error:
"Program 'composer.bat' failed to run: The specified executable is not a valid application for this OS platform.At
line:1 char:1"
It's very annoying.
Here's a solution: Find Composer.bat in C:\Windows\System32 and remove it. For whatever reason, I had an empty Composer.bat file in there that was causing the issue. After removing it, Composer runs as intended.
Thanks for posting your solution! (Too many people leave a question and figure out the answer... then never follow up!)
Helpfull article. Good work carry on
TY! Worked perfect!
thanks work for php 7.4.6 :)
Thanks bro.. it works
Thanks bro. It worked perfect.
Thanks so much. You are a lifesaver. Everything worked perfectly.
Why non thread instead of thread, no explanation was given for that? Also I ask because I am watching a tutorial with Apache, MySQL and PHP and it recommends that it be with thread safe. What the reasons for both and can I have both on my system?
Thank you, this was extremely helpful! I was looking for a decent tutorial about this for quite a while and you definitely nailed it!
Thanks so much! What an amazingly useful video! Much appreciated
thank you, Sir. it totally worked! so glaaaad~