For many years, developers who like Windows OS have lived on dual boots and virtual machines. Microsoft’s introduction of Windows Subsystem for Linux (WSL) was one of the best moves the company made in an effort to make Windows a one stop for both personal computing and development. I live on both Windows and Linux and therefore to me, this was a long awaited feature.
WSL is disabled by default and can be enabled by going to “Add Optional Programs and Features” in Windows control panel or running the following command followed by a reboot.
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
In settings, enable “Developer Mode” and then download your favorite distro from Microsoft Store. Once download is complete, open Command Prompt and type
bash. Follow the prompts to create an account.
In the last few years, I have been developing on SP4 with high end specs and it has served me well. My environments ranged from WAMP servers to custom installations. It became tricky developing solely on Windows and deploying to Linux servers. I encountered random and unexpected behaviors in applications that consumed time to fix. I then settled on Homestead using Vagrant and Virtualbox. Virtualbox creates virtual network Ethernet cards that also became a pain when developing React Native apps using Expo. By default, Expo picked the Virtualbox Ethernet IP to serve the app, which obviously was not available to Android phone for testing. I had to disable those virtual cards every time I was developing using Expo.
When running the environment on top of using multiple tabs on Chrome, phpStorm, Git terminal and a few other programs on the host machine, CPU usage would spike and response on the idea would decrease.
After searching for a better and viable solution online, I came across Laragon, a portable isolated dev environment, I was sold… almost. I checked them out. Installed, configured and up and running within a few minutes. When I launched it, I found that I had Commander terminal and Notepad++, all of which I didn’t need since I use Sublime Text and VS Code for various quick code editing tasks. I liked the portability aspect and ability to create sites and setup hosts file automatically, and the small memory print. The problem was, I has to switch root directory on the program each time I was working on a different project and I didn’t need many of the other features they have to offer. I spend a lot of time on Linux terminal and thus I would still have to work with Laragon and Ubuntu in addition. So, why not make Ubuntu on WSL do the same work? A few minutes of initial setup, then replicate in future projects. WSL has extremely low memory usage and barely affects startup time on my machine. So in my process to customize WSL, this is what I came up with.
Next time you need to create a new site, simply open Ubuntu or Bash from your Windows host as administrator, type new-site and hit enter. Answer the 2 prompts (site domain e.g. mysite.test and document root e.g. mylaravelsite/public) and you are all set.
What this code does.
You should now see the site load on your browser at http://mysite.test
Since you cannot run a ‘true’ service on startup in WSL, you can use this workaround to run a script when the machine boots.
mkdir -p ~/.local/bin/ echo "service apache2 start" >> ~/.local/bin/mydev-env.sh echo "service mysql start" >> ~/.local/bin/mydev-env.sh chmod +x ~/.local/bin/mydev-env.sh
Then, allow your WSL account to run root commands without password
sudo visudo john ALL=(root) NOPASSWD: /home/john/.local/bin/mydev-env.sh
Create a login script that runs this command
bash.exe -c 'sudo ~/.local/bin/mydev-env.sh'
Or simply create as scheduled task using this command
SCHTASKS /Create /SC ONLOGON /TN "Start My DEV Env" /TR "bash.exe -c 'sudo ~/.local/bin/mydev-env.sh'"
To run mailhog, you can either type
mailhog on the terminal or specify ips and ports.
mailhog \ -api-bind-addr 127.0.0.1:8025 \ -ui-bind-addr 127.0.0.1:8025 \ -smtp-bind-addr 127.0.0.1:1025
mhsendmail in your php.ini
sendmail_path = /usr/local/bin/mhsendmail
Your Windows mount directory can be anywhere. Just change the ln command to create the symbolic links to the specific directories.
Remember to backup your work often. You never know when lightening may strike! You can backup your personal files to a local external backup media and your code to a remote repository. Github, Gitlab and Bitbucket are some of the services I regularly use.
Happy coding!Tags: bash, linux, scripts, windows, wsl