PHP and Composer on the Windows Subsystem for Linux

In this guide, I’ll be showing you step-by-step what you need to do to install the Windows Subsystem for Linux, and then both PHP and the Composer package manager.

As a prerequisite, this requires an up-to-date version of Windows 10. Windows 8.1, 7, and below are unsupported. I will write about setting up PHP environments under legacy Windows versions in a later article.

Step One – Installing WSL

First, use your start menu or Cortana search bar to search for “windows features” – the first option provided will be a tool to add or remove features to your Windows installation. Select this.

Once the dialogue appears, scroll down until you can find Windows Subsystem for Linux. Tick the box and click OK. Windows will download and install this functionality, and may require a system restart afterwards.

Step Two – Installing a Linux distribution

After a restart, we can now choose a Linux distribution. Open the Microsoft Store app that you can find in your start menu. On the app, search for linux to bring up a list of available distributions.

You’re free to choose whatever distribution I want, but for a beginner, it’s my personal recommendation that you install Ubuntu as it’s well supported, easy-to-use, and does not require any complex configuration. Click on the one you want, and choose either Get or Install – depending on your system’s locale. Once it’s downloaded, the button’s text should change to Launch and Ubuntu will appear in your start menu. Fire it up.

Step Three – Installation and UNIX username

After opening the app, it will take a few minutes to install. This can take a while on older systems, but fret not, it will finish!

After a short while, it will prompt you to set a UNIX username. This should be a single word, all lowercase, with no spaces or symbols. My name is Oliver, so oliver is fine. Then set a password. This does not need to be the same as the password you use to log in to Windows. Don’t forget, your password won’t actually appear as asterisks as you type it, but it’s definitely there – it’s normal on these platforms.

Once that’s done, you’ll be presented with a command line and information regarding the system information.

Step Four – Installation of PHP

Before you install PHP, you might need to refresh Ubuntu’s repositories. I know this sounds complicated but think of it like Ubuntu checking an app store for what’s available. Type the following command to do this:

sudo apt update

It might prompt you that there are updates available, but you can take care of that later. Once the update is done, we can install PHP. Type the following command:

sudo apt install php

Sudo (short for superuser do) allows you to run commands as an administrator (root) – which is necessary for installing new software. Apt is Ubuntu’s package manager – necessary for pulling in said software. It will prompt you for the password you set earlier.

The package manager may be different if you installed a different Linux distribution. openSUSE for example uses Zypper instead.

Give the package manager some time to install and configure PHP and all of its prerequisites. Once it’s done, you can check that it’s fully installed using the following command:

php -v

This should print PHP’s version to the screen. As of this time of writing, Ubuntu installs PHP 7.4 by default. My output looks like this:

PHP 7.4.3 (cli) (built: May 26 2020 12:24:22) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies

Installing Composer

All this talk of package managers, when yes, PHP has its own package manager! If you’re familiar with JavaScript and/or Node.js, you’ll have definitely encountered NPM or Yarn, which perform basically the same functionality.

PHP’s package manager is called Composer, and this can be easily installed at the command line in the same way you did PHP:

sudo apt install composer

Similarly, you can check the version and status of Composer using

composer -v

Your output will look similar to mine, along with a long list of commands and some ASCII art:

Composer 1.10.1 2020-03-13 20:34:27

Now Composer will be available at the command line for you to install global packages with, or to install local dependencies outlined in a project’s composer.json file.

Optional: Install the Laravel installer and add Composer binaries to PATH

First, install the Laravel installer using Composer:

composer global require laravel/installer

After this is installed, you should normally be able to type laravel to access its installer, but you should get an error. Why?

By default, any software you install globally via Composer won’t be instantly accessible from your command line, because Composer’s directories have not been added to your PATH. If you’re not sure what this is, don’t worry – in a nutshell, this is information that tells your terminal where it can find the programs that you type in (like apt or php) so they can be run. Windows has this too.

To fix this, you need to open your aliases file in a text editor. We’ll use Nano, but feel free to use something else:

nano ~/.bash_aliases

In this file, type:

export PATH=~/.config/composer/vendor/bin:$PATH

Press CTRL+O to save, and CTRL+X to close Nano. With this change in place, we need to reload Bash’s configuration. You only need to do this once.

source ~/.bashrc

Now, your Composer global applications will work. Type laravel to confirm this.

Conclusions

By following this guide, you should be able to install the WSL under Windows 10 with Ubuntu, and have a functioning installation of PHP and Composer ready to go. I hope that this helps.

Please leave your comments below and if you found this useful, please let me know!

Why I continue to develop with PHP

I have no doubt that if you work in any realm of software engineering, or have at the very least spent enough time on Twitter, you’ll have encountered the seemingly endless cycles of arguments about what programming language, framework, or paradigm is best, and why everyone else is wrong.

PHP is no stranger to being a point of contention among different factions of web developers and is sometimes depicted as ancient, labouring technology. In reality, this is so far from the truth that it’s astonishing.

Why do people dislike it?

Some people can’t even give you an answer! It’s just fun to make fun of it. Though many do have genuine and understandable answers. A great deal of PHP code is written terribly, without conforming to the accepted standards that make working with the language, especially in corporate environments, significantly easier. Even common frameworks like CodeIgniter and the ubiquitous WordPress are guilty of this, hence the derision. Other codebases don’t make use of modern PHP functionality and suffer as a result.

A lot of other ridicule comes from lack of consistency in older function naming conventions and quirks of the language itself, but I really feel that this is grasping at straws at most, and such criticisms could be directed at other languages like JavaScript too, that aren’t flawless.

Node.js isn’t king, yet

Node.js, the JavaScript runtime powering many web applications is an insanely powerful tool with great frameworks to boot. It’s incredibly popular and is sometimes credited with being the future of all web development – JavaScript across the entire stack.

This, of course, doesn’t mean that PHP (and indeed other languages) are being forced out. Languages like PHP (and Python, Ruby, etc.) have diverse and mature ecosystems that power everything from simple blogs to large eCommerce monoliths. Let’s look at the statistics:

The PYPL index indicates that JavaScript sits at #3, and PHP shortly trails at #4, from this time of writing.

According to the TIOBE index, PHP continues to rise, sitting at around tenth place at the time of this writing.

Bare in mind, this includes usage of JavaScript across the entire stack, including front-end, where it remains an integral building block of the web.

The rise of Laravel… and WordPress

Backend Frameworks with the most stars on GitHub:

1. Laravel – 61.6K⭐️
2. Django – 52.1K⭐️
3. Flask – 52.1K⭐️
4. Spring Boot – 50.8K⭐️
5. Express – 50.2K⭐️
6. Ruby on Rails – 46.5K⭐️
7. Meteor – 42K⭐️
8. Nest – 30.5K⭐️
9. Koa – 30K⭐️

Which one is your favorite?— Marko ⚡ Denic (@denicmarko) September 26, 2020

The Laravel web framework, despite being relatively new, has skyrocketed its way to stardom on GitHub. This naturally isn’t representative of its usage, but its MVC, Rails-like architecture, command-line interface Artisan, database interactivity via object-relational mapping, templating engine, and testing suite for robust Test-Driven Development all out of the box, it’s a Swiss army knife that makes development not just easy, but immensely fast. I routinely make use of this framework, so I’m biased, but I highly recommend it.

For better, or for worse, WordPress continues to climb in usage. It’s easy to set up, its assortment of plugins and themes are unparalleled across all content management systems (CMS), and to its credit, has been working hard to rectify many of its historical security problems. The rise of JAMstack and static site generators have arguably made WordPress even more popular – by utilising it as an easy-to-use headless CMS that exposes its API to hydrate rapidly loading front-ends. Of course, if you’re looking to make money, then coding your own plugins and themes will remain profitable.

Okay I’m convinced! What now?

PHP is a fun and useful programming language to have in your arsenal, but don’t take my advice for it, go and explore the elephant ecosystem for yourself! I strongly recommend the following learning resources for getting on your way:

  • Codecademy is ideal for absolute beginners who want to learn language syntax without needing to install the language.
  • PHP The Right Way is something you should keep on hand at all stages of your career – it will remind you of the best ways to do things and common pitfalls to avoid. It will also guide you on how to install the language and package manager, Composer, on your operating system.
  • Laracasts is an incredible learning resource for both PHP and Laravel, with plenty of free learning paths for you to sample. I highly recommend a paid subscription if you’re serious about PHP. Great for Vue.js too.
  • Codecourse is another strong contender with plenty of free resources on PHP, Laravel, and other popular microframeworks.
  • PHP.net and of course, the language’s actual documentation.

Final thoughts

Ultimately, my tl;dr answer for why I develop using PHP in 2020 is because I like it. I grew up with it – and it remains and continues to mature as a respected and powerful tool on the Internet. This article’s purpose isn’t to discredit other languages, like JavaScript, but to reaffirm PHP’s place among them, and to serve as a reminder that it isn’t going anywhere anytime soon, regardless of how many paamayim nekudotayim jokes you make!