Laravel Development Environment Setup
Laravel Development Environment Setup
Overview
This is a hybrid environment with the web browser and VS Code IDE installed in Win11 and all other dev tools installed in WSL2.
Win11: VS Code IDE with recommended extensions
WSL2: PHP, MariaDB/Postgres, Laravel, VS Code remote development extensions
This environment does not use docker inside WSL as VS Code extensions cannot work inside docker. Hence all the dev tools are installed natively in WSL and VS Code will automatically detect/install required extensions to work with WSL.
Software Used
- Windows 11
- Ubuntu 22.04 in WSL
- VS Code
- Laravel 10.x
Installation Steps
- Install Ubuntu 22.04 LTS in WSL
- Install required packages
apt install php-cli php-common php-curl php-mbstring php-mysql php-xml php-zip php-pgsql
- Development packages
apt install git
- Database
apt intall mariadb-server
orapt install postgresql
- Download the latest
composer
package to/usr/local/bin
- Use composer to install laravel.
Installing Language Support for VS Code
- Install Laravel IDE Helper for full VS Code intellisense support
composer require --dev barryvdh/laravel-ide-helper
- Add
/_ide_helper*
to.gitignore
to ensure ide-helper files are not committed into the Git repo - Run
php artisan clear-compiled
- Run
php artisan ide-helper:generate
to create Facades - Run
php artisan ide-helper:models
to create Models - Use the default “no” when prompted.
- In
composer.json
add the below to ensure IDE helper PHP files are recreated whenever composer updates dependencies.
1
2
3
4
5
6
7
8
9
"scripts": {
"post-update-cmd": [
"@ide-helper"
],
"ide-helper": [
"@php artisan ide-helper:generate",
"@php artisan ide-helper:models -N"
]
}
- You can now
composer run ide-helper
anytime to recreate the ide-helper files.
PHP Code Formatting Using Pint
- Laravel Pint is automatically installed in Laravel v10
- Install the Run on Save VS Code extension
- In VS Code
settings.json
, add the following code to run pint whenever any.php
file is saved.
1
2
3
4
5
6
7
8
"emeraldwalk.runonsave": {
"commands": [
{
"match": "\\.php$",
"cmd": "${workspaceFolder}/vendor/bin/pint ${file}"
}
]
}
Rejecting non-Pint Compliant Code in Git
A git pre-commit
hook can be used to reject any PHP code is not compliant with Pint.
Note that Pint does not support blade templates at this time.
Setting up the git hooks directory in the project root directory is useful as it makes it available for every member of the project team. This provides a consistent environment and behavior for everyone which is great.
- Setup a new
.githooks
folder and copy thepre-commit
bash script for customization.
1
2
mkdir .githooks
cp .git/hooks/pre-commit.sample .githooks/pre-commit
- Add the below lines to the top of the
.githooks/pre-commit
bash script.
1
2
3
4
5
6
7
# Ensure the committed files are Pint compliant.
echo "Running Laravel Pint..."
if ! ./vendor/bin/pint --dirty --test
then
echo "Pint errors detected, git commit aborted."
exit 1
fi
- Change the git hooks directory from the default
.git/hooks
to.githooks
by runninggit config core.hooksPath .githooks
. To automate this for other developers, add the below JSON snippet to thecomposer.json
/scripts
/post-autoload-dump
section. Thepost-autoload-dump
scripts are executed whenever composerinstall
orudpate
commands is invoked. See Composer Scripts.
1
2
3
4
5
6
7
"scripts": {
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"@php artisan package:discover --ansi",
"git config core.hooksPath .githooks"
],
}