I love MacPorts.
What a fantastic way to install open source software to run on a Mac. Sometimes I want to be able to run certain Linux/Unix tools on my Macbook Pro, only to find there’s no Mac alternative available. Or maybe I just prefer to do things the hard way via command line. I’m weird like that sometimes.
You need Xcode to use MacPorts, so get it and install it if you don’t already have it: http://developer.apple.com/tools/xcode/. It’s free too!!
Installing it is simple enough as downloading and installing a PKG. From there you can choose to either control your ports via command line (my personal preference) or a GUI. If you want a GUI, I like Porticus, because it will show you the command as you would type it in Terminal minus sudo. So if Porticus says “port selfupdate”, your command in terminal would be “sudo port selfupdate” (no quotes).
Setting up Terminal for MacPorts is easy, and outlined on the MacPorts website. I modified my setup a bit from how the website recommended because I test a lot of different software which requires modifying my paths quite a bit. I prefer to keep all these preferences in one place, so I chose my .bash_profile file in my home directory. I’ve noticed that if you use .profile and you have a .bash_profile, the .bash_profile will take precedence.
If you’ve never heard of .bash_profile or are wondering why the hell I’m typing it in with a period in front of the name, you’re in for a treat. OS X is Unix based, meaning there’s a LOT that you don’t see in the file structure using Finder. For example, open up your Terminal Application in /Applications/Utilities/. From there type ls. You’re in your User directory, so you’ll see a list of your user folders such as Documents, Pictures, Desktop, etc. Cool, huh? Now try ls -l, which basically shows the previous command in a columnar view along with a bunch of other useful information, which I won’t go into here (that’s a whole ‘nother tutorial). Now try the command ls -la. Wow! Where did all of that stuff come from?? You’ll likely see a few files/directories with a period in front of it. What that does is designate a “hidden” file or directory. They’re all there for a reason, so don’t go trying to delete them unless you know exactly what they do.
Now let’s set your preferences for MacPorts so you can use it via command line. What you’ll want to do is create or edit your .bash_profile file, using either vi or nano. Try nano if you’ve never used a command line editor before, as vi is a little tough to get used to. So once you’ve opened Terminal, type nano .bash_profile. A screen will come up:
Yours might not contain any content if it didn’t previously exist. What you want to do is add the last two lines:
Once you’ve done that, hit ctrl+x then y to save your changes. Now you’re back in the regular command line again. Type source .bash_profile to make the new settings take effect.
Once you’ve done all this, let’s test your new settings. Type which port, and you should get a response that says /opt/local/bin/port, assuming you installed MacPorts to the default place. If you get that response, you’re set!
Now you can try a command such as sudo port selfupdate and type in your password when prompted, which will likely produce the following:
MacPorts base version 1.600 installed
Downloaded MacPorts base version 1.600
The MacPorts installation is not outdated and so was not updated
Basically that command checks if there’s a newer version of MacPorts available. If there was, it would install it by itself. There’s all kinds of other commands available, such as sudo port search ___ and sudo port install ___.
You can even try installing something, such as the command line linux downloader wget. Just type sudo port search wget, and it will show you the available packages to be installed. Now type sudo port install wget. What happens next is awesome, because MacPorts fetches each individual source package required to run wget, and compiles it to run on OS X. This is why having Xcode installed is important, because it gives MacPorts what it needs to compile these packages for OS X. Everything is set up in /opt/local/, which pretty much stays out of OS X’s way. You won’t have to worry about interfering with critical stuff, because you won’t.
This is just a basic introduction to MacPorts. Read up on the MacPorts Wiki and get your hands dirty with the Terminal! There’s all kinds of cool command line (as well as GUI based) applications available.