Installing PHP 7 and Composer on Windows 10, Using Ubuntu in WSL

Note: If you want to install and use PHP 7 and Composer within Windows 10 natively, I wrote a guide for that, too!

Since Windows 10 introduced the Windows Subsystem for Linux (WSL), it has become far easier to work on Linux-centric software, like most PHP projects, within Windows.

To get the WSL, and in our case, Ubuntu, running in Windows 10, follow the directions in Microsoft's documentation: Install the Windows Subsystem for Linux on Windows 10, and download and launch the Ubuntu installer from the Windows Store.

Once it's installed, open an Ubuntu command line, and let's get started:

Install PHP 7 inside Ubuntu in WSL

Ubuntu has packages for PHP 7 already available, so it's just a matter of installing them with apt:

  1. Update the apt cache with sudo apt-get update
  2. Install PHP and commonly-required extensions: sudo apt-get install -y git php7.0 php7.0-curl php7.0-xml php7.0-mbstring php7.0-gd php7.0-sqlite3 php7.0-mysql.
  3. Verify PHP 7 is working: php -v.

If it's working, you should get output like:

PHP 7 running under Ubuntu under WSL on Windows 10

Install Composer inside Ubuntu in WSL

Following the official instructions for downloading and installing Composer, copy and paste this command into the CLI:

php -r "copy('', 'composer-setup.php');" && \
php -r "if (hash_file('SHA384', 'composer-setup.php') === '544e09ee996cdf60ece3804abc52599c22b1f40f4323403c44d44fdfdd586475ca9813a858088ffbc1f233e9b180f061') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" && \
php composer-setup.php && \
php -r "unlink('composer-setup.php');"

To make Composer easier to use, run the following command to move Composer into your global path:

sudo mv composer.phar /usr/local/bin/composer

Now you can run composer, and you should get the output:

Composer running under Ubuntu under WSL on Windows 10

That's it! Now you have PHP 7 and Composer running inside Ubuntu in WSL on your Windows 10 PC. Next up, dominate the world with some new PHP projects!


how to install 7.1 from 7.0?

I think it should be as simple as replacing 7.0 with 7.1 in all the phpX.X-name packages in the apt-get install command. Haven't tested this yet, though, but I think that should work!

Fantastic. Works on Debian subsystem with latest Windows 10. I have a working D8 site.

We could possibly use ansible. Here is a quick and dirty proof of concept to install Drupal.

Before attempting to install Drupal with Composer, do:
sudo apt-get install php7.0-zip
so that Composer can unzip downloaded files.

Then follow instructions at to install Drupal
Then do:
composer require drush/drush in the project directory

Start php web server:
cd /path/to/doc/root (changing the path as required here, and elsewhere in this comment: in our case cd /var/www/html/some-dir/web)
php -S localhost:8888
drush rs
(On Debian subsystem I had to do
sudo apt-get install sqlite3
before drush runserver worked)

To get drush & drupal console working do:
/path/to/project-directory/vendor/bin/drush init
export PATH=$PATH:/path/to/project-dir/vendor/drupal/console/bin/drupal
source ~/.bashrc

Visit localhost:8888 to install the D8 site using the UI, choosing sqlite as the database :-)


sudo apt-get install mysql-server
sudo /etc/init.d/mysql start

and set up a database and user for your Drupal site.
We will want mod_rewrite:

sudo a2enmod

To make things simple to get started, I enabled mod_php7.0 and mod_prefork, removed Apache default vhost, and added the vhosts file from drupalvm to /etc/apache2/sites-available, but with
ServerName example.test
ServerAlias www.example.test

SetHandler application/x-httpd-php


edit the Windows hosts file (needs admin permission), adding: example.test

Visit example.test in a browser to play with the site now running on Apache + Mysql in the WLS :-)
You will probably need to start mysql and Apache manually each time you reboot.

Microsoft have to have some credit for this achievement. I know Macs benefits from Unix, but I cannot use Macs (too easy to break owing to drops and spillage, and increasingly impossible to repair), and I still find Linux a hassle on my Thinkpads for various reasons.

A gotcha: after installing Drupal to /var/www/html/some-dir/ with Composer, there was a Windows-specific file system error, which prevented Apache and drush rs servers from working correctly (but not a server started with php -S). No amount of changing Unix permissions and ownership got them working, nor did changing permissions allow me to rename some-dir/ to drupal/, which was a clue. So I made a new directory drupal/, moved the contents of some-dir/ to the new directory, the deleted some-dir. Of course it was necessary to change the Apache virtual host to match the new path. After that fix, both the server started with 'drush rs', and Apache, worked correctly.

Thanks Jeff!