Easily Manage Unix Packages on the Mac with MacPorts, formerly DarwinPorts
I compiled the latest version of PHP this week at work. It took longer than I expected and dealing with the myriad of package dependencies required to add support for XML/XSLT, GD, and Sybase/MS SQL Server was a PITA. While I know that *nix server administrators must be able to compile software from source, I wouldn't want to make it a regular practice.
Just like Linux distributions, Unix package managers exist for Mac OS X. There are two package managers available for the Mac: MacPorts and Fink. Although Fink appears to have a much larger user base, MacPorts is more closely tied to Apple's open source community. In this post I'll go over how you can use MacPorts to manage Unix libraries, modules for languages including Perl and Python, and open source applications, including Wordpress, MediaWiki, and Gallery.
The prerequisite installations for MacPorts are Apple's Xcode, formerly known as Developer Tools, and the X11 windows environment. If you have a newer Mac, you should already have an Xcode install disk. If not, you can get Xcode from Apple's Developer Connection web site. Xcode installs several developer tools, including gcc, the GNU C Compiler, and GNU Make. These tools handle building Unix packages from source and MacPorts won't work without them. X11 can be downloaded from Apple's web site. After installing both, download MacPorts, unpack, and run the installer.
In the beginning... was the command line
After installation, open a Terminal window to configure MacPorts. You'll need to add MacPorts directories located in /opt to your shell environment. From a terminal window, run the following:
If you plan on running MacPorts installed packages with X11, review the MacPorts installation instructions.
With MacPorts installed and configured, open a terminal window and we'll get busy installing packages. MacPorts is run from the command line by entering 'port' followed by a command and options. Below are the most common MacPort command; note that installs and updates are run with sudo.
# View the thousands available packages
# Search for a package
port search package_name
# Get information about a package
port info package_name
# Install a package
sudo port install package_name
# Upgrade a package
sudo port upgrade package_name
# List installed packages
# Update MacPorts to the latest version
sudo port selfupdate
Installation is simple and the great thing about MacPorts is that if a package is dependent on other packages, MacPorts installs the dependent packages, too; no questions asked! The same is true when upgrading MacPorts managed packages. How simple is that?!
Here's sample output from an install of MySQL 5.
% sudo port install mysql5
---> Fetching mysql5
---> Attempting to fetch mysql-5.0.45.tar.gz from http://mysql.mirrors.pair.com/Downloads/MySQL-5.0/
---> Verifying checksum(s) for mysql5
---> Extracting mysql5
---> Applying patches to mysql5
---> Configuring mysql5
---> Building mysql5 with target all
---> Staging mysql5 into destroot
---> Installing mysql5 5.0.45_3+darwin_8
---> Activating mysql5 5.0.45_3+darwin_8
---> Cleaning mysql5
This example's output is pretty short. Most packages display hundreds of lines of output.
Variants, Manage Package Options
Unix packages often have options which can be added or removed during installation. MacPorts refers to these options as variants. PHP, for example, can be configured to work with specific web servers and different databases.
To view a package's variants:
port variants package_name
To specify variants, use the plus sign:
% sudo port install php5 +apache2 +mysql5 +sqlite +postgres
Tired of the Command Line? Try Porticus, a MacPorts GUI
Fink users have had the Fink Commander GUI to manage their installs for several years. Finally, a few GUIs have emerged for MacPorts. Porticus is the most recent GUI released and is in beta at the writing of this post.
Porticus is written in Cocoa, so it's pretty fast. Although the beta release has a few rough edges, it's worked really well for me and its simple interface made it easy to quickly update my LAMP development server. In the screenshot above, blue items are installed and up-to-date, and items with updates available display in red. Select an item to view package details, including version, project URL, maintainer, dependencies, and description.
Porticus also supports Growl notifications so you can start up a long install, work on something else, and see exactly when install is complete.
I've read good things about Port Authority, another MacPorts GUI, but I wasn't willing to pay $20 for a license. Porticus, on the other hand, is currently a free download.
Give MacPorts and Porticus a try. You won't need to hunt down package installers or pre-compiled binaries for Apache, PHP, and their dependencies again.