Tips on MacPorts

DONG Yuxuan @ Feb 03, 2020


MacPorts is the package manager I’m using in macOS. Personally I prefer it than Homebrew. But it also has some flaws so I give some tips about them for MacPorts beginners.

Installation and Updating

If you install MacPorts by the .pkg file, you may stuck on “Running Package Scripts”. If it happens, it’s because of that your network is hard to connect the server of MacPorts to update. Nevermind, MacPorts is already installed, just not updated.1 Forcely kill the installer using the kill command. You can run sudo port selfupdate to update it later. However, because it’s a network issue, you will still stuck on sudo port selfupdate. Don’t worry, continue reading.

MacPorts synchronizes local ports trees with the remote server using rsync. Some networks and proxies prevent or break the use of rsync or svn via http. For people on these networks, getting an updated port tree can be difficult. The rsync server regularly creates a tarball and stores it on the web server. This document explains how to configure MacPorts to use this tarball to update your local ports tree.2

Edit $prefix/etc/macports/sources.conf, comment out the rsync entry, and add a new entry as follows:

#rsync://rsync.macports.org/macports/release/tarballs/ports.tar [default]
https://distfiles.macports.org/ports.tar.gz [default]

The ports trees can be synchronized now but not MacPorts itself. Edit $prefix/etc/macports/macports.conf and find the line #rsync_server rsync.macports.org. Go to https://trac.macports.org/wiki/Mirrors#MacPortsSource and pick the server near you, for example I’m in China so I pick pek.cn.rsync.macports.org, and add to the file:

#rsync_server        	rsync.macports.org
rsync_server        	pek.cn.rsync.macports.org

Now you can run sudo port selfupdate.

Vim

Install Vim by sudo port install vim +huge. The +huge variant provides many important features like supporting the system clipboard.

Python Packages

After you have installed Python and pip with MacPorts. You may want to install Python packages that have executable files, Eg. virtualenv.

You can successfully install it. But when you type virtualenv from the shell, it can’t be found, because it’s not placed into $PATH.

That’s because MacPorts provides some Python packages by itself. For example, you can install virtualenv for Python 3.6 by running sudo port install py36-virtualenv. If MacPorts links executables from pip, it may conflict with executables from MacPorts.

Personally, I don’t think it’s a good decision. Firstly, I think Python packages should be provided only by pip, not by a system package manager like MacPorts. Secondly, if it really wants to do this, it should also do this on Ruby packages, Node.js packages, etc. But it doesn’t. If you install Ruby packages by gem, executables will be linked.

My solution is simple. Supposing MacPorts is installed in the default location /opt/local and we use Python 3.6, we link /opt/local/Library/Frameworks/Python.framework/Versions/3.6/bin/ to /usr/local/py3-bin and put it into my $PATH. If I want to switch the version of Python, I could just relink it.

Ruby Packages

After you install Ruby with MacPorts. You try to install Ruby packages with gem. For example, you run sudo gem install jekyll. But the building may fail for it can’t find some basic header files like stdio.h.

You should carefully check the output, it looks like the following:

sysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk doesn't exist

Run cd /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/ and check files. If MacOSX10.14.sdk or what the file you miss in your system really does not exist, you could make a symlink to MacOSX.sdk by yourself.

# under /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/

$ ln -s MacOSX.sdk MacOSX10.14.sdk

References

  1. https://trac.macports.org/ticket/52434 

  2. https://trac.macports.org/wiki/Mirrors#MacPortsSource