How to setup Laravel elegantly with LocalWP by Godwill Barasa

Godwill Barasa
5 min readJul 21, 2022
This elegant photo was shot by Thomas William

You might, or might not have heard of LocalWP. Simply put, it is a game-changer for WordPress theme developers like myself by providing developers with advanced workflow features. It has revolutionized my experience in developing WordPress Applications over the last couple of months that I have been using it.

Backstory

Yes, a couple of months. Before I had the old LAMP stack tutorial-advised XAMPP for the last three years. Everything was super fine until I got a job at an agency where not only was I dealing with a lot of sites, but also mitigating versions of specific packages was literally hell. These projects were mostly from different eras with different dependencies ie PHP versions.

Switching from one PHP version to another during projects on XAMPP, although possible was hell, and doing that concurrently as I switch between different projects during my day was also going to be a definite party pooper!

Adding to this conundrum: UX/UI for XAMPP has not changed over the last 10 years or so. What if I need a database instance that’s not MySQL?

Clearly old XAMPP is not going to cut it. I needed to handle such exceptions more efficiently so that I could hit my KPIs like Will Smith hit Chris Rock.

Me hitting my KPIs

Separation of concern, do what thou wilt

During a vision, while at Mt. Kenya (I meant, Googled when I was at a cabin in Nanyuki Town), I found the Local, in all its glory, Local sets up WordPress for you. One-click and your site is ready to go, SSL included! It also offers root SSH access, WP-CLI, and the ability to hot-swap PHP environments for easy testing!

Said cabin in the mountains

Very cool! I thought. I have finessed my way into a very fluid workflow. The Dev-Ops Engineer in me was very satisfied. I felt like a God!

Until !

Me dreading for the day I was going to have to switch my setup

The Lore of Laravel

Three months down, the line with my perfect setup. The agency I work for started getting a ton of Laravel-related work. I had to modify my current scenario and going back to my ex (XAMPP) was not going to cut it, the relationship was abusive and it did not understand PHP environment variable boundaries

Here is how I did it

Setup LocalWP

  1. Head over to Local website and click on the download button
  1. Once installed, click that green “+” button usually on the bottom left of your screen
  2. Give the site a name (let’s say “CoverLetterGenerator”)
  3. Select the environment your want (the Preferred Nginx will work just fine with Laravel)
  4. Enter your WP username and password (these are required now but will be deleted later)
  5. Click add site.
  6. Once the site is created, update the PHP version to the latest 8.0.x (as of today, Local only goes up to PHP 8.0.0, but hopefully this will be resolved soon).
  7. Add SSL

Install Laravel and a New Laravel Project

Download composer from here. In addition, I recommend installing NodeJS which comes pre-packaged with NPM.

Those are found here, but the basic command will be:

composer global require laravel/installer

Note: You need to run the above command (and all php artisancommands) from the Local machine. To do this, in the Local app, right-click on your new Laravel site, and select “Open Site Shell”. If you change to the laravel/appdirectory, you’ll be able to run these commands.

Once you have Larvel installed globally you can do the following:

  • In Site Shell Terminal, change to the “Laravel” directory you just created (cd ../.. most likely) .
  • Delete the currentapp folder (either in your Finder/Explorer window or via command line)
  • Then run the following command from the new site directory:
laravel new app

This will create a new Laravel project within the app directory, and will include the public folder where Local will normally look for it.

At this point, you should be able to click “View Site” in the Local app, and it should show you the Laravel welcome page at htts://laravel.local, for instance. If you’re getting a 502, 404, or some other error, you may need to restart the server:

If you get a minimum PHP version requirement

Temporary fix! Local only includes PHP support to 8.0.0, but Laravel’s current dependencies require PHP 8.0.2.

So, on the homepage, if you see a fatal error that says Your Composer dependencies require a PHP version ">= 8.0.2", you can do one of two things:

1. According to the Composer docs here, you should emulate your PHP environment like so in your composer.json file:

{
"config": {
"platform":{
"php":"8.0.2"
}
}
}

This adds a spoof to your platform so that even though you’re not really running PHP 8.0.2, it will install dependencies as such.

2. Remove the vendor directory and run:

composer install --ignore-platform-reqs

Obviously, this is a temporary solution to get you going. You can follow this thread to get the latest: https://community.localwp.com/t/add-support-for-php-8-1-in-local/29352/8

Connecting the Database

In your .envfile, you can replace the following credentials (assuming you haven’t changed the default LocalWP

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_SOCKET="/Users/yourusername/Library/Application Support/Local/run/somethinglikethis12312123/mysql/mysqld.sock"
DB_DATABASE=local
DB_USERNAME=root
DB_PASSWORD=root

The above will connect to the Local DB. If you open your database using TablePlus, you can go in and delete the standard wp_ tables.

Finally, you can seed your Laravel database with the command:

php artisan migrate

After running the above command, you can refresh your database in Table+ and see the default seeded database.

Misc

My name is Godwill Barasa, a Senior Web Developer at Belva Digital, Building (and occasionally designing) exceptional digital experiences.

I am currently learning Laravel, trying to get a fundamental understanding of the framework thanks to an amazing course and future content will revolve around this.

Catch me on Twitter, and let’s banter about tech, career advice, cars, women, or anything in between.

My resume is here. Feel free to reach out for side gigs because weuh !

--

--

Godwill Barasa

Award-winning Software Developer, Writer and Designer