If you haven’t figured this out yet — WSL is awesome! It gives Windows based developers the full power to use all the modern dev tools you need. If you haven’t installed and set it up — do that right now, for all the reasons I’ve previously outlined. Also, if you choose to use Ubuntu as your Linux distro, then I suggest customizing your terminal to match the default Ubuntu look.
If you are a WordPress developer, one of the key advantages of WSL is it means you can utilize the full power of WP-CLI. As noted in the WP-CLI installation details, there is limited support for Windows. This is correct, but also incorrect, because if you enable WSL you have the full power of Linux at your disposal on Windows. Once you do that, then simply put now the Linux details apply to you.
With WP-CLI + WSL in mind, the next step is to decide the best way to harness and utilize these together. You can certainly go full LAMP stack and manage everything from your WSL bash app. However, personally I think the best (read easiest and simplest) way to roll is combine the power of WSL and Windows. By that I mean you can keep your Apache, MySQL and PHP in your Windows system, but access it and WP-CLI through WSL bash. This mean you can continue to use all your Windows GUI tools and easy to setup packages like XAMPP or MAMP. Let me now show you how to do this, and great news, it’s actually really easy to setup.
Simply follow the easy WP-CLI install instructions for Linux based systems. You’ll run all these commands of course from your WSL bash app (i.e. Ubuntu).
Now, still in your WSL bash app, run this command:
That will install all the tools your need to run WP-CLI and access your MySQL server (running on Windows).
We need to be able to access your Windows file system folders from bash. So create a symlink to them like so (updating the path on the left to wherever you store your website folders):
Now you can simply go to this directory on the right side of this command in bash and from there you’ll be in your main Windows based “websites” folder. You can then create folders here with bash or just use Windows File Explorer, or other Windows tools.
Step 4: (the key trick!)
This is where the magic happens. You need to enable your bash MySQL to your Windows MySQL. It’s actually very easy. The key is using the IP address
127.0.0.1 instead of
localhost. You can read more about why this works in this WSL Github issue or in this example of connecting to XAMPP from WSL.
Next up, you’ll likely do a usual WP-CLI download of WP, from within your empty project folder like so:
Next, when you go to create your config file with WP-CLI, you need to make sure you do something like this, taking note of the IP address optionm that’s the KEY step:
wp config create --dbname=wpdbname --dbuser=wpdbuser --dbpass=securepswd --dbhost=127.0.0.1
That’s it. You’ve now connected to your Windows based MySQL server from WSL! You can now continue to use any and all WP-CLI commands from your WSL bash, because well you’re working in Linux in there, it’s no different from any pure Linux environment. Enjoy. Dev in Windows. Profit.