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('https://getcomposer.org/installer', '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!

Comments

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 https://github.com/drupal-composer/drupal-project 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
or
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 :-)

MYSQL & APACHE METHOD

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

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

sudo a2enmod
rewrite

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

do:
a2ensite
vhosts

edit the Windows hosts file (needs admin permission), adding:
127.0.0.1 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!

thx man!

install file was corrupt with your script though, took this from composer site, please update:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '93b54496392c062774670ac18b134c3b3a95e5a5e5c8f1a9f115f203b75bf9a129d5daa8ba6a13e2cc8a1da0806388a8') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"