Cam Scott

Editing PHP Memory Limits With Laravel Valet

Laravel Valet provides a shockingly easy development environment, but bouncing between versions of PHP can introduce a few frustrating gotchas. (One of those "blogging so I don't forget it" posts.)

Nowadays, I use Laravel Valet as the development environment for all my PHP projects, from Laravel to WordPress to Statamic. The project does an excellent job catering to the middle 80% of use cases and often just works when serving new projects.

To accommodate a variety of products, Valet has an isolate command to specify per-project versions of PHP. Or, if you're like me—a jackass—you can change Valet's PHP installation globally via valet use PHP@{version}.

Valet doesn't actually manage your PHP installations directly; instead, it uses Homebrew. When you run the use command, Valet executes a series of commands to unlink the previous version of PHP, re-link the new version, and update some configurations to ensure the new version works as expected.

During these updates, Valet will reset any .ini customizations you may have made—including memory limits, which can impact file uploads, larger test suites, and many other facets of your projects.

To update memory limits, run these commands:

1cd /usr/local/etc/php/8.2/conf.d
2nano php-memory-limits.ini

You'll see the .ini options that govern memory limits, likely set with their default value of 512M. Change these to whatever you like, close the file, and run valet restart to load the updates.

Bonus Tricks

Trying to Install Older Versions of PHP

You can only install supported versions of PHP directly with Homebrew. As of mid-2023, that's 8.0 and up. However, if you need to dust off a project, you can access older versions of PHP with the following commands:

1brew tap shivammathur/php # You only need to do this once, regardless of which PHP version you install.
2brew install shivammathur/php/php@7.4 # Or whatever version you need
3brew link php@7.4 # Ensuring the version number matches
4valet use php@7.4

You'll likely only need to run this process once per version. Once installed, you can toggle between available PHP versions with valet use or valet isolate.

Composer Syntax Errors

When using valet use to switch from an older version of PHP to a newer one, you might run into an error like this:

1Fatal error: Composer detected issues in your platform: Your Composer dependencies require a PHP version ">= 8.0.2". You are running 7.3.33. in /Users/username/.composer/vendor/composer/platform_check.php on line 24.

To resolve this, run the following commands:

1brew unlink php@7.4 # or whatever version you're using
2brew link php
3valet use php

If you run php -v, it should list the latest PHP—8.2 at the time of writing.

Weirdo icu4c Errors

When first switching to older versions of PHP, you might run into some scary-looking errors like these:

1dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib

There are myriad StackOverflow articles suggesting brew cleanup or brew reinstall icu4c. Depending on the origin of the error, though, you might need something stronger. For a surefire resolution, reinstall a clean copy of your PHP version:

1brew reinstall shivammathur/php/php@7.4

This command might take a minute, but running php -v after it completes should return a PHP version number as expected. Make sure to follow the steps above to tweak your memory limits!

Last updated on April 5, 2023