Home / Blog / Setting up a developer environment with WSL2 on Windows

Setting up a developer environment with WSL2 on Windows

17 May 2021

Windows has a fairly new system that allows you to run Linux in a highly optimised virtual machine. This means you can connect to a printer (yay, Windows) and run ansible/install poppler-util/make node-gyp work (yay, Linux) without it being toooo much of a pain in the arse. the virtualised Linux is also faster for many operations that the native window (composer install/npm i are much faster generally).

Install WSL2, Ubuntu and Windows terminal

Follow the instructions here: https://docs.microsoft.com/en-us/windows/wsl/install-win10 – you need Windows 10 Pro edition but you should have that anyway as you should have you hard drive encrypted with Bitlocker in case you lose your laptop and have sensitive information or keys on it. Outlandish will buy you a licence if you work for Outlandish and don’t already have one.

Install Ubuntu 20.04 from the Windows Store: https://www.microsoft.com/en-gb/p/ubuntu-2004-lts/9n6svws3rx71?rtc=1&activetab=pivot:overviewtab

Install Windows terminal following the instructions here: https://docs.microsoft.com/en-us/windows/terminal/get-started – it’s a convenient way to access your Linux subsystem

Update Apt and get the latest packages

sudo apt update
sudo apt upgrade

Install apache and configure it to listen on port 8081

Apache will listen on port 80 by default, but you might have something on Windows already listening on that port (e.g. an old XAMPP installation) so it’s a good idea to add another port to reduce the chance of conflicts. If you don’t have anything listening on port 80 then this linux-powered apache will also listen on 80.

sudo apt -y install apache2
sudo cp /etc/apache2/ports.conf /etc/apache2/ports.conf.bak
sudo bash -c 'echo "Listen 8081" >> /etc/apache2/ports.conf'
sudo service apache2 restart

http://localhost:8081 should now work on the host windows machine

Change the default path for your web projects from /var/www/html to /var/www for convenience

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/000-default.conf.bak
sudo sed -i 's///' /etc/apache2/sites-available/000-default.conf
sudo sed -i 's|DocumentRoot /var/www/html|DocumentRoot /var/www|' /etc/apache2/sites-available/000-default.conf

http://localhost:8081 should now serve the parent dir (/var/www)

change web root to current owner

sudo chown -R $USER:$USER /var/www
sudo chmod -R 755 /var/www


Set apache to run as current user

So that you can view and edit any files it makes, and that apache can read the right files and folders

sudo cp /etc/apache2/apache2.conf /etc/apache2/apache2.conf.bak
sudo sed -i 's/${APACHE_RUN_USER}/'"$USER"'/' /etc/apache2/apache2.conf
sudo sed -i 's/${APACHE_RUN_GROUP}/'"$USER"'/' /etc/apache2/apache2.conf


Install PHP

sudo apt install -y php libapache2-mod-php php-mysql php-xml php-cli php-curl


Install xdebug

sudo apt-get install -y php-xdebug
sudo cp /etc/php/7.4/apache2/conf.d/20-xdebug.ini /etc/php/7.4/apache2/conf.d/20-xdebug.ini.bak
sudo bash -c "echo 'xdebug.remote_enable=1' >> /etc/php/7.4/apache2/conf.d/20-xdebug.ini"
sudo bash -c "echo 'xdebug.remote_port=9000' >> /etc/php/7.4/apache2/conf.d/20-xdebug.ini"
sudo bash -c "echo 'xdebug.remote_log=\"/tmp/xdebug.log\"' >> /etc/php/7.4/apache2/conf.d/20-xdebug.ini"

set the xdebug remote host to be the windows IP address, and add this to .bashrc so that it gets reset each time WSL restarts

We extract the Windows IP from `/etc/resolv.conf` and set it as an environment variable called `WSLIP`, then we overwrite the `xdebug.remote_host=` line in `/etc/php/7.4/apache2/conf.d/20-xdebug.ini`

WSLIP=$(grep nameserver /etc/resolv.conf | cut -d ' ' -f2)
sudo bash -c "echo "xdebug.remote_host=$WSLIP" >> /etc/php/7.4/apache2/conf.d/20-xdebug.ini"
sudo cp ~/.bashrc ~/bashrc.bak
sudo chmod -R og+w /etc/php/7.4/apache2/conf.d/ #current user need to be able to write to the file
sudo echo 'sed -ri "s|xdebug.remote_host=[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}|xdebug.remote_host=$WSLIP|" /etc/php/7.4/apache2/conf.d/20-xdebug.ini' >> ~/.bashrc


Install composer

cd ~ && curl -sS https://getcomposer.org/installer -o composer-setup.php && sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer


Install MariaDB

sudo apt install -y mariadb-server
sudo service mysql start


Create user DB ‘admin’ user with password ‘password’

sudo mysql -uroot -e "CREATE USER 'admin'@'localhost' IDENTIFIED BY 'password';"
sudo mysql -uroot -e "GRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost' WITH GRANT OPTION;"
sudo mysql -uroot -e "FLUSH PRIVILEGES;"


Install node via NVM

sudo curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
source ~/.bashrc
nvm install --lts


Install build-tools, python and ssl libs

sudo apt install -y build-essential libssl-dev libffi-dev python3 unzip


Copy your SSH key from windows and set the right permissions

cp /mnt/c/Users/$USER/.ssh/id_rsa ~/.ssh/id_rsa
cp /mnt/c/Users/$USER/.ssh/id_rsa.pub ~/.ssh/id_rsa.pub
chmod 600 ~/.ssh/id_rsa


Install pip and Ansible

(a fairly old version that’s compatible with some of the old libraries)

cd ~
curl https://bootstrap.pypa.io/pip/2.7/get-pip.py --output get-pip.py
sudo python2 get-pip.py
python2 -m pip install "ansible==2.8.5"
python2 -m pip install boto
python2 -m pip install boto3
echo "export PATH="$PATH:~/.local/bin" >> ~/.bashrc