Upgrading Laravel Homestead

3 minute read
April 27, 2021

Laravel Homestead is a virtualization that allows me to work in a separate system from my host computer and is relatively easy to destroy and recreate. Some other development environment options work directly on the host computer but I prefer the ability to separate the two.

Laravel provides an image with a number of options such as database providers, search tools, and common server utilities. Every now and then that box is updated which means I have the option of upgrading my local copy.

Because a virtual machine is temporal, upgrading to a new instance means the loss of configuration on the previous instance. This has bitten me a couple times in the past resulting in lost data and time spent looking for the necessary configuration to reapply.

Backup the Database(s)

Homestead provides an option to use Vagrant triggers to automatically dump any databases to .sql files prior to destroying the old instance, however I haven't used that feature with much success. From what I can tell, you must use the Homestead.yaml file to define all the databases you want backed up and I frequently create them manually after provisioning the box.

Instead, before upgrading I will look through both mysql and postgres databases to see if there are any that I should export. Many of my projects have seeders that can recreate the local development data so I don't worry about dumping them. The ones I'm concerned about are ones that have specific configuration or content such as a CMS database.

After identifying which ones need to be restored on the new copy I'll use a tool like Navicat to create a .sql dump.

Download the new Box

Running vagrant box update will pull down the latest copy of the box but doesn't replace the existing box. In order to do that we run vagrant box destroy. This will erase your current instance so be sure you've grabbed any data you need to transfer to the new one!!!

vagrant up will swap the previous instance for the newly downloaded one!

Configuration Changes

Change default PHP version:
The new instance will default to using php 8 by default and I prefer using 7.4. The command php74 will tell Homestead to default to 7.4 instead.

Increase the memory limit: 
Open /etc/php/cli/7.4/php.ini and increase the memory limit to something like "2G".

Raise the maximum open file count:
Open /etc/security/limits.conf and add * hard nofile 100000 and * soft nofile 100000 to the bottom. This addresses the Max Open Files error you might see when running a PHPUnit test suite.