Local WordPress Development on macOS with Valet+

Introduction

I received a MacBook Air with M1 chip at work earlier this year. A decent machine for sure, but unfortunately it was the 8GB version which isn't really enough for web development, especially if you need to use tools like Adobe Photoshop. In an effort to reduce resource usage on my machine I started to look into replacing some apps with less resource intensive alternatives. To start, I choose to replace MAMP and Local with Valet+.

Valet+ is a minimal and fast local PHP development environment. It's a fork of Laravel Valet with added functionality including a few things I was specifically interested in:

  • Framework specific develpment tools such as WP-CLI.
  • Mailhog for catching emails from WordPress.
  • PHP version switching. Unfortunatetly, Valet+ doesn't support different PHP versions across multiple sites at this time. Laravel Valet does however using the isolate command.

Most if not all of the above can be used with Valet but they will need to be installed and configured whereas Valet+ includes them be default.

Installation

Valet+ has no UI, everything must be done using commands through bash/zsh.

1 - Install or update Homebrew.

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew update

2 - Install the Homebrew PHP tap for Valet+.

brew tap henkrehorst/php

3 - Install PHP 7.3, 7.4, and/or 8.0 using Homebrew.

brew install [email protected] --build-from-source
brew install [email protected] --build-from-source
brew install [email protected] --build-from-source

If you run into issues installing PHP:

4 - Link the PHP version you want to use using one of the commands below.

brew link [email protected] --force
brew link [email protected] --force
brew link [email protected] --force

5 - Install or update Composer.

brew install composer
composer self-update

6 - Install Valet+ using Composer.

composer global require weprovide/valet-plus

7 - Add export PATH="$PATH:$HOME/.composer/vendor/bin" to .bash_profile ir using bash, or .zshrc if using zsh.

8 - Run the valet fix command to fix any installation problems that prevent Valet+ from working.

9 - Install Valet+.

valet install

Once installed, try pinging example.test. If Valet+ was installed correctly you should get a response on 127.0.0.1.

ping -c 4 example.test

The official installation instructions can be found in the Valet+ Wiki.

Serving Sites

1 - Create a directory for your sites and cd into it.

mkdir Valet\ Sites
cd Valet\ Sites

2 - Use the valet park command to register the current directory as your projects directory. All directories within this directory will be accessible in your web browser at http://example.test where example is the name of the directory.

Database

Valet+ automatically installs MySQL 5.7, I advise sticking with this unless you absolutely must have the latest version (MySQL 8). I opted to go with MySQL 8 and spent a number of hours purging my system of all MySQL installs, files, and folders, and installing MySQL 8 through Homebrew. If you also choose this route, make sure to back up any databases before removing MySQL.

brew install mysql

After I installed MySQL 8 I ran mysql_secure_installation to change the password and to further secure MySQL.

Sequel Ace

Sequel Ace, as described by the developers, is the "sequel" to the longtime macOS tool Sequel Pro. This can be used to access and manage your databases.

New Connection

1 - Download Sequel Ace.

2 - Install.

3 - Open and add your connection details:

  • Name: MySQL 8 or whatever you want to use as a label for the connection.
  • Host: localhost
  • Username: root
  • Password: Leave blank, or if you changed it via mysql_secure_installation or other means, use that password. The password could also be root if you opted to stick with MySQL 5.7 installed with Valet+.

4 - Click the Add to Favorites button if you plan to use the connection again.

5 - Click the Test connection button; if the connection is successful, click the Connect button.

Database Users

Since I'm working locally and have secured MySQL I opted to create one user for all databases.

1 - Click Users in the menu bar.

2 - Click the Add User icon (plus sign) at the bottom-left.

3 - Under the General tab, enter a Username and Password.

4 - Under the Global Privileges tab, select everything in the Database and Tables box except for References and Trigger.

  1. Click the Apply button.

WordPress Site Example

Use WP-CLI to download and install Wordpress.

1 - Create a directory in your Valet projects directory and cd into it.

mkdir example
cd example

2 - Download the latest version of WordPress.

wp core download

3 - Create a wp-config.php file and enter the database password when prompted.

wp config create --dbname=wp-example --dbuser=wp-example --prompt=dbpass

4 - Create the database.

wp db create

5 - Finally, install WordPress.

wp core install --url=example.test --title="Example" --admin_user=<username> --admin_password=<password> [email protected]

6 - Access the site in your browser at example.test.

Conclusion

I did run into a couple of issues during installation as mentioned above, but everything has worked perfectly since. I don't have to start any apps or services when booting up my system and setting up a new site takes only a couple of minutes. The sites themselves are also very fast. I will likely default to this setup on macOS going forward.

I need to find an equal for Windows next, perhaps Laragon?