Macports: Current platform "darwin 21" does not match expected platform "darwin 20"

root's picture

Switched from BigSur to Monterey and as normal this pops out when trying to upgrade macports:

fmbp16:~ root# port -v update
Error: Current platform "darwin 21" does not match expected platform "darwin 20"
Error: If you upgraded your OS, please follow the migration instructions:
OS platform mismatch
    while executing
"mportinit ui_options global_options global_variations"
Error: /opt/local/bin/port: Failed to initialize MacPorts, OS platform mismatch

The error is explaining very well what is happening and the recommendation is to visit the Migration website for further instructions.
My recommendation (and tested of course) is to just download the latest macports, the Catalina version, install it, then proceed as normal with port selfupdate command.

Afterwards you need of course to run sudo port upgrade outdated and you may get another error, related to Xcode. But that is normal too and you need to update that one too (via App Store). Once you are done with that, run again sudo port upgrade outdated and will be OK this time.

If at the end you get too many compile errors and you cannot get out of it, just follow the migration instructions.

If you think those instructions are too long, you have to do the following:

fmbp:~ root# port -qv installed > myports.txt
fmbp:~ root# port echo requested | cut -d ' ' -f 1 > requested.txt
fmbp:~ root# port -f uninstall installed
fmbp:~ root# port selfupdate
fmbp:~ root# port clean all
fmbp:~ root# curl -O
fmbp:~ root# chmod +x restore_ports.tcl
fmbp:~ root# ./restore_ports.tcl myports.txt

Thou shalt not steal!

If you want to use this information on your own website, please remember: by doing copy/paste entirely it is always stealing and you should be ashamed of yourself! Have at least the decency to create your own text and comments and run the commands on your own servers and provide your output, not what I did!

Or at least link back to this website.