We ran into the following error on one of our client’s servers that we periodically work on:

root@server:/ # pkg update -f
Updating FreeBSD repository catalogue...
pkg: repository meta /var/db/pkg/FreeBSD.meta has wrong version 2
pkg: Repository FreeBSD load error: meta cannot be loaded No error: 0
Fetching meta.txz: 100%    916 B   0.9kB/s    00:01    
pkg: repository meta /var/db/pkg/FreeBSD.meta has wrong version 2
repository FreeBSD has no meta file, using default settings
Fetching packagesite.txz: 100%    6 MiB   6.5MB/s    00:01    
pkg: repository meta /var/db/pkg/FreeBSD.meta has wrong version 2
pkg: Repository FreeBSD load error: meta cannot be loaded No error: 0
Unable to open created repository FreeBSD
Unable to update repository FreeBSD
Error updating repositories!

How to resolve this? After trying a few different options we eventually settled on re-installing the latest version of the pkg tool. You can identify the most recent packaged version of pkg by looking at the list of compiled packages at https://pkg.freebsd.org/FreeBSD:12:amd64/latest/All/ - EDIT: Since directory listing is now forbidden on that server you are best off looking at FreshPorts to at identify potential versions.

root@server:/ # fetch -qo - https://pkg.freebsd.org/FreeBSD:12:amd64/latest/All/ | grep 'href="pkg-[0-9]'
<tr><td class="link"><a href="pkg-1.14.4.txz" title="pkg-1.14.4.txz">pkg-1.14.4.txz</a></td><td class="size">            6786424</td><td class="date">2020-Jun-11 01:07</td></tr>

So we’ve identified (at time of writing) that 1.14.4 is the current version, so to fetch and install we execute:

root@server:/ # fetch https://pkg.freebsd.org/FreeBSD:12:amd64/latest/All/pkg-1.14.4.txz
root@server:/ # pkg add -f pkg-1.14.4.txz

Once that is done we end up with a working package system again!

note

The URL https://pkg.freebsd.org/FreeBSD:12:amd64/latest/All/ no longer allows for directory listings so the easiest way to locate the current version it to look on the Freshports site at https://www.freshports.org/search.php?stype=name&method=exact&query=pkg