By Steven Stevenson May 10, 2014

How to set up Rails on Heroku and Dokku

Steven Stevenson, a graduate of our iOS Development 8-week program, shared some tips on setting up Rails on Heroku and Dokku.

Setup can be a real B%@#%@! Expletives aside, I was learning how to implement Rails, and it’s actually a really fun framework to work in. After doing a fair amount of iOS coding, I see how easy it can be to transition between Rails and iOS.

It helps that both have an amazing network of support and examples. Cocoapods is an amazing source for just getting pure inspiration of what others have done, if not for the features of your app. Rails has Bootstrap, which has been an amazing tool as I have made a few designs. Not to mention Sass (.scss files), .haml, and flat out Ruby files making my life so much more fun.

For those who are new to programming, it gets easier after each language you learn. Syntax seems to be an issue only after the first few glances at a language. As soon as you start writing code in that language, things just flow. Documentation is another nightmare, and that’s honestly what friends are for, if you ever get stuck. The MVC ideals still follow in Rails (for those who don’t know), and after Objective-C’s straight forward approach, Rails was fairly simple to comprehend.

Heroku

Heroku has a great free system. You can easily set up and run your Rails service in less than 15 minutes. When you set up an account, you’ll get an IP address to access through SSH (terminal) to configure and push your deployment Rails.

The best practice, as I have learned, is to setup a Git (the service, not necessarily GitHub) for your project:

git init  
git touch .gitignore //look for Rails or other Git ignores online  
git add -A .  
git remote add REMOTE_NAME LINK_TO_DEPLOYMENT_SITE  

That’s the basic Git process to get it started. When you do have the SSH key for Heroku, you’ll just use git push REMOTE_NAME master to set up the Rails site! So much easier than SFTP and whatever to reset your system and database. Just know that if you do any database configurations on your own computer, you have to do them again on the Heroku server. Try this out, and see where you get! Heroku is a great free service, especially if you’re just testing things out.

When you are in Heroku, remember that to run Rails you cannot just type rails db:migrate, you actually have to type heroku run rails db:migrate. It’s a silly addition, but it ensures the virtual machine unit behavior with this process.

Dokku (Digital Ocean)

If you want to deploy your server on a more dedicated service box, say digitalocean.com, it’s also super doable with the Dokku frame running on Linux. Go to digitalocean.com and create a droplet. Make sure you add the application Dokku to your droplet.

The procedure is about the same with Git, but a little different when you actually get to building your Rails (database) and deploying it with Git. For the database, it’s best to use PostgreSQL (you’ll likely have to download it via brew or other providers). For Rails, in your databasel.yml, change the adapter to PostgreSQL. Don’t use SQLite, because Dokku doesn’t support that database.

Running on your own machine, you have to start your database as well as Rails server:

//startup Postgres
postgres -D /usr/local/var/postgres

//startup Rails
rails s

This is important to note, because you’ll have to also link your Postgres database when you’re on your Dokku (you didn’t necessarily need to do this with your Heroku, but it held me up for a while when installing on Dokku).

Now that you have your droplet and a Rails site, Git push to the Dokku remote (the SSH is similar to how you did with Heroku). This is also assuming your Dokku is set up with Rails, but if not, install everything on your VM that you did for your local machine. Also note that the remote needs to contain the app deployment name. This is the same as the app name and goes at the end of your SSH URL as 123.45.67.89:SITE_NAME.

Next, SSH into your VM. You’ll first need to run the Postgres database linkage to your app:

dokku postgresql:link APP_NAME DB_NAME  

This creates the database link. You can do the db:migrate, create from here:

dokku run APP_NAME rake db:seed  

And that’s it! You now know how to set up Rails on Heroku and Dokku! With all these fun names, I people to start saying Gitbo for Github Repos… But I digress…

Cheers!

Check out the original post and more resources on Steven’s blog, iOS-Coder.com.