Use your favorite open source apps on your Mac with MacPorts

MacPorts is an easy way to get open source applications and keep them updated on macOS.
73 readers like this.
Getting started with Perlbrew

freephotocc via Pixabay CC0

"Package manager" is a generic name for software to install, upgrade, and uninstall applications. Commands like dnf or apt on Linux, or pkg_add on BSD, or even pip on Python and luarocks on Lua, make it trivial for users to add new applications to their system. Once you've tried it, you're likely to find it hard to live without, and it's a convenience every operating system ought to include. Not all do, but the open source community tends to ensure the best ideas in computing are propagated across all platforms.

There are several package managers designed just for macOS, and one of the oldest is the MacPorts project.

Darwin and MacPorts

When Apple shifted to Unix at the turn of the century, it essentially built a Unix operating system called Darwin. Shortly thereafter, a group of resourceful hackers promptly began work on a project called OpenDarwin, with the intent of creating an independent branch of Darwin. They hoped that OpenDarwin and Apple developers could work on related codebases, borrowing from each other whenever it was useful. Unfortunately, OpenDarwin didn't gain traction within Apple and it eventually came to an end. However, the OpenDarwin package manager project, MacPorts, is alive and well and continues to provide great open source software for macOS.

MacOS already comes with a healthy set of default terminal commands, some borrowed from GNU, others from BSD, and still others written especially for Darwin. You can use MacPorts to add new commands and even graphical applications.

Install MacPorts

Your macOS version dictates which MacPorts installer package you need. So first, get the version of macOS you're currently running:

$ sw_vers -productVersion
10.xx.y

MacPorts releases for recent macOS versions are available on macports.org/install.php. You can download an installer from the website, or just copy the link and download using the curl command:

$ curl https://distfiles.macports.org/MacPorts/MacPorts-2.6.3-10.14-Mojave.pkg \
--output MacPorts-2.6.3-10.14-Mojave.pkg

Once you download the installer, you can double-click to install it or install it using a terminal:

$ sudo installer -verbose \
-pkg MacPorts*.pkg
-tgt /

Configure MacPorts

Once the package is installed, you must add the relevant paths to your system so that your terminal knows where to find your new MacPorts commands. Add the path to MacPorts, and add its manual pages to your PATH environment variable by adding this to ~/.bashrc:

export PATH=/opt/local/bin:/opt/local/sbin:$PATH
export MANPATH=/opt/local/share/man:$MANPATH

Load your new environment:

$ source ~/.bashrc

Run an update so your MacPorts installation has access to the latest versions of software:

$ sudo port -v selfupdate

Use MacPorts

Some package managers install pre-built software from a server onto your local system. This is called binary installation because it installs code that's been compiled into an executable binary file. Other package managers, MacPorts among them, pull source code from a server, compile it into a binary executable on your computer, and install it into the correct directories. The end result is the same: you have the software you want.

The way they get there is different.

There are advantages to both methods. A binary install is quicker because the only transaction required is copying files from a server onto your computer. This is something Homebrew does with its "bottles," but there are sometimes issues with non-relocatable builds. Installing from source code means it's easy for you to modify how software is built and where it gets installed.

MacPorts provides the port command, and calls it packages ports (inherited terminology from projects like NetBSD's Pkgsrc and FreeBSD's port system.) The typical MacPorts workflow is to search for an application and then install it.

Search for an application

If you know the specific command or application you need to install, search for it to ensure it's in the MacPorts tree:

$ sudo port search parallel

By default, port searches both the names and descriptions of packages. You can search on just the name field by adding the --name option:

$ sudo port search --name parallel

You can make your searches "fuzzy" with common shell wildcards. For instance, to search for parallel only at the start of a name field:

$ sudo port search --name --glob "parallel*"

List all ports

If you don't know what you're searching for and you want to see all the packages (or "ports" in MacPorts and BSD terminology) available, use the list subcommand:

$ sudo port list

The list is long but complete. You can, of course, redirect the output into a text for reference or pipe it to more or less for closer examination:

$ sudo port list > all-ports.txt
$ sudo port list | less

Get information about a package

You can get all the important details about a package with the info subcommand:

$ sudo port info parallel
parallel @20200922 (sysutils)

Description:          Build and execute shell command lines from standard input in parallel
Homepage:             https://www.gnu.org/software/parallel/

Library Dependencies: perl5
Platforms:            darwin
License:              GPL-3+
Maintainers:          Email: example@example.com

This displays important metadata about each application, including a brief description of what it is and the project homepage, in case you need more information. It also lists dependencies, which are other ports that must be on your system for a package to run correctly. Dependencies are resolved automatically by MacPorts, meaning that if you install, for example, the parallel package, MacPorts also installs perl5 if it's not already on your system. Finally, it provides the license and port maintainer.

Install a package

When you're ready to install a package, use the install subcommand:

$ sudo port install parallel

It can take some time to compile the code depending on your CPU, the size of the code base, and the number of packages being installed, so be patient. It'll be worth it.

Once the installation is done, the new application is available immediately:

$ parallel echo ::: "hello" "world"
hello
world

Applications installed by MacPorts are placed into /opt/local.

View what is installed

Once a package has been installed on your system, you can see exactly what it placed on your drive using the contents subcommand:

$ sudo port contents parallel
/opt/local/bin/parallel
[...]

Clean up

Installing a package with MacPorts often leaves build files in your ports tree. These files are useful for debugging a failed install, but normally you don't need to keep them lying around. Purge these files from your system with the port clean command:

$ port clean parallel

Uninstall packages

Uninstall a package with the port uninstall command:

$ port uninstall parallel

Open source package management

The MacPorts project is a remnant of an early movement to build upon the open source work that served as macOS's foundation. While that effort failed, there have been efforts to revive it as a project called PureDarwin. The push to open more of Apple's code is important work, and the byproducts of this effort are beneficial to everyone running macOS. If you're looking for an easy way to get open source applications on your Mac and a reliable way to keep them up to date, install and use MacPorts.

What to read next
Tags
Seth Kenlon
Seth Kenlon is a UNIX geek, free culture advocate, independent multimedia artist, and D&D nerd. He has worked in the film and computing industry, often at the same time.

5 Comments

Thanks for this article. How is MacPorts different or the same as Homebrew? After Matt Broberg's article in June I installed GnuCash and some other applications on a Mac with Homebrew.

Great question, Don.

The end results are the same: you install an application, and the application runs on your computer.

The way they get there is the only difference. The way homebrew is bound to a specific install location can be mildly problematic for some sysadmins, and homebrew uses Ruby (I think?) for its install scripts, while MacPorts uses Makefiles.

As a user, you won't notice a difference. As a packager or sysadmin, you may prefer one over the other.

This is new information for me. Now to see if the results will be compatible with MacOS accessibility features (especially VoiceOver for the blind). I'll make a point to check it out. Thanks for the article.

That will largely depend on how Apple integrates VoiceOver with its terminal applications, and how Apple integrates VoiceOver with XQuartz (or whatever X11 support library they're using these days). If Apple has VoiceOver set to read both the input and output of a terminal application, then any new command you install through MacPorts ought to be pretty usable. And if Apple made it so VoiceOver reads stuff running in an X11 graphics server, then it should be able to describe even GUI apps.

I'd be interested in hearing the results, should you try this out.

It's a real pity that Apple hasn't released its advancements in accessibility as open source technology. It seems that their work toward better accessibility could benefit everyone, and it's a real shame to have it locked behind a price tag.

In reply to by edkenlon

I recently got back into MacPorts after irreparably banging up my Homebrew installation *again* – no I don't remember how, and my puttering is the sort of constant chaos that most folks will not need to worry about on a single user machine. Homebrew's reasonable defaults are fine for most.

But for me? I love the way MacPorts approaches optional package variants. They're summarized in `port info PACKAGE` when present, `port variants PACKAGE` describes them, and `port install PACKAGE +VARIANT` applies them.

Good stuff.

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.