If you’re using WSL (Windows Subsystem for Linux) and want to build Laravel applications, the first step is getting the Laravel installer set up properly. While installation is easy, many developers (including me!) hit a snag: after restarting the terminal, the laravel
command stops working.
In this article, I’ll guide you through installing Laravel globally and fixing the “command not found” issue that happens after closing your terminal.
First, ensure you have Composer installed inside your WSL environment.
Then install the Laravel installer globally:
composer global require laravel/installer
This installs Laravel in your global Composer vendor/bin
directory, which is not added to your PATH by default.
laravel
command not found after restarting terminalAfter installation, you might be able to run:
laravel --version
But once you close and reopen the terminal, you’ll get:
command not found: laravel
This happens because the directory where Composer installs global binaries isn’t in your system’s $PATH
.
Run this command to find the full path:
composer global config bin-dir --absolute
You’ll likely see:
/home/your-username/.config/composer/vendor/bin
Copy that path.
$PATH
Now you need to permanently add that path to your shell configuration file so it loads every time you open the terminal.
~/.bashrc
):nano ~/.bashrc
export PATH="$HOME/.config/composer/vendor/bin:$PATH"
CTRL+O
, hit ENTER
, then exit with CTRL+X
.source ~/.bashrc
~/.zshrc
):nano ~/.zshrc
export PATH="$HOME/.config/composer/vendor/bin:$PATH"
CTRL+O
, ENTER
, CTRL+X
source ~/.zshrc
Close and reopen your terminal.
Then run:
laravel --version
You should now see the version without any error. 🎉
To verify the Laravel path is included:
echo $PATH
You should see something like:
/home/your-username/.config/composer/vendor/bin:...
With Laravel now properly added to your PATH, you’re ready to scaffold new projects:
laravel new my-app
cd my-app
code .
php artisan serve
You can create and manage your databases using tools installed on Windows, such as DBngin, and still connect to them from Laravel inside WSL.
Since WSL and Windows share the same network interface, you can connect to your DBngin database using 127.0.0.1
(localhost) and the port DBngin assigns (e.g., 3306 for MySQL).
.env
for Laravel:DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306 DB_DATABASE=your_db_name
DB_USERNAME=root
DB_PASSWORD=
Just make sure:
127.0.0.1
instead of localhost
in .env
, as sometimes localhost
resolves differently in WSL.This setup is great if you want to keep your development tools on Windows but code inside WSL!
Happy coding!