Install MacPorts

MacPots lets you install additional Linux packages on your Mac. Tired of Adobe Media Encoder? MacPorts makes it easy to install ffmpeg, which is FOSS and much more versatile! Want to connect anonymously to your Nextcloud? MacPorts can install Tor as a service, to sync files over the Tor Network...

Install MacPorts


The MacPorts Project is an open-source community initiative that enables you to compile and install open source software that runs natively on Linux (UNIX). We use MacPorts to install software and command line tools that are FOSS and otherwise not available on MacOS, but very useful to break the lock-in-effects of expensive software ecosystems (i.e. Adobe Creative Cloud, Microsoft 365) and to improve our digital security and our online privacy.

THE REASON WHY WE WROTE A SCRIPT TO COMPILE MACPORTS 

Yes, you can of course install the required prerequisites, download the MacPorts installer, run it - and MacPorts should just work on your computer. If you just want to install MacPorts you can follow the official guide by The MacPorts Project on their website. Every bit of necessary knowledge is there.

However, sometimes when a new version of MacOS has been released, there is not yet an installer available on the MacPorts website for the new version of MacOS. In this case you have to compile MacPorts from its source code, otherwise the software will not work.

This happened to one of our friends, who wanted to use MacPorts to install ffmpeg as a free and open-source alternative to Adobe Media Encoder, just after he upgraded to MacOS Ventura. Our friend never used the command line before and he found it too difficult to follow the official Git Install (instructions that can be found on the MacPorts website). So we wrote an installation script that can guide others through the installation process.

If you want to learn how to do it the manual way, follow the section below. Otherwise you can jump right to the next chapter and use our installation script.

COMPILE MACPORTS

the manual way

PREREQUISITES

Xcode is Apple's integrated development environment for software engineers to develop software for MacOS, iOS, iPadOS, WatchOS and tvOS. MacPorts makes use of many tools that are bundled with Xcode, which is why MacPorts requires Xcode and Xcode Command Line Tools to be installed on your system.

Please go ahead and install Xcode from the Apple AppStore before you continue with this tutorial. Alternatively or if you run an older version of MacOS, you can download the required version of both, Xcode and Xcode Command Line Tools from the Apple Developer Website.

This will take a while. Xcode is a large software package that requires about 23GB of disk space. So please take a break, make yourself a coffee and do something else while your Mac is installing Xcode!


Follow this step-by-step guide if you want to compile MacPorts manually. If you prefer an automated fast and easy way to set it up, use our scripted solution instead: Compile MacPorts (interactive script)


Then open our Terminal.app (found with Spotlight: press and [SPACE], then type Terminal, or find it in your Applications -> Utilities Folder) and execute the following command to install Xcode Command Line Tools:

sudo xcode-select --install

Set the path for the active developer directory:

sudo xcode-select -s /Applications/Xcode.app/Contents/Developer

To agree to the Xcode license (EULA), open Xcode once or execute the following two commands to agree to the license:

sudo xcodebuild -license accept

Run the Xcode FirstLaunch command:

sudo xcodebuild -runFirstLaunch

COMPILE MACPORTS

MacPorts is FOSS. The code is published and updated regularly on Github. We want to use this git repository to compile MacPorts from its source. For the following steps you need to have superuser privileges:

sudo su

I) Check out MacPorts Source

First, you need to create a MacPorts' installation directory:

mkdir -p /opt/mports

Change into this directory:

cd /opt/mports

Clone the current MacPorts git repository. This will create the following folder /opt/mports/macports-base, filled with everything that is needed to compile the software:

git clone https://github.com/macports/macports-base.git

Change into this directory:

cd /opt/mports/macports-base

Execute the following three commands to check out the latest stable MacPorts release:

# Find the latest release tagcurl --silent "https://api.github.com/repos/macports/macports-base/releases/latest" | grep '"tag_name":' |  cut -d : -f 2,3 | sed 's/[ ",]//g' > .release
# Check out latest release taggit checkout $(cat .release)
# Cleanuprm .release

II) Build and Install MacPorts

MacPorts uses autoconf and makefiles for installation. MacPorts will be installed to /opt/local.

The following command will check weather you have everything needed for the installation and inform you if there are critical errors:

./configure --enable-readline

This command will tell the compiler to compile the code, install the required external libraries and create the MacPorts executables:

make

Next, this command moves all needed files of the installation to the appropriate system directories:

sudo make install

Cleanup:

make distclean

Now you have compiled and installed MacPorts from source. Exit your superuser shell:

exit

III) Add MacPorts Executables to Paths

Your system needs to know where the MacPorts executables are stored, so you can execute them easily via the command line. Create your ZSH shell configuration file (if it does not yet exist already):

touch -a ~/.zshrc

Insert the MacPorts path variable into this file:

echo 'export PATH="/opt/local/bin:/opt/local/sbin:$PATH"' >> ~/.zshrc

Reload your ZSH shell configuration file:

source ~/.zshrc

IV) Use MacPorts

MacPorts is a command-line utility. Check out the official MacPorts Manual for all available commands. Before you attempt to install any software with MacPorts, you have to update the MacPorts port tree:

sudo port -v selfupdate

Now you can install additional software via MacPorts. For example this command will install ffmpeg:

sudo port install ffmpeg

This command will update all installed ports (libraries, software, applications, etc.):

sudo port -puN upgrade outdated

The following commands will uninstall inactive ports and unclutter existing installations:

sudo port uninstall inactive
sudo port uninstall leaves

We do recommend you update and unclutter your installations on a regular basis!

In the following section we show you, how you can setup a LaunchDaemon that will update MacPorts installations and run basic maintenance tasks on a regular basis.


EXTRA: SETUP AUTOMATIC UPDATER

In this step we create a LaunchDemon that runs a script with the required MacPorts commands 5min after every reboot. Error messages will be written to temporary log files. This way you can check for potential issues and error messages. This file will be overwritten with each reboot.

We use the Shared Folder on our machine to run our own scripts. In our setup the location for the MacPorts Updater Script is: /Users/Shared/Enhancements/macports_updater. If you want to use another location for the script, you will have to adjust the path variable in the LaunchDemon accordingly.

mkdir /Users/Shared/Enhancements
mkdir /Users/Shared/Enhancements/macports_updater

Configure folder ownership:

sudo chown $(stat -f '%Su' /dev/console):wheel /Users/Shared/Enhancements

Navigate to the MacPorts Updater Folder:

cd /Users/Shared/Enhancements/macports_updater

Create empty log files:

touch macports.err.log && touch macports.out.log

Then open a command-line editor, i.e. nano, to create the Shell Script that contains the MacPorts update commands:

sudo nano macports_updater.sh

Copy and paste the following lines:

#!/bin/bash

date +'%d/%m/%Y %H:%M:%S' > /Users/Shared/Enhancements/macports_updater/macports.out.log
echo ' ' >> /Users/Shared/Enhancements/macports_updater/macports.out.log
date +'%d/%m/%Y %H:%M:%S' > /Users/Shared/Enhancements/macports_updater/macports.err.log
echo ' ' >> /Users/Shared/Enhancements/macports_updater/macports.err.log
bash -c '/opt/local/bin/port selfupdate ; /opt/local/bin/port -puN upgrade outdated ; /opt/local/bin/port uninstall inactive ; /opt/local/bin/port uninstall leaves'

Press [control] + X and then Y + [ENTER] to save the file and close the editor.


Setup ownership and permissions:

# Ownership shell scriptsudo chown root:wheel macports_updater.sh
# Ownership log filessudo chown $(stat -f '%Su' /dev/console):admin macports.err.log macports.out.log
# Permissions shell scriptsudo chmod 744 macports_updater.sh
# Permissions log filessudo chmod 644 macports.err.log macports.out.log

Next you want to setup the LaunchDaemon that runs the updater. Navigate to the default location where LaunchDaemons are stored:

cd /Library/LaunchDaemons

Open your command-line editor again, i.e. nano, to create the LaunchDaemon:

sudo nano info.term7.macports.updater

Copy and paste the following lines:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>info.term7.macports.updater</string>
    <key>LaunchOnlyOnce</key>
    <true/>
    <key>ProgramArguments</key>
    <array>
        <string>/Users/Shared/Enhancements/macports_updater/macports_updater.sh</string>
    </array>
    <key>StandardOutPath</key>
    <string>/Users/Shared/Enhancements/macports_updater/macports.out.log</string>
    <key>StandardErrorPath</key>
    <string>/Users/Shared/Enhancements/macports_updater/macports.err.log</string>
    <key>StartInterval</key>
    <integer>300</integer>
</dict>
</plist>

Press [control] + X and then Y + [ENTER] to save the file and close the editor.

Setup the required ownership and permissions:

# Ownershipsudo chown root:wheel info.term7.macports.updater.plist
# Permissionssudo chmod 644 info.term7.macports.updater.plist

Finally load your LaunchDemon manually with the following command to start the update process, or reboot your computer:

sudo launchctl load info.term7.macports.updater.plist

Congratulations: From now on your automated MacPorts Updater is now up and running! After five minutes you should be able to observe how your empty MacPorts log files are filled with content.

UNINSTALL MACPORTS

the manual way

Maybe you want to start from scratch, or maybe you just want to get rid of MacPorts for good and delete all related files and traces. To clean your system, run the following commands:

1) Delete all installed Ports:

sudo port -fp uninstall installed

2) Delete the MacPorts User account and the MacPorts Group:

sudo dscl . -delete /Users/macports
sudo dscl . -delete /Groups/macports

3) Take ownership of former MacPorts folders and files:

sudo chown -R root:admin /opt/local

4) Delete all files and folders that are related to MacPorts:

sudo rm -rf \
    /opt/local \
    /opt/mports \
    /Applications/DarwinPorts \
    /Applications/MacPorts \
    /Users/Shared/Enhancements/macports_updater \
    /Library/LaunchDaemons/org.macports.* \
    /Library/LaunchDaemons/info.term7.macports.updater.* \
    /Library/Receipts/DarwinPorts*.pkg \
    /Library/Receipts/MacPorts*.pkg \
    /Library/StartupItems/DarwinPortsStartup \
    /Library/Tcl/darwinports1.0 \
    /Library/Tcl/macports1.0 \
    ~/.macports

MacPorts now has been purged from your system. Unless you want to install MacPorts again, you may want to delete Xcode as well.

COMPILE MACPORTS

interactive script

Our installation script for MacPorts is an interactive script that will help you to compile MacPorts from source. Furthermore it can set up a System Service that updates MacPorts on a regular basis. Essentially this script does everything for you that we outlined in the previous section of this tutorial. We also provide an automatic uninstall script that will purge MacPorts and all relates files and folders from your system.


BE CAREFUL: YOU SHOULD ALWAYS LOOK THROUGH THE CONTENT OF ANY SHELL SCRIPT YOU DOWNLOAD FROM AN UNKNOWN SOURCE BEFORE YOU EXECUTE IT! MAKE SURE IT IS SAFE TO EXECUTE!

WE HOST OUR SCRIPTS FOR EVERYONE TO SEE ON OUR GITHUB PAGE.


To run our script, you first have to download it. Open the Terminal.app (found with Spotlight: press and [SPACE], then type Terminal, or find it in your Applications -> Utilities Folder). In your Terminal, use this command to navigate to your Downloads Folder:

cd ~/Downloads

Download the interactive script:

# Interactive Scriptcurl -O https://raw.githubusercontent.com/term7/MacOS-Privacy-and-Security-Enhancements/main/03_MacPorts/install_MacPorts.sh

If you ever want to uninstall MacPorts, our MacPorts Updater and all related files and folders, download our uninstall script too:

# Uninstall Scriptcurl -O https://raw.githubusercontent.com/term7/MacOS-Privacy-and-Security-Enhancements/main/03_MacPorts/UNINSTALL_MacPorts.sh

Give the respective file execute permissions:

chmod +x *MacPorts.sh

Execute the respective script:

./install_MacPorts.sh
./UNINSTALL_MacPorts.sh

If asked, enter your administrator password and hit [ENTER] and follow the instructions. Your password won't be shown by default.

PREVIEW:


MACPORTS vs HOMEBREW

Another popular package manager for Mac users is Homebrew.

However, even though Homebrew is easier to use than MacPorts, we prefer MacPorts. The reason is simple: Homebrew is more dependent on existing MacOS packages than MacPorts, which is why installations are fast, but tend to break more quickly after MacOS updates. MacPorts on the other hand maintains its own system libraries in its own separate folder structure, which is why it is more independent from MacOS. The downside is that libraries and software packages are not available pre-compiled. Everything has to be compiled on your system, which makes the installation process more complex and a lot slower. We still think the choice for MacPorts is safer, because installations are less likely to break due to MacOS updates and upgrades.


MacOS-Privacy-and-Security-Enhancements/03_MacPorts at main · term7/MacOS-Privacy-and-Security-Enhancements
Executables to enhance MacOS Privacy and Security. Contribute to term7/MacOS-Privacy-and-Security-Enhancements development by creating an account on GitHub.
The MacPorts Project -- Home
The MacPorts Project is an open-source community initiative to design an easy-to-use system for compiling, installing, and upgrading either command-line, X11 or Aqua based open-source software on the Mac OS X operating system.