Wednesday, April 04, 2012

Quick-start with Lift, Scala, and mongodb on Mac OS X

Earlier, I wrote about the programming language, Scala. I had planned to follow with a similar, somewhat academic discussion of the Lift Web Framework, which is based on Scala, and mongodb,  a NoSQL database, before going into practical stuff like how to get started with this stack. But I can't help it but get my hands dirty, so here's how to get started. I'll go back and explain why I chose these components later!

First, the development environment. In my opinion, Mac OS X provides the most capable and powerful software development environments for all platforms that I've encountered, unless you're developing in .NET. I started programming at a Unix shell prompt, and call me old-fashioned, but try as I have, I'm just not feeling the love for IDEs. I'll leave that part of the environment setup for someone else to cover.

One missing piece of the OS X platform is a capable package manager. I chose Homebrew ( Installation is simple:
bash$ /usr/bin/ruby -e "$(/usr/bin/curl -fksSL"
Just follow the prompts. Then update so that the following installations are current:
bash$ brew update
Next, you'll need a build manager for your Scala projects. I chose SBT (Simple Build Tool - SBT requires Java 1.6 or later, but I'll assume that you already have a suitable Java environment on your Mac.
bash$ brew install sbt
Maven is another popular choice that might be more familiar to you if you're a Java developer.

Install mongodb (
bash$ brew install mongodb
Install giter8 ( Wait a minute: Scala, Lift, and mongodb. You didn't mention giter8! What is it? It's a tool that lets you generate a local source tree from templates on Github. I'll use this later to generate source tree for a Lift, Scala, and mongodb app from a very nice template.
bash$ brew install giter8
Generate the g8/LIFT/mongodb source tree ( This is the very nice template that I mentioned earlier. Generate the source tree, configure a few things, and you will have a fully functional Lift, Scala, and mongodb application with basic user registration, login, and session management functionality. The lift-mongo template even incorporates Twitter Bootstrap, providing simple but powerful HTML5, CSS3, and Javascript components.
bash$ g8 eltimn/lift-mongo
bash$ cd [your-project-name]
Here, in your project directory, you'll find a file that will walk you through configuring your new project. Here's a summary for convenience. Twitter Bootstrap is included via a git submodule, so after generating your source tree, you'll need to go to the project root directory and do the following:
bash$ git init
bash$ git submodule add modules/bootstrap
bash$ git submodule init
bash$ git add modules/bootstrap
bash$ cd modules/bootstrap
bash$ git co v1.4.0
This will pull in the necessary files. Note that this template is built against Bootstrap 1.4. The current version is Bootstrap 2. I'm hoping that I won't have to upgrade anytime soon!

Next, configure mongodb. I installed mongodb locally for my development environment. If you do the same, you won't have to configure your application for mongodb; the Lift defaults will work just fine.

IMPORTANT: Due to a Mac OS/Java driver bug in which localhost:27017 is translated to <public-machine-name>/<public-ip-address>:27012, you will need to change the default mongodb setting and allow connections from everywhere (or at least from your local machine's public IP address) by commenting out the bind_ip line in /usr/local/Cellar/mongodb/2.0.2-x86_64/mongod.conf. Make sure you understand the security ramifications of doing this.

Then, start mongodb:
bash$ sudo mongod -f /usr/local/Cellar/mongodb/2.0.2-x86_64/mongod.conf
Finally, build and run the template app:
bash$ sbt > ~;container:start; container:reload /
It will take a few minutes to build for the first time, because SBT will download and compile all of the dependencies. Subsequent builds are much faster. The app will start, and any time you change a source file, it will compile and reload automatically. It will be running on http://localhost:8080. Try registering and logging in.

So what did you not have to do in setting up this stack?
  • Install an IDE, plug-ins, and lots of libraries
  • Do a lot of database or other configuration
  • Define or create a database schema
  • Install Scala (Really? I'm not 100% sure, but I think SBT took care of this)
  • Install Lift and Bootstrap (giter8 took care of this)
  • Write any code (yet)
Pretty easy, right? I hope your experience is the same, and I really hope that I didn't forget any steps. Please give this a try and let me know how it goes.

1 comment:

Rob Marano said...

Way cool!