OS X as a UNIX workstation for Astronomy

(and hopefully other sciences)

Setup
Differences with Traditional UNIX
Misc. Info
Python

This page is still incomplete. I will have a more complete version soon.

Apple's OS X works for most things out of the box. Unfortunately, as it is set up by default, the UNIX parts of the OS are pretty well hidden from the user. This may be fine for desktop users and graphic art/movie people, but it doesn't help people who want the computer to do science, which pretty much requires something like UNIX.

There are lots of pages describing how to set a Mac up for UNIX work, but since I found I needed to use several of them, I am compiling everything I learned (or figured out myself) in one place.

First a little disclaimer: I have a 12 inch G4 powerbook with OS X 10.5 and a MacBook Pro with OS X 10.5. So I only personally test things on these machine. There is no reason why things here shouldn't work on other Macs with OS X, but we all know computers aren't always logical. If something I suggest doesn't work on your machine, please let me know (my email is at the bottom of the page) and I will amend my suggestions accordingly.

Update: I have retired the powerbook and now have a Mac Mini running OS X 10.6 (along with a MacBook Pro running 10.6 at work). Overall, I found very few changes or problems with 10.6 (and most of the changes were in the non-UNIX parts like iCal). So pretty much everything here has now been tested on OS X 10.6 (and some was tested on 10.5 and 10.4)

Generally, I am a GNU/Linux user, and have been for several years. My desktop at work has Scientific Linux on it and my home desktop has Ubuntu (although I went through Mandrake, Fedora Core, and Gentoo before that). Neither are dual boot. I was not especially happy about getting a computer with a non-Free operating system on it. But, OS X is based on a BSD UNIX (which are free), and I primarily use the same software I use in Linux, so at least I'm not getting stuck with proprietary formats. Apple also produces some really nice hardware (and in the case of the 12 inch powerbook, small and cheap hardware). It is nice using a UNIX that is completely supported on the hardware by the manufacturer. And as a bit of a UNIX geek, it is hard to turn down the chance to play with a new (and in a lot of ways different) UNIX that is modern and updated. There are also a lot of people in astronomy using OS X, so it is nice being able to make sure that codes I use work on commonly used platforms.

Now, in case anyone got through all that, on to hopefully useful stuff.


Setup

  1. Put terminal in Dock.
  2. By default, when you exit a terminal, it it says [Process Complete] and the window does not close, which is not the proper behavior for an xterminal. So, with a terminal open, go to Terminal->Window Settings, and choose "Close only if shell exits cleanly". You then have to press "Use Settings as Default" (unlike most Apple settings which happen immediately).
  3. Update system (from gui or in terminal: sudo softwareupdate -ia).
  4. 10.5 or below only. Install X11 from install CD.
  5. 10.5 or below only. Add X11.app to startup programs.
  6. Install XCode from install CD or from apple developer site.
  7. Install Fink or MacPorts. I prefer MacPorts myself so I'll give some additional advice on that later.
    1. Add the following to your .bashrc:
      export PATH="/opt/local/bin:/opt/local/sbin:$PATH"
      export MANPATH="/opt/local/share/man:$MANPATH"
    2. Make sure macports is updated (sudo port selfupdate) and all ports are updated (sudo upgrade -R -u outdated).
    3. Install whatever UNIX programs you use (sudo port install NAME).
    4. If using fink, install Fink Commander if you want to use a GUI to interact when you use fink.
  8. Go to the apple menu->System Preferences->Security and select "Use Secure Virtual Memory".
  9. Let ssh forwarding through the firewall: Go to the apple menu->System Preferences->Sharing. Then in the firewall, enable Remote Logging (Apple's attempt to give a non-technical name to ssh).
  10. Now that ssh forwarding is available, you need to enable X forwarding. As root edit (sudo emacs, sudo vi, or whatever you prefer) /etc/sshd_config and make sure the line X11Forwarding says "yes".
  11. Even though normal PC keyboards have the caps lock key in the same stupid place as the powerbook keyboard (unlike the nice UNIX keyboards from Sun), I find that on the small powerbook keyboard, it is more of a problem for me. Apple does provide an easy way to change this. Go to Apple Menu -> System Preferences -> Keyboard & Mouse -> Keyboard and then click on Modifier Keys. From there, you can switch caps lock and ctrl to match the normal UNIX behavior (or even better, leave both keys as ctrl since no one actually uses caps lock).
  12. This isn't really a UNIX thing, but it annoys me anyway. I like the arrows for the scroll bars to be on the top and bottom, and not together. Go to Apple Menu->Appearance and you can choose where the scroll bars go.
  13. 10.4 and below only. Go to SSH Keychain. Download and install that. This allows you to handle ssh-keys and forwarding from both X and OS X applications and uses Apple's keychain. Very cool. You need to know how to use ssh keys, but there are lots of other places with help on this.
  14. I still don't like or use Spotlight, but I've decided this step is a VERY BAD idea. Turn Spotlight off. I have nothing against spotlight (although the locate command does just fine for me). But the 12 inch powerbook is short on processor power and screen space, so even the modest use of them from Spotlight is not worth it for me. Apple provides no simple checkbox or anything to turn it off, but you can still do it.
    As root (sudo emacs or sudo vi) edit /etc/hostconfig. Change the line:
    SPOTLIGHT=-YES-
    to
    SPOTLIGHT=-No-
    Then either remove (or if you want to be safe, move) /System/Library/CoreServices/Search.bundle
    You will probably need to repeat this step after major system updates (things will behave really screwy if you don't).
  15. This next problem was solved for me by Nicholas Chapman. On a laptop, there is a single key labeled "Delete" which does a backspace. On a full keyboard (for me that usually means when I ssh into my laptop from a desktop as odd as that is), there is also the key next to "end" labeled "del". This should be the forward delete key, but in most terminals it produces a ~ character. In tcsh (or csh) Nicholas figured out that you can type (or put in your startup file)
    bindkey "\e[3~" delete-char
    Now, for bash users (which is the default shell for pretty much everything these days (not sure about recent Solaris versions)), you can put the following in your .bashrc file:
    bind '"\e[3~" delete-char'
    And now your terminals work a little closer to normal.
  16. Install Firefox. Some people are okay with Safari, but I like to use the same browser on all my systems (and don't really like Safari).
  17. Terminal.app has some strange ideas about what the PAGE-UP and PAGE-DOWN keys do. To fix this, open Preferences for Terminal.app. Go to the keyboard tab for the profile you use. Find the "page up" key and set it to the string \033[5~ and make sure the action is set to "send string to shell". For "page down", set it to \033[6~. You can probably fix HOME and END in similar ways but I don't find that those bother me as much.
  18. Install Aquamacs. This is a very nice version of Emacs for the Mac GUI. Recent versions of GNU Emacs can build with an Aqua GUI too so this will probably be outdated soon, but for now it is kind of nice. They do try to make things a little too Mac-like for my taste though.
    1. In your .bashrc, add the line:
      alias aquamacs="open -a /Applications/Aquamacs\ Emacs.app/"
    2. In your .emacs file (Aquamacs says you should put this elsewhere but that is kind of dumb since there is no reason to have emacs setting split in different places);
      (when (boundp `aquamacs-version)
      (setq confirm-kill-emacs 'yes-or-no-p)
      (lisp-interaction-mode)
      )
  19. sudo chmod 1777 /var/mail to make programs like pine and mailx happy.

Not everything is configured via text files in /etc. There are several options, mostly for X, that you set from the command line.
To make focus follow the mouse for X (in the proper X-Windows fashion), type the following in a terminal:
defaults write com.apple.x11 wm_ffm true
defaults write com.apple.x11 wm_click_through -bool true

To make focus follow the mouse in the Apple Terminal App (not xterm):
defaults write com.apple.Terminal FocusFollowsMouse -string YES
I needed to do this for OS X 10.3. 10.4 has the same option, but it may have been set by default. I don't specifically remember doing it, but my terminals behave correctly on my laptop.

In ~/.profile:
#Need to set term properly to log on to solaris machines
export TERM="xterm"
source ~/.bashrc

10.5 or earlier only. This works out of the box on 10.6. In ~/.bashrc:
if [[ -z $DISPLAY ]]; then
disp_no=($( ps -ax | grep X11.app | grep -v grep | awk '{print $7}' ))
if [[ -n $disp_no ]]; then
export DISPLAY=${disp_no}.0
else
export DISPLAY=:0.0
fi
fi


Differences with more traditional UNIX systems

There are a couple of places where OS X is not like the normal UNIX systems we are used to. These are not terrible, but they can cause lots of problems if you don't know about them and find them the hard way. I discovered most of these while porting ZEUS-MP, the MHD code I work on, to OS X.

1. The default filesystem (HFS+) is not case sensitive. It does preserve the case of filenames, so your names don't get mangled, but you cannot have filename and FILENAME in the same directory because they are the same file to OS X (with the default filesystem).

A common practice in FORTRAN programming is to have source files end with .F and contain C preprocessor directives. After preprocessing, the new files end in .f and are then processed by the FORTRAN compiler. This system fails (very badly) as the original files are overwritten by the pre-processed .f files. For zeusmp, I modified the Makefile so the pre-processed files end in .for (also common in FORTRAN circles) while the original source files still end in .F.

2. There is no -static option for gcc. If you see an error where the linker is not able to find crt0.o, it is probably because you were trying to compile statically. There is some horrible workaround hidden somewhere on Apple's website (which involves replacing some system files with ones from OpenDarwin), but just avoiding the -static option seems to be a better solution.

3. There is no /home directory. Home directories reside in /Users. This doesn't really matter, but if you have any scripts that rely on /home, they will fail. The best thing to do is to use ~username in your scripts instead of /home/username so things stay compatible with other systems.

4. This last problem only comes up if you use a USB flash drive, removable hard drive, NFS share, or any other disk that is accessed by non-OS X machines. Anytime you mount (or maybe access, I haven't experimented too much) a directory, OS X leaves behind two files in that directory: .DS_Store and .localized. They are too small to matter, but they do tend to annoy Windows users since they show up in Explorer. They aren't as bad for UNIX users since they start with a period and so are hidden. But it is very annoying that Apple has their system put pointless (at least to other systems) files all over the place (even on resources that may very well be shared) with no good way to turn it off.

There is at least a partial solution to this (from an Apple tech article). In a terminal type:

defaults write com.apple.desktopservices DSDontWriteNetworkStores true

This should prevent the creation of .DS_Store files on remote directories.


Miscellaneous Interesting Commands

Here are a couple of interesting commands that are specific to OS X (and possibly darwin, but I don't know).

To access info from the OSX AddressBook.app from the command line:
sqlite3 -separator ' ' ~/Library/Application\ Support/AddressBook/AddressBook-v22.abcddb "select e.ZADDRESSNORMALIZED,p.ZFIRSTNAME,p.ZLASTNAME,p.ZORGANIZATION from ZABCDRECORD as p,ZABCDEMAILADDRESS as e WHERE e.ZOWNER = p.Z_PK;"
I'm not really sure what to do with that yet. I first saw it in the context of getting an address book for mutt but since I use pine for my mail (and keep my address book on an imap server), I would need to play with that a bunch to get it to work that way. But it is still pretty cool in principle.

ioreg -l | grep IOBattery
I'm not sure exactly how to interpret the output of that, but it is an interesting way to get some information about the battery from the command line. I would be interested if someone has a better way or a better way to interpret this.
pbcopy
pbpaste
These two commands are for copying and pasting from the clipboard. I'm still not sure what to do with them, but they seem like a cool idea.

defaults write com.apple.Safari IncludeDebugMenu 1

Enter this command in a terminal. To undo it, enter the same command only replace the '1' with a '0'. This enables an interesting debug menu in Safari. Not sure if it is actually useful though.




Python

Python is a great scripting language. I still love regular expressions and the speed at which I can write script to deal with text in Perl, but for data analysis and plotting, python has become my main language.

Of course, I was thrilled that OS X came with python by default. Unfortunately, it isn't set up in the most useful way. For starters, it lacks GNU readline support compiled in. This is annoying if you want to use python interactively. I really don't understand this choice. Apple certainly is not afraid of the GPL. After all, the whole system is built with gcc. And it comes with emacs!

Also, the default python install lacks any array packages. I tend to use numeric, but don't care too much. If you use macports, this is easy enough to install. Install python (sudo port install python26) along with the array package of your choice. Then install python_select in the same way and run that to make the new version of python your system default (python_select python26).

Now when you use python in scripts and on the command line, you get a nice, up-to-date, and more complete version while the Apple supplied version is still around in case you ever need to test against it.