It might seem that upgrading your Laravel Homestead environment should be as easy as running vagrant box update. You probably realized something was wrong when you tried to remove the previous version of the box, and got an error like this:
Box ‘laravel/homestead’ (v0.4.0) with provider ‘virtualbox’ appears to still be in use by at least one Vagrant environment. Removing the box could corrupt the environment. We recommend destroying these environments first:
The problem is that Vagrant doesn’t know how to migrate the changes from one virtual hard drive image into another virtual hard drive image. Updating requires you to start from a new hard drive image, then reapply changes through file synchronization and database migrations/seeds. While it’s not quite as simple as running vagrant box update, it’s still pretty easy if you’ve prepared and know the commands to run.
To upgrade your Laravel Homestead environment, follow these steps:
- You shouldn’t have any data inside the Homestead virtual machine that can’t be recreated from other sources. You should already be using Migrations and Seeders to create your development database, and your Laravel application files should be synchronized from the host’s filesystem. If not, and there’s any data you need to preserve from the virtual machine, make a backup now and keep it somewhere outside your Homestead virtual machine.
- Run the vagrant box update command to download the latest version of the Laravel Homestead Vagrant box.
- Next run the vagrant destroy command to destroy the existing Homestead environment.
- Now you’re ready to run the vagrant up command, which will bring up and provision the new Homestead Vagrant environment using the latest version of the laravel/homestead box. This will also run any provisioning commands in ~/.homestead/after.sh (for instance, to re-install packages), and also synchronize files from the host file system.
- Login to the Homestead virtual machine, and from your Laravel application directory, run the php artisan migrate --seed command to migrate and re-seed the database.
- (Optional) Find any old versions of the Homestead box by running vagrant box list, and delete them with vagrant box remove laravel/homestead --box-version version:C:\Users\David\Homestead>vagrant box list
laravel/homstead (virtualbox, 0.4.0)
laravel/homestead (virtualbox, 0.4.2)
C:\Users\David\Homestead>vagrant box remove laravel/homestead --box-version 0.4.0
Removing box ‘laravel/homestead’ (v.0.4.0) with provider ‘virtualbox’…