I upgraded from Mac OS X 10.10 Yosemite to macOS 10.12 Sierra. After that, any MacPorts command I ran returned the following error:
Error: If you upgraded your OS, please follow the migration instructions: https://trac.macports.org/wiki/Migration
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 problem is that MacPorts is specific to a Mac OS X platform. The best way to fix this problem is to follow the Migrating a MacPorts installation guide.
Totally Clean Slate
MacPorts migration provides a “restore_ports.tcl” script to restore all the ports (a.k.a. packages) once you have upgraded MacPorts. However, I decided to start from a clean slate and only install ports as I needed them.
port -qv installed > ~/ports_installed.txt
# Save list of ports you manually installed (exclude dependency ports)
port installed requested > ~/ports_requested.txt
# Uninstall all installed ports
sudo port -f uninstall installed
# Clean any partially-completed builds
# Remove leftover build files (this should be done automatically already)
sudo rm -rf /opt/local/var/macports/build/*
# Remove download files
sudo rm -rf /opt/local/var/macports/distfiles/*
Note: I noticed that executables like svn (from subversion port) were left behind in /usr/bin and could still be used.
Partially Clean Slate
If you decide to keep your existing ports and to use the “restore_ports.tcl” script, you might consider cleaning out inactive packages:
# Alternative command: port echo inactive
port installed inactive
# Remove all of the inactive ports
sudo port uninstall inactive
Install macOS-specific MacPorts
Install the latest MacPorts for your macOS version:
- Install or upgrade to the latest version of Xcode Developer Tools (free from the Mac’s App Store) and run it once.
- Install the latest Command Line Developer Tools by running this command in the Terminal app:
xcode-select --install
- Download and install the MacPorts package matching your Mac OS X version. It will overwrite the existing MacPorts installation.
Update MacPorts Configuration
The new MacPorts installer won’t modify the existing configuration file so you will need to update it manually. The updated configuration file “macports.conf.default” is located in the “/opt/local/etc/macports” directory. The old configuration file “macports.conf” is also in the same location.
Before overwriting the old file with the new, I recommend doing a file comparison:
diff macports.conf macports.conf.default
Note: If you prefer a nice graphical user interface, you can use the FileMerge application which comes with Xcode. Just run FileMerge and input the paths to the two files to compare them.
There wasn’t any significant difference between the two files (beyond comments), but I went ahead and overwrote the old one with the new.
sudo cp macports.conf.default macports.conf
Note: There are two other MacPorts configuration files, “variants.conf” and “sources.conf”, which the migration guide doesn’t mention. I compared them anyways and the only significant difference I found was in the “sources.conf” where the “rsync” value was different. I overwrote the “sources.conf” with the latest to ensure that everything is updated.
If you didn’t do a totally clean slate, you will want to follow the migration instructions on how to run the “restore_ports.tcl” script.
Some info above derived from: