Turbocharge your development with WSL 2 and Laravel Valet
Martijn | Developer
If you use Windows and need a native solution instead of tools like Laragon, it's time to explore Windows Subsystem for Linux 2 (WSL 2). In this article, we'll show you to make this switch and optimize your workflow with Laravel Valet.
My personal experience
While Laragon is great for beginners, it can be very heavy on system resources and really slow down your workflow, especially when managing larger projects.
I've used Laragon for a long time and often found myself frustrated with certain performance issues and strange errors. Getting Redis to work is not easy, queries can take really long despite being optimized and rendering complex Livewire components can take many seconds.
Switching to WSL 2 solves these problems since it allows you to run a full Linux environment within Windows. It feels like you are suddenly working on a more powerful machine than before.
The satisfaction I personally get from this is beyond measure.
Is it the right choice for you?
If you're excited but still doubting if this is the right choice for you, let me share some insights to help you decide.
- For advanced users only: this setup works perfect when correctly configured, but if you run into any errors they can be very difficult and time-consuming to fix. Make sure you know what you are doing.
- No GUI: there is no GUI to use for enabling/disabling services. If you don't like doing anything in the terminal, stick to a Laragon alternative like Laravel Herd.
- File permission issues (GitHub Desktop): GitHub Desktop works fine with WSL 2, but a few small things don't work as expected. For example: you can't discard newly added files inside GitHub Desktop (you have to do this in Explorer or in the terminal).
- Automatically created files when copying: if you copy certain files from a Windows location to a WSL 2 location in Explorer,
Zone.Identifier
files will be created with a reference to the original location of the copied file. - Deleted files are gone immediately: when you delete a file in a WSL 2 folder, it will not be moved to the trash bin but permanently deleted.
- Trusting new repositories: every time you clone a repository you need to mark it as trusted in the app.
Let's start
We recommend doing this on a fresh Windows installation to avoid any strange errors. If you don't have this option and you encounter any issues, confirm that you have disabled similar tools.
Step 1 - Install WSL
The first step is to install WSL. Open PowerShell as an administrator and run the following command:
wsl.exe --install
Follow all the steps of the installation and create a password if you want to.
After the installation is complete, you can run WSL with the following command:
wsl
You should now be able to run Linux commands in the WSL environment.
Step 2 - Install PHP
With WSL installed, it's time to install PHP. Use the following commands to set it up:
sudo apt-get update
sudo apt install software-properties-common libnss3-tools jq xsel
sudo add-apt-repository ppa:ondrej/php
sudo apt install openssl unzip php8.2-cli php8.2-bcmath php8.2-curl php8.2-mbstring php8.2-mysql php8.2-tokenizer php8.2-xml php8.2-zip php8.2-fpm php8.2-redis
If you need another version of PHP, just change 8.2 to your desired version. However, you need to make sure that package is supported in the chosen version.
Optionally, you can install the following extensions:
sudo apt install php8.2-gd php8.2-intl
Step 3 - Install Composer
Composer is essential for managing your PHP dependencies. Install Composer with the following commands:
cd ~
curl -sS https://getcomposer.org/installer -o /tmp/composer-setup.php
HASH=`curl -sS https://composer.github.io/installer.sig`
php -r "if (hash_file('SHA384', '/tmp/composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
sudo php /tmp/composer-setup.php --install-dir=/usr/local/bin --filename=composer
Step 4 - Install MySQL
MySQL is a crucial part of most Laravel projects. Install MySQL with these commands:
sudo apt-get install mysql-server
sudo mysql
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '';
exit;
mysql -u root -p
You are, of course, not limited to MySQL and can use a different database engine.
Step 5: Install Redis (optional)
Redis can significantly improve the performance of your Laravel application. Install Redis as follows:
sudo apt update
sudo apt install redis-server
redis-server --version
sudo service redis-server start
redis-cli ping
When executing the last line, you should see PONG
in your terminal.
Step 6 - Install NPM
For managing your frontend dependencies, you'll need NPM. Install it using NVM (Node Version Manager):
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash
command -v nvm
If you don't get a response, close your terminal and reopen it. Then run:
command -v nvm
nvm ls
nvm install --lts
nvm install node
nvm ls
node --version
npm --version
Step 7 - Install Laravel Valet
Laravel Valet provides a minimalistic yet powerful development environment.
Install Valet with the following commands:
composer global require cpriego/valet-linux
echo 'export PATH="$PATH:$HOME/.config/composer/vendor/bin"' >> ~/.profile
source ~/.profile
valet install
cd ~/.valet/Sites
valet park
Step 8 - Enable SSL (optional)
Enable HTTPS with SSL certificates using the following commands:
valet secure
openssl x509 -outform der -in ~/.valet/CA/LaravelValetCASelfSigned.pem -out ~/.valet/CA/LaravelValetCASelfSigned.crt
valet secure [project]
Replace [project]
with your project folder's name (without .test
).
Step 9 - Add the domain to the hosts file
As a final step, add your project domain to the hosts file:
sudo nano /etc/hosts
127.0.0.1 [project].test
Optionally, you can navigate to the folder: C:\Windows\System32\drivers\etc
.
Open the file as an administrator to be able to save the changes.
How to view files in the WSL 2 directory
In the WSL 2 directory you will see one folder called Ubuntu by default.
Windows will automatically add 'Linux' to Quick access, but not immediately.
If you don't see it, you can use one of the following options to navigate there.
- Open Explorer and enter
\\wsl$\Ubuntu\
. - Press the Windows key + R and enter
\\wsl$
.
If this doesn't work, please reboot your device and try again.
Run WSL 2 on boot
If you notice that your projects are not working without starting WSL 2 first, you'll probably want to schedule it to run on boot. Read more here on how to do that.
Conclusion
By following these steps, you can transform your Laravel development environment into a powerful, efficient setup with WSL 2 and Valet. Say goodbye to the limitations and frustrations of slower tools, and enjoy a seamless, high-performance workflow.
Happy coding! 💻