Deploying a Luminus web app to Heroku with Postgres
If you’ve been tinkering with a web app in Clojure using the Luminus template and want to deploy it, Heroku is a great choice. They offer a free tier and so it’s a great way to continue working on your project without any upfront costs. This guide will cover what is necessary to get everything set up correctly so that your app will work and be connected to a database.
Prerequisites
This topic assumes you have some kind of web app generated by the Luminus template, are using Postgres, and would like to deploy the app to Heroku. I had to do a few extra things that are not found in the official documentation. I’m posting them here as a reference and to help anyone else with this goal.
Steps
Here are the first commands to run. I did this from the terminal window on my Mac and made sure that I was in the root directory of my project.
git init
git add .
git commit -m "initial commit"
Next, I had to add a bin/build
file. The purpose of this is to install the node modules that are used in getting the app to work. When I didn’t do it, I’d get errors saying that React
was not installed. This may not be necessary for everyone.
To do this, create a new folder in your root directory called bin
and then put a file in that folder called build
with no file extension. You can do this with the command touch bin/build
. You may have to first run the command mkdir bin
, however I usually just add my folders by simply left clicking in the directory in Finder and selecting “New folder” and then renaming it.
These are the contents of my bin/build
file.
#!/usr/bin/env bash
npm install --legacy-peer-deps
npx shadow-cljs release app
lein uberjar
This will install the dependencies that are needed, and I added —legacy-peer-deps
because there was some issue with certain dependencies having different version requirements.
Next I ran this command in the terminal:
chmod u+x bin/build
Now it’s time to add buildpacks. I believe this may only be necessary if you are including the bin/build
file.
heroku buildpacks:add heroku/nodejs
heroku buildpacks:add heroku/clojure
I like to also run heroku buildpacks
just to make sure that they both show up, and are the only ones that show up.
Now after adding these last few steps, it might be worth it to commit again to make sure all the changes are reflected in git, because that’s where Heroku is looking for our files.
git add .
git commit -m “adding bin/build and buildpacks”
Now it’s time to create the project in Heroku, and add the postgresql database.
heroku create
heroku addons:create heroku-postgresql
git push heroku main
Your git might have a different name than main
. It might be master
or something else. If so, use that. You should be able to see what it is in the output from the git
commands.
Now ideally everything compiled to Heroku okay. At this point, you should be able to see the app in the browser. However, the parts of the app that use postgres won’t work yet.
Here is how to get that part working. Run the following command in your terminal (and replace kewl-app
with the name of your own project):
heroku run java -cp target/uberjar/kewl-app.jar clojure.main -m kewl-app.core migrate
Now you can reload the app in Heroku and everything should work just as it did in your localhost, except now it’s on the world wide web!
Errors
I had been getting many errors when I tried to simply run heroku run lein run migrate
instead of the last step. In trying to fix it, I made a few changes that I’m not sure are necessary or not. I tried adding things to the Procfile
and also played around with different config.edn
files. However, I believe that the luminus template should have okay Procfile
and config.edn
files as-is, as long as you follow the steps above.
If you have any questions or want to see more content like this, you can find more here or comment here.
References
https://pragprog.com/titles/dswdcloj3/web-development-with-clojure-third-edition/
https://luminusweb.com/docs/deployment.html#heroku_deployment
https://github.com/luminus-framework/luminus/issues/231
http://www.miuminati.com/post/3
And many thanks to the helpful clojurians over at clojurians.slack.com