Sven Mesecke's blog

Posted Di 09 Juli 2013

Setting up a Mac for Scientific Computing and Software Development I

Here at EMBL, many people use a Mac for their work. If you want to use your machine for scientific computing and/or software development, a number of tunings will make your scientific life with the Mac much more enjoyable. This post will demonstrate one way, my way, of setting up a macbook pro or air. It will cover Editors, Dev tools, the Terminal, Mac Package Managers, Python, MATLAB and OpenCV.

Editors

All code is text. Developers and everyone else working with code have a strong love for their text editors. Even Editor wars are not unheard of. If you just want to quickly print a file in the terminal, use less. For doing a small change in a text file, use nano. One step up and you reach vi or vim, a fully fledged text editor existing in the terminal with syntax highlighting and a lot of helpful text editing features. It is a good-to-know tool as vim will be available on almost all web servers. However, vim has a rather steep learning curve, making it a tough first choice for the novice (the two commands everyone should know are ESC :q! and ESC ZZ for closing text files - otherwise you will be stuck in an opened text file forever).

The one text editor that has taken developer's hearts and minds by storm is Sublime Text 2, which is a great combination of pretty and features, however, it is unfortunately not open source. A strong contender might rise with the new, potentially amazing and free and open source (FOSS) LightTable).

On most of my typical working days I use Sublime > vim > less in order of intensity; however, choosing an editor is a thing of faith and preferences.

Programming is not really possible without access to language or API documentation. A great tool for offline access to a large number of docs is Dash. Even if you are not travelling on a train and have access to the web, it is still great to have all docs available in one place.

Development Tools

With a text editor chosen and set up, let's install needed compilers, available in the Apple XCode Development tools if you have not installed them during the MAC OS X installation process. They are either available via the Mac App Store if you are using a current Mac OS X version, or via the Apple Development site (which requires a free account). Don't forget to include the Command Line Tools for XCode in your installation.

The terminal

Even when using an integrated development environment (IDE), developers and computational scientists very frequently have to (and want to) use the terminal, as typing commands is often quicker than combining typing and a mouse or track pad and lets you access any and every aspect of your machine. The terminal app is hidden in Applications/Utilities on Macs. Best to drag it to somewhere easily accessible (cause you'll be using it a lot). You can enable color in the Mac OS X Terminal by (i) opening a terminal, (ii) opening the preferences, (iii) going to Settings -> Advanced and (iv) declaring terminal as xterm-256color. We'll make use of the enabled color in a second.

To start Sublime Text 2 or your chosen editor from the Terminal (aka command line interface (CLI), aka bash in the case of the default terminal app), check whether it is accessible

sublime filename

If sublime does not open, you have three options:

Let's use sublime to customize bash (the bourne again shell), the default shell in the Mac OS X Terminal app. bash can be customized using the .bashrc and .bash_profile files (learn more about these two files and their different roles).

sublime ~/.bash_profile

In the opened or new file, copy-paste

export CLICOLOR=1
export LSCOLORS=ExFxCxDxBxegedabagacad
export PS1="\w>>"

This will enable the use of color in the bash when using ls and modify the command prompt so that the current working directory and ">>" are shown. Check this for a comprehensive overview of bash color options and here for an overview of command prompt options. The latter is also available via

man bash

You can also use bash_profile to set up aliasses for certain often used but lengthy commands such as

alias l="ls -Ahl"

which lets you use l to list All files in a human readable long format.

Type

source ~/.bash_profile

once you are done with editing to enable your changes.

Mac Package Managers

Mac OS X has some neat tools and apps that you can download from websites and the App Store. However, if you want to do serious development or scientific computing, there is no way around being able to install unix tools and libraries. Three package managers exist that enable you to easily download and install said tools, fink, macports and Homebrew. I have not used the first two in a couple of years so will only comment on Homebrew, which I can definitely recommend as it has been completely hassle-free so far. You can install it from the Homebrew website using

ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"

then run

brew doctor

to check whether the installation worked. If it does not error or give any warnings, you are already done. That easy! Get the gfortran compiler which we will need for scipy and associated tools:

brew install gfortran

A full coverage of Homebrew and its possibilities is available from the Homebrew website.

This is the end of part i, part ii, where we will be diving into Python and MATLAB, is available here.

Any comments/questions? Send me an email.

Category: Scientific Computing
Tags: mac science development tutorial