= Install open source on your Mac with MacPorts

:Author: Seth Kenlon
:Email: seth@opensource.com
:Revision: 1.0

The term _package manager_ is a generic phrase for software meant 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 a user 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 has a tendency to ensure the best ideas in computing are propogated across all platforms.
There are several package managers designed just for macOS, and one of the oldest is the http://macports.org[MacPorts] project.


== Darwin and MacPorts

After Apple shifted to UNIX at the turn of the century, they essentially built a UNIX operating system called Darwin.
As a result, a group of resourceful hackers promptly began work on a project called OpenDarwin, with the intent of creating an independent branch of Darwin.
The hope was that OpenDarwin developers and Apple developers could work on related code bases, borrowing from each other whenever it was deemed useful to do so.
Unfortunately, OpenDarwin didn't gain traction within Apple and eventually https://web.archive.org/web/20070111155348/opendarwin.org/en/news/shutdown.html[came to an end].
However, the OpenDarwin package manager project, http://macports.org[MacPorts], is alive and well, and continues to provide great open source software for macOS.

MacOS 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 add new commands, as well as graphical applications, using MacPorts.


== Installing MacPorts

First, get the version of macOS you're currently running:

----[source.bash]
$ sw_vers -productVersion
10.xx.y
----

This version dictates what MacPorts installer package you need.
Releases for recent macOS versions are available on https://www.macports.org/install.php[macports.org/install.php].
You can download an installer from the website, or just copy the link and download using the https://opensource.com/article/20/5/curl-cheat-sheet[curl] command:

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

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

----[source.bash]
$ sudo installer -verbose \
-pkg MacPorts-2.6.3-10.14-Mojave.pkg
-tgt /
----


== Configuring MacPorts

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

----[source.bash]
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
export MANPATH=/opt/local/share/man:$MANPATH
----

Load your new environment:

----[source.bash]
$ source ~/.bashrc
----

Run an update so your MacPort install has access to the latest versions of software:

----[source.bash]
$ sudo port -v selfupdate
----


== Using 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 pull source code from a server, compile it into a binary executable on your computer, and then install it into the required 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 https://opensource.com/article/20/6/homebrew-linux[Homebrew] does with its "bottles", but there are sometimes issues with https://discourse.brew.sh/t/why-do-bottles-need-to-be-in-home-linuxbrew-linuxbrew/4346/3[non-relocatable] builds.
Installing from source code means it's easy for you to modify how software is built, and where it gets installed.

The typical workflow is to search for an application, and then install it.


=== List all ports

If you don't know what you're searching for in the first place, and you just want to see all avairable ports, use the `list` subcommand:

----[source,bash]
$ 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.

----[source,bash]
$ sudo port list > all-ports.txt
$ sudo port list | less
----


=== Search for an application

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

----[source.bash]
$ sudo port search parallel
----

By default, `port` searches both the name and description of a package.
You can search just the name field by adding the `--name` option:

----[source.bash]
$ 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:

----[source.bash]
$ sudo port search --name --glob "parallel*"
----


=== Get information about a package

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

----[source.bash]
$ 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: ciserlohn@macports.org, GitHub: ci42
----

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 in order for this one to run correctly.
Dependencies are resolved automatically by MacPorts, meaning that when you install, for example, the `parallel` package, MacPorts also installs `perl5` if it's not already on your system.
Finally, the License and port maintainer is listed.


=== Install a package

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

----[source.bash]
$ 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 install is done, the new application is available immediately:

----[source.bash]
$ 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's been placed on your drive using the `contents` subcommand:

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


=== Cleaning 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 they're not something you need to keep lying around.
Purge these files from your system with the `port clean` command:

----[source.bash]
$ port clean parallel
----


=== Uninstall

Uninstall a package with the `port uninstall` command:

----[source.bash]
$ port uninstall parallel
----


== Open soruce package management

The MacPorts project is a remnant of an early movement to build upon the open source work that served as the foundation for macOS.
While that effort failed, there have been efforts to revive it as a project called http://www.puredarwin.org/[PureDarwin].
The push to open more of Apple's code is important work, and the byproducts of this effort is 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, you should install and use MacPorts.
