MacRuby & Xcode 4: Build a Self-Contained MacRuby Application

It's exciting times for MacRuby developers. MacRuby 0.10 was recently released, and Xcode 4 is finally out. And while Apple has announced that MacRuby will be included in Lion, it will be a private framework, not a public one. For now, you need to include the MacRuby framework inside your application bundle. This post shows you how.

1. Create a new project in Xcode 4, and select the MacRuby template

Xcode-1

2. Name your application, and select an app store category

Xcode-3

After this, you'll save your project, and you'll get the Xcode 4 main window.

3. Two things you need to change to package your application

While you can build and run your application now, you can't package it for archiving/distribution. You need to make two changes first:

A) Change the Valid Architectures to X84_64 only:

Activity_view_window_for_untit

If you don't make this change, you get this error when archiving: 

ld: warning: ignoring file /Developer/SDKs/MacOSX10.6.sdk/Library/Frameworks//MacRuby.framework/MacRuby, file was built for unsupported file format which is not the architecture being linked (i386)

B) Add a build target to the Deployment scheme:

Xcode-5

Add the default target to build before the Deployment target. When done, It should look like this:

Xcode-6

if you don't make this change, you get this error when archiving:

Path does not exist `/Users/andre/Library/Developer/Xcode/DerivedData/Alpha-duvzaijhkvprmzdhkzkydcudwljf/ArchiveIntermediates/Deployment/BuildProductsPath/Release/Alpha.app'

4. Select Product => Archive to build for distribution

Xcode's organizer will pop up when the build is done (or select Window => Organizer). If you just want the .app file, click Share, then select Application:

Xcode-7

Save the .app file wherever you like. It should be about 28MB. Yes, it's large because it includes the MacRuby framework. Adding additional code to your application won't increase the bundle size much -- it's all overhead from the MacRuby framework. You can expect your application to zip to around 10MB -- a reasonable size for distribution.

You now have a self-contained MacRuby desktop application. What about gems?

One thing we didn't cover here: using Ruby gems in your MacRuby application, and bundling the gems with your application for distribution. I'll cover gems in an upcoming post.

Further reading