Setup a Local PHP & MySQL Development Environment using Windows Subsystem for Linux 2
Tags: php, wsl, beginners
Published: 2020-11-17 15:32:17

This post will guide you through setting up a local PHP development environment using MySQL on a Windows PC. We'll setup Windows to use WSL 2 and install the Ubuntu 20.04 LTS distribution. Then we'll install Git, PHP, Node and MySQL as well as install and configure Visual Studio Code and TablePlus.

## Prerequisites
To use WSL 2, you must be running Windows 10:
* For x64 systems: **Version 1903** or higher, with **Build 18362** or higher.
* For ARM64 systems: **Version 2004** or higher, with **Build 19041** or higher.
* Builds lower than 18362 do not support WSL 2. Use the [Windows Update Assistant](https://www.microsoft.com/software-download/windows10) to update your version of windows.

## Install WSL2
>The Windows Subsystem for Linux (WSL) is a new Windows 10 feature than enables you to run native Linux command-line tools directly on Windows.

Open PowerShell as an administrator and enter the following commands:

*Enable WSL*
```powershell
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
```

*Enable Virtual Machine Platform*
```powershell
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
```

Once you've done this, you'll need to restart your PC. Then [click here](https://docs.microsoft.com/en-us/windows/wsl/install-win10#step-4---download-the-linux-kernel-update-package) to download and install the Linux kernel update package.

Finally, open PowerShell as an administrator and set the default WSL version:

```powershell
wsl --set-default-version 2
```

## Install Ubuntu 20.04 LTS
>Ubuntu is a Linux distribution based on Debian and mostly composed of free and open-source software.

Open the Microsoft Store App and search for Ubuntu. Choose the 20.04 LTS Option and install.

![microsoft store ubuntu 20.04 LTS](https://dev-to-uploads.s3.amazonaws.com/i/3saf0i5f7kez9a32x1c2.PNG)

Once installed open the distribution from your programs:

![ubuntu program start menu](https://dev-to-uploads.s3.amazonaws.com/i/4e5dathx1jis85oj2nnc.PNG)

As this is the first use of Ubuntu 20.04 LTS, you will be asked to set up a new user:

![ubuntu 20.04 LTS new user](https://dev-to-uploads.s3.amazonaws.com/i/9jm9n3duq329obhqi5xl.PNG)

Finally, you'll need to update the Ubuntu 20.04 LTS Operating System:

*Download available package information*
```bash
sudo apt update
```

*Install available upgrades of all packages*
```bash
sudo apt upgrade
```

## Install Git
>Git is a distributed version-control system for tracking changes in source code during software development.

*Install git*
```bash
sudo apt-get install git-all
```

*Check install and version*
```bash
git --version
```

## Install PHP
>PHP is a general-purpose scripting language especially suited to web development.

*Install PHP package*
```shell
sudo apt install php
```

*Check install and version*
```bash
php -v
```

## Install Composer ##
>Composer is an application-level package manager for the PHP programming language that provides a standard format for managing dependencies of PHP software and required libraries.

[Click here](https://getcomposer.org/download/) and copy the script from the downloads page:

![composer download script](https://dev-to-uploads.s3.amazonaws.com/i/da2ppe87kwfh5pnd6x3x.PNG)

Paste the script into your Ubuntu 20.04 LTS terminal to download Composer. You'll then want to make Composer available globally:

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

*Check install and version*
```bash
composer -V
```

## Install Node & NPM ##
>Node.js is an open-source, cross-platform, back-end, JavaScript runtime environment that executes JavaScript code outside a web browser and npm is a package manager for the JavaScript programming language. 

We'll use [Node Version Manager](https://github.com/nvm-sh/nvm#about)(NVM) to manage Node and NPM. Use the following command to download and install:

```bash
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.0/install.sh | bash
```

*Check install and version*
```bash
nvm -v
```

You can then use NVM to install the latest version of node:

```bash
nvm install node
```

*Check install and version*
```bash
node -v
```
```bash
npm -v
```

## Install Visual Studio Code
>Visual Studio Code is a free source-code editor made by Microsoft for Windows, Linux and macOS.

>The Remote - WSL extension lets you use VS Code on Windows to build Linux applications that run on the Windows Subsystem for Linux (WSL).

[Click here](https://code.visualstudio.com/) to download and install the latest version of Visual Studio Code.

Once installed you will need to add the **Remote - WSL** extension:

![visual studio code remote wsl extension](https://dev-to-uploads.s3.amazonaws.com/i/xbqqonhlw0lphwflxel8.PNG)

Once the **Remote - WSL** extension has been installed, you can open folders within your Ubuntu system within Visual Studio Code.

##Install and Configure MySQL##

*Install MySql Server package*
```bash
sudo apt install mysql-server
```

*Start the MySQL Server service*
```bash
sudo service mysql start
```

*Open MySQL command-line client*
```bash
sudo mysql
```

You can now send commands to MySQL. First you will need to create a new user and assign permissions:

*Create a new database user*
```SQL
CREATE USER 'jack'@'localhost' IDENTIFIED WITH mysql_native_password BY 'myPassword';
```

*Create a new application database*
```SQL
CREATE DATABASE MY_APP_DB;
```

*Give the user permissions the new application database*
```SQL
GRANT ALL PRIVILEGES ON MY_APP_DB.* TO 'jack'@'localhost';
```

*Exit the MySQL command-line client*
```SQL
exit;
```

You can now log back into the MySQL command-line client as the user you have just created, use the newly created database and add a table.

*Open MySQL command-line client*
```bash
mysql -u jack -p
```

*Select the database you've just created*
```SQL
USE MY_APP_DB;
```

*Create a new table*
```SQL
CREATE TABLE users(
   id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
   name VARCHAR(30) NOT NULL,
   email VARCHAR(50)
);
```

*Add records to the table you've just created*
```SQL
INSERT INTO `users` (`name`, `email`) VALUES 
('Jack', 'jack@email.com'),
('Dave', 'dave@email.com'),
('Joey', 'joey@email.com');
```

*Exit the MySQL command-line client*
```SQL
exit;
```

*Install the PHP MySQL extension*
```bash
sudo apt-get install php-mysql
```

##Install and Configure TablePlus##

[Click here](https://www.tableplus.io/) to download the latest version of TablePlus.

Once installed open the application and select "Create a new connection..."

Enter the connection details, using the database and user you created previously:

![tableplus mysql local connection details](https://dev-to-uploads.s3.amazonaws.com/i/g9a2irqnsl7w0hqykpua.PNG)

| Field   |  Value |
|----------|------:|
| Name | MY_APP_DB |
| Host | 127.0.0.1 |
| User | jack |
| Password | myPassword |
| Database | MY_APP_DB |

Test the connection then Save:

![tableplus connection is okay](https://dev-to-uploads.s3.amazonaws.com/i/vj2k9hr0hv7kq9e8j9c1.PNG)

Once connected to the database, you'll see the records you created previously:

![tableplus table view](https://dev-to-uploads.s3.amazonaws.com/i/5ur0b7qsz7xsw4mr8j0l.PNG)

## Create a Test Application ##
You'll want to close any Ubuntu 20.04 LTS terminals you have open to allow any changes to take place. Once re-opened enter the following commands:

*Create a new folder*
```bash
mkdir my-test-application
```

*Change directory to the new folder*
```bash
cd my-test-application
```

*Create a new file*
```bash
touch index.php
```

*Open the directory in Visual Studio Code*
```bash
code .
```

This will open Visual Studio Code with the contents of the directory present in the workspace to the left of the screen:

![visual studio wsl extension](https://dev-to-uploads.s3.amazonaws.com/i/t0tju08o96xzzwpy608l.PNG)

Add the following to the index.php file:

```php
<?php
$servername = "127.0.0.1";
$username = "jack";
$password = "myPassword";
$dbname = "MY_APP_DB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT id, name, email FROM users";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  // output data of each row
  while($row = $result->fetch_assoc()) {
    echo "id: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
  }
} else {
  echo "0 results";
}
$conn->close();
?>
```

Save the file in Visual Studio Code, then go back to your Ubuntu 20.04 LTS terminal and enter the following command:

```bash
php -S localhost:8000
```

This will start a local development server. You can now open a web browser and navigate to localhost:8000.

![localhost](https://dev-to-uploads.s3.amazonaws.com/i/ei5ptc06akin1z0xrpss.PNG)

## Conclusion ##
You should now have everything you need to start developing on PHP. Enjoy!