Getting Started with Clojure on Heroku

Posted by: James Ward on 08/14/2012

Last week I introduced the Den of Clojure to Heroku. I really enjoyed learning more about Clojure and experiencing super simple Clojure deployment on Heroku. For those who haven’t yet deployed Clojure on Heroku, lets walk through 8 quick steps to get you started:

  1. Install the Heroku Toolbelt and Leiningen
  2. Login to Heroku from the command line:
    heroku login

    If this is your first time logging into Heroku from the command line then you will be led through some steps to associate an SSH key with your Heroku account.

  3. Create a new Leiningen build definition by creating a file named project.clj containing:
    (defproject hello-clojure-noir "0.1.0-SNAPSHOT"
      :main web
      :dependencies [[org.clojure/clojure "1.4.0"]
                     [noir "1.2.1"]])

    As you can see from the dependencies, this simple app uses the Noir web framework.

  4. Create a simple Noir app by creating a file named src/web.clj containing:
    (ns web
      (:use noir.core)
      (:require [noir.server :as server]))
     
    (defpage "/" [] "hello, world")
     
    (server/start (Integer/parseInt (or (System/getenv "PORT") "8080")))

    This very basic web app returns “hello, world” for requests to “/”. It starts the server using either the port defined by an environment variable named “PORT” or a default of 8080.

  5. Test this app locally by running:
    lein run

    Then visit http://localhost:8080 in your browser and verify that you see “hello, world”.

  6. To upload this application to Heroku you will first need to create a Git repository, add the files to it, and commit them:
    git init
    git add project.clj src
    git commit -m init
  7. Now create a new application on Heroku:
    heroku create

    This creates an HTTP and a Git endpoint for your application. The Git endpoint will be added to your Git configuration as a “remote” named “heroku”.

  8. Upload your Git repository to the Git repository for your application on Heroku:
    git push heroku master

    This will kick off the Leiningen build process on Heroku. The build will download the dependencies for the app then compile the app and put everything into a “slug” that will be deployed onto a Dyno. Once the process is complete you can open the HTTP endpoint for your app in your browser:

    heroku open

    You should now see “hello, world” coming from the Cloud!

The source for this example is on GitHub.

To learn more about Clojure on Heroku, check out the Heroku Dev Center. Let me know how it goes!


About James Ward

James Ward

James Ward (www.jamesward.com) works for Typesafe where he teaches developers the Typesafe Stack (Play Framework, Scala, and Akka) . James frequently presents at conferences around the world such as JavaOne, Devoxx, and many other Java get-togethers. Along with Bruce Eckel, James co-authored First Steps in Flex. He has also published numerous screencasts, blogs, and technical articles. Starting with Pascal and Assembly in the 80′s, James found his passion for writing code. Beginning in the 90′s he began doing web development with HTML, Perl/CGI, then Java. After building a Flex and Java based customer service portal in 2004 for Pillar Data Systems he became a Technical Evangelist for Flex at Adobe. In 2011 James became a Principal Developer Evangelist at Salesforce.com where he taught developers how to deploy apps on the cloud with Heroku. James Tweets as @_JamesWard and posts code at github.com/jamesward.

More About James »

Northern Virginia Software Symposium

November 1 - 3, 2013

Reston, VA

Current Topics on the NFJS Tour

  • Core Java, JEE
  • Dynamic Languages: Groovy, JRuby, Scala, Clojure
  • RESTful Web Apps
  • Frameworks: Hibernate, Grails, Spring, JSF, GWT, more
  • Agility
  • Test Driven Design
  • Security
  • Ajax, Flex, RIA
View Event Details »

Why Attend the NFJS Tour?

  • » Cutting-Edge Technologies
  • » Agile Practices
  • » Peer Exchange

Current Topics:

  • Languages on the JVM: Scala, Groovy, Clojure
  • Enterprise Java
  • Core Java, Java 7
  • Agility
  • Testing: Geb, Spock, Easyb
  • REST
  • NoSQL: MongoDB, Cassandra
  • Hadoop
  • Spring 3
  • Automation Tools: Git, Hudson, Sonar
  • HTML5, Ajax, jQuery, Usability
  • Mobile Applications - iPhone and Android
  • More...
Learn More »

NFJS, the Magazine

May Issue Now Available
  • On the road to learning

    by Raju Gandhi
  • Refactoring to Modularity

    by Kirk Knoernschild
  • RESTful Groovy

    by Kenneth Kousen
  • Getting Started with D3.js

    by Brian Sletten
Learn More »