Getting Started with Scala on Heroku

Over the past year I’ve been gradually learning Scala and I think it’s fantastic! So I’m incredibly excited that Scala now runs on Heroku! Of course you can use the standard Java on Heroku / Maven method of running Scala on Heroku. But as of today you can also use sbt (the Scala Build Tool) to run Scala apps on Heroku. If you are new to Heroku, it is a Polyglot Cloud Application Platform. Put very simply:

Heroku = Polyglot + Platform as a Service (PaaS) + Cloud Components

If you want to try out Scala on Heroku, here are a few quick steps to get you started:

  1. Create a Heroku account

  2. Install the Heroku command line client on Linux, Mac, or Windows.

  3. Install git and setup your SSH key

  4. Install sbt 0.11.0

  5. Login to Heroku from the command line: bash heroku auth:login

  6. Create a file named build.sbt containing: ```scala scalaVersion := “2.9.1”

{
  val stage = TaskKey[Unit]("stage", "Prepares the project to be run, in environments that deploy source trees rather than packages.")
  stage in Compile := {}
}
```

This adds the "stage" task which is used for the build on Heroku.

* Create a _project/build.properties_ file containing:
```
sbt.version=0.11.0
```
    
This tells sbt which version of sbt to use.
    
* Create a very simple Scala app in _src/main/scala/Hello.scala_ containing:
```scala
object Hello extends App {
  println("hello, world")
}
```
    
* Test the app locally by running:
```bash
sbt run
```
        
You should see something like:
        
```bash
[info] Set current project to default-0c17d0 (in build file:/home/jamesw/projects/helloscala/)
[info] Running Hello 
hello, world
[success] Total time: 1 s, completed Sep 7, 2011 4:17:01 AM
```
    
* Create a _.gitignore_ file containing:
```
target
project/boot
project/target
```
    
* Create a git repo, add the files to it, and commit them:
```bash
git init
git add .
git commit -m init
```
    
* Create a new app on Heroku using the Cedar stack:
```bash
heroku create -s cedar
```
    
* Upload your app to Heroku using git:
```bash
git push heroku master
```
    
* Run the app on Heroku:
```bash
heroku run "sbt run"
```
        
Voilà! You just ran Scala on the Cloud! </li> </ol> 
        
You can get the full code for this project on github:

<https://github.com/jamesward/helloscala>
        
That was a very simple example to get you started. Visit the [Heroku Dev Center][10] to continue learning about how to use Scala on Heroku. And let me know if you have any questions.