The other day, I had an idea for a simple website. I wanted to find an easier way to deploy side projects than I’ve used in the past. AWS gives me too fine a control over things I don’t care about, so I decided to try out Heroku. It was much easier than I thought it would be. Here is basically what I had to do to get a prototype on the public internet, admittedly at a subdomain of herokuapp.com. Making a Heroku account is the usual “sign up with an email and a password,” to I’m assuming you’ve done that step.
Download the Heroku CLI
You can download the Heroku CLI here, and just follow the wizard. Then you type heroku login
at the console, login with your Heroku account, and you’re logged in.
Create an App
At the CLI, you just type heroku create
to create a new app. If you’re in a git repository, heroku will create an app on their hosting service, and add a remote called heroku
to the git repo.
Fight with the DB for a Minute
The prototype I built has no use for a database at all. When I first ran git push heroku master
to trigger a deploy, it errored out because rails new
creates a Gemfile that uses sqlite as its database, but Heroku doesn’t support sqlite. I spent a few minutes trying to get postgresql set up correctly on my laptop, then decided it would be faster to rip out all notion of the DB, at least temporarily. (I’m not really planning to store anything, remember.) That was a bit of an adventure, but I found a few StackOverflow questions that were very helpful.
Following this answer helped a lot, but basically what I did what break inheritance from ActiveRecord, and then change require 'rails/all'
to require everything but ActiveRecord.
I’m not going to go into huge detail about how to do this, since I think that (1) if you’re paying attention when you type rails new
you will either end up with the DB you want or no DB if that’s what you want and (2) building new rails apps with no database is kind of an edge case.
Initially Run Assets Stuff
The next hiccup I had was that Heroku kept reporting an error in the asset pipeline with the precompile step. This had a few causes and fixes.
- CoffeeScript still exists for some reason, and I had done a bad job of ripping it out, so I had to edit
app/assets/javascripts/application.js
to stop it from loading in CoffeeScript files that didn’t exist. (I deleted the `//=require` lines) - I had to tell
config/application.rb
not to initialize assets on precompile. - I tried to repro the error locally with
rake assets:precompile
. See below.
When I ran rake assets:precompile
I got a very helpful error message explaining that yarn was needed (this is the first app I’ve created in Rails 5.1). I did brew install yarn
, which worked fine, and then tried to use rake assets:precompile
again.
Now I reached another helpful error that webpacker was not installed. rake webpacker:install
fixed this. Then rake assets:precompile
finished without error.
The previous steps generated a lot of config files around webpack, and created app/javascripts/packs/application.js
. I added these files to git (just trusting that rails new
makes a sensible gitignore file), pushed to heroku again, and it worked!
Overall, the most frustrating part of the process was trying to get postgresql working correctly on my Macbook. I normally run my development server inside a Vagrant VM, but the file mounts are sort of slow, so I wanted to run this app directly on my laptop when developing against it. A lot of the instructions for getting postgres set up correctly assume Linux, and since I don’t need storage any time soon for my silly app, I ended up fighting with postgres for a while and then just not using it.
The app I was actually pushing to heroku is still too much of a bare-bones prototype for me to want to share, but I will continue chronicling its development, and in a week or two, I’ll put up links here.
Till next time, happy learning!
-Will