Deploy Rails 4.x App to Heroku 2020
Deploying applications to Heroku generally means pushing an app to Heroku using Git.
Heroku provides simple service that can be used to host Ruby on Rails applications.
We'll be able to host our app on Heroku on their free-tier, and we need a Heroku account.
Head to https://www.heroku.com/, click "Sign Up" and create an account.
The starter documentation for Heroku is available at: quickstart.
Now we need to download a Toolbelt from https://toolbelt.heroku.com/ and set it up on our computer.
It provides us access to the Heroku Command Line Interface (CLI), which can be used for managing and scaling our applications and add-ons. A key part of the toolbelt is the heroku local command, which can help in running our applications locally.
$ sudo wget -qO- https://toolbelt.heroku.com/install-ubuntu.sh | shIt gives us the following:
- Heroku client - CLI tool for creating and managing Heroku apps.
- Foreman - an easy option for running our apps locally.
- Git - revision control and pushing to Heroku.
The install script will add our repository and key to our apt sources and then have apt install the heroku and foreman packages from it. The heroku command line client will be installed into /usr/local/heroku and /usr/local/heroku/bin will be added to our PATH.
$ which heroku /usr/local/heroku/bin/heroku
Once installed, we'll have access to the heroku command from our command shell. Log in using the email address and password we used when creating your Heroku account:
$ heroku login ... heroku-cli: Installing Toolbelt v4... done For more information on Toolbelt v4: https://github.com/heroku/heroku-cli heroku-cli: Adding dependencies... done heroku-cli: Installing core plugins... done Enter your Heroku credentials. Email: pygoogle@aol.com Password (typing will be hidden): Logged in as pygoogle@aol.com
At this point, we can test using the cloned app repo from Heroku.
But we're going to use our own app we built: Facebook and Twitter Authentication using Omniauth oauth2. The repo is Rails4-PyGoogle.
In this step we will deploy the app to Heroku.
Create an app on Heroku, which prepares Heroku to receive our source code.
k@laptop:~/Rails4-PyGoogle$ git remote -v origin https://github.com/PyGoogle/Rails4-PyGoogle.git (fetch) origin https://github.com/PyGoogle/Rails4-PyGoogle.git (push) k@laptop:~/Rails4-PyGoogle$ heroku create Creating app... done, stack is cedar-14 https://agile-fjord-1135.herokuapp.com/ | https://git.heroku.com/agile-fjord-1135.git k@laptop:~/Rails4-PyGoogle$ git remote -v heroku https://git.heroku.com/agile-fjord-1135.git (fetch) heroku https://git.heroku.com/agile-fjord-1135.git (push) origin https://github.com/PyGoogle/Rails4-PyGoogle.git (fetch) origin https://github.com/PyGoogle/Rails4-PyGoogle.git (push)
Note that the heroku create command added remote repository to our git.
Now, we want to deploy our code:
k@laptop:~/Rails4-PyGoogle$ git push heroku master Counting objects: 277, done. Delta compression using up to 2 threads. Compressing objects: 100% (252/252), done. Writing objects: 100% (277/277), 390.23 KiB | 0 bytes/s, done. Total 277 (delta 14), reused 0 (delta 0) remote: Compressing source files... done. remote: Building source: remote: remote: -----> Ruby app detected remote: -----> Compiling Ruby/Rails remote: -----> Using Ruby version: ruby-2.1.3 remote: -----> Installing dependencies using bundler 1.9.7 remote: Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin -j4 --deployment ... remote: -----> Discovering process types remote: Procfile declares types -> web remote: Default types for buildpack -> console, rake, worker remote: remote: -----> Compressing... done, 32.7MB remote: -----> Launching... remote: Released v5 remote: https://agile-fjord-1135.herokuapp.com/ deployed to Heroku remote: remote: Verifying deploy.... done. To https://git.heroku.com/agile-fjord-1135.git * [new branch] master -> master
The application is now deployed.
Now visit the app at the URL generated by its app name (https://agile-fjord-1135.herokuapp.com/). As a handy shortcut, we can open the website as follows:
k@laptop:~/Rails4-PyGoogle$ heroku open Opening agile-fjord-1135... done k@laptop:~/Rails4-PyGoogle$ Created new window in existing browser session.
Deployed but I got an error:
On local, it's fine in dev env:
So, need to check the log:
k@laptop:~/Rails4-PyGoogle$ heroku logs ... /app/vendor/ruby-2.1.3/lib/ruby/2.1.0/open-uri.rb:36:in `initialize': No such file or directory @ rb_sysopen - /app/config/application_local.yml (Errno::ENOENT) ...
My case is special because the application_local was not pushed because .gitignore told so.
To fix the problem, I remove that line in .gitignore, and added and committed. Then, issued the following commands as we did earlier:
$ git push heroku master $ heroku create
Now it seems to be working fine:
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization