When pkg upgrade fails with a kernel mismatch

I tried installing updates on my FreeBSD install with pkg. I do it from time to time, but this one I saw the following prompt:

$ sudo pkg upgrade
Updating FreeBSD repository catalogue...
Fetching data.pkg: 100%   10 MiB  10.5MB/s    00:01
Processing entries:   0%
Newer FreeBSD version for package zxfer:
To ignore this error set IGNORE_OSVERSION=yes
- package: 1402000
- running kernel: 1401000
Ignore the mismatch and continue? [y/N]:

Hmm, seems the package requires a newer kernel. That sounds risky, let’s not do it:

Ignore the mismatch and continue? [y/N]: n
pkg: repository FreeBSD contains packages for wrong OS version: FreeBSD:14:amd64
Processing entries:   0%
Unable to update repository FreeBSD
Error updating repositories!

Still, freebsd-update (which is actually the recommended way to install updates!) didn’t help much, but gave a hint what’s happening:

$ sudo freebsd-update fetch
Looking up update.FreeBSD.org mirrors... 3 mirrors found.
Fetching metadata signature for 14.1-RELEASE from update1.freebsd.org... done.
Fetching metadata index... done.
Inspecting system... done.
Preparing to download files... done.

No updates needed to update system to 14.1-RELEASE-p8.

WARNING: FreeBSD 14.1-RELEASE-p7 HAS PASSED ITS END-OF-LIFE DATE.
Any security issues discovered after Tue Apr  1 00:00:00 UTC 2025
will not have been corrected.

I’m still running the EOLed 14.1. pkg upgrade tries to install 14.2 upgrades on top of 14.1, which is obviously not a great idea. This is the reason for the “kernel mismatch” we saw.

The solution is to just install 14.2:

$ sudo freebsd-update upgrade -r 14.2