Like an idiot while I was going to do a “quick” bit of work to get ahead for next week on Friday. First I decided it was time to update Homebrew before I really got into diagnosing a problem for a client. Of course mayhem ensued with the wpcli
commands no longer working with LocalWP after I ran brew update && brew upgrade
.
There were two main errors to search. First the icu4u
library wasn’t loaded. This library is used for unicode characters and is a requirement of PHP
, MySQL
and a number of other things that Homebrew was managing. The specific error is below.
Library not loaded: /opt/homebrew/opt/icu4c/lib/libicuio.73.dylib
Further down in the same error block there was a note about PHP 8.0 not being installed.
Error: php@8.0 has been disabled because it is a versioned formula!
After searching and finding these general directions to remove and reinstall LocalWP, I was still out of luck. Worse, I no longer had any development sites installed locally as I removed them all during the uninstall process.
Fixing with Homebrew
Ultimately after a bunch of fruitless searching I came across this article saying that PHP 8.0 was no longer supported in Homebrew. To continue to have it installed I needed to get it from a different repository with the following commands.
- Add the new repository to Homebrew –
brew tap shivammathur/php
- install php 8.0 –
brew install shivammathur/php/php8.0
Now my wpcli
commands work as expected and I no longer see the errors from icu4c
. The big problem with this, and anything installed with Homebrew, is that we’re messing with my main PHP version in my shell. I’m always running PHP 8.0 now, even on projects that may need earlier or later versions of PHP.
Fixing it with nix-shell
Of course you’ll need to install nix-shell
first with these instructions.
A far more portable and repeatable way to fix my issue is to use nix-shell
. Instead of messing with the main versions of software running on my machine, I’m only messing with the current instance of the terminal that I’m working in.
The easy way to test this is to run the following command which temporarily installs php8.1
and wpcli
in my terminal.
nix-shell -p php81 wp-cli
You’ll still get an error connecting to the database because the shell doesn’t understand how to make the connection to LocalWP but that can be fixed by going to the Database tab in LocalWP and copying the socket value into the DB_HOST
value. It should look something like this:
define( 'DB_HOST', 'localhost:/Users/curtismchale/Library/Application Support/Local/run/CiNKEaTuy/mysql/mysqld.sock' );
Now when I run a wp-cli
command I have a connection which means the next step is to create a shell.nix
file I can place in the root directory of my project which has the values I need setup already so I don’t have to manually run them each time. This is what my shell.nix
file looks like.
let
nixpkgs = fetchTarball "https://github.com/NixOS/nixpkgs/tarball/nixos-23.11";
pkgs = import nixpkgs { config = {}; overlays = []; };
in
pkgs.mkShellNoCC {
packages = with pkgs; [
wp-cli
php81
];
}
Then when I open up my terminal I run nix-shell
and the current shell is setup with php81
and wp-cli
every time. I can move the same shell.nix
file to a new site, or a new computer and run nix-shell
and I’ll get the same environment in my shell every time.