How to deploy application to online server using Heroku
By: Lukasz Muzyka, On:
This tutorial assumes you have already completed:
- Install Ruby on Rails
- Create Ruby on Rails application
- Create Static Pages - without this deploy will not work
- Install Git
- Create Remote Git Repository - optional but recommended
Our simple application is now working. It's now time to make it work on the internet so that other people can access it. Back in the day, this was a difficult process. Today, thanks to companies like Heroku it has become very, very simple. It takes just a couple simple steps to deploy the app to the Heroku platform.
Our application is prepared to work in 3 different configurations: Test, Development and Production. The test environment is a special configuration that runs special scripts. These scripts checks to ensure the application is working in the right order. Development is what we can access when we run $rails console
or $rails server
and open a browser with the address http://localhost:3000. Production is a special configuration for the server. All environments are using different databases and we can make them use different email addresses, etc.
For our production environment, we will be using the heroku.com platform. Not only can we host our application for free there (until we generate reasonable traffic) but also everything is ready to go. After the initial setup, deploying an application will be as easy as typing a simple command in the Terminal. Let's get going:
Step 1: Create an Account on Heroku
Head over to www.heroku.com and create an account there.
After you confirm your account head back to https://devcenter.heroku.com/articles/quickstart to download the Heroku toolbelt.
Step 2: Heroku Login
After you install it, open terminal and type:
bash
heroku login
Enter your credentials:
Enter your Heroku credentials. Email: adam@example.com Password: Could not find an existing public key. Would you like to generate one? [Yn] Generating new SSH public key. Uploading ssh public key /Users/adam/.ssh/id_rsa.pub
Step 3: Configure the Database
First, we need to make sure we use the proper type of database for our production environment. Rails, by default, gave us an SQLite3 database, which is okay for our development environment. Heroku however wants us to use PostgreSQL database. Let's make sure our Gemfile
has the necessary gems.
Replace
Gemfile
# Use sqlite3 as the database for Active Record gem 'sqlite3'
with
group :development, :test do gem 'sqlite3' end group :production do gem 'pg' end
Because the way heroku uses Gemfile.lock file to build our application we will need to delete that file now, and rebuild it with command "bundle install". Otherwise, heroku will reject our push. So go ahead and delete Gemfile.lock
. It's next to the Gemfile.
Step 4: Install dependencies
Now, we can install all the new dependencies. In your terminal, type:
bash
$ bundle install
Step 5: Change Configuration File
One more change we need to make is to change the configuration file for our database. Head over to config/database.yml
:
config/database.yml
# SQLite version 3.x # gem install sqlite3 # # Ensure the SQLite 3 gem is defined in your Gemfile # gem 'sqlite3' # default: &default adapter: sqlite3 pool: 5 timeout: 5000 development: <<: *default database: db/development.sqlite3 # Warning: The database defined as "test" will be erased and # re-generated from your development database when you run "rake". # Do not set this db to the same as development or production. test: <<: *default database: db/test.sqlite3 production: adapter: postgresql host: localhost pool: 5 timeout: 5000 database: demo_production
Note that we have changed the settings for the production database.
Step 6: Changes to Git Repository
We can now commit the changes to the git repository:
$ git add . $ git commit -m "changed database type for production to postgres"
Step 7: Deploy application to Heroku
At this point, we are ready to deploy our application to Heroku. First, we have to create an application:
$ heroku create pick-unique-name-of-your-application
Creating peoplecancode-demo... done, stack is cedar http://peoplecancode-demo.herokuapp.com/ | git@heroku.com:peoplecancode-demo.git
If the creation is successful, you will be able to deploy the application to Heroku:
$ git push heroku master
Fetching repository, done. Counting objects: 9, done. Delta compression using up to 4 threads. Compressing objects: 100% (5/5), done. Writing objects: 100% (5/5), 970 bytes | 0 bytes/s, done. Total 5 (delta 4), reused 0 (delta 0) -----> Ruby app detected -----> Compiling Ruby/Rails -----> Using Ruby version: ruby-2.0.0 -----> Installing dependencies using 1.5.2 Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin -j4 --deployment Using rake (10.3.1) Using json (1.8.1) Using i18n (0.6.9) Using thread_safe (0.3.3) Using builder (3.2.2) Using erubis (2.7.0) Using minitest (5.3.3) Using rack (1.5.2) Using polyglot (0.3.4) Using mime-types (1.25.1) Using arel (5.0.1.20140414130214) Using bcrypt (3.1.7) Using coffee-script-source (1.7.0) Using execjs (2.0.2) Using thor (0.19.1) Using orm_adapter (0.5.0) Using hike (1.2.3) Using multi_json (1.9.3) Using pg (0.17.1) Using bundler (1.5.2) Using tilt (1.4.1) Using sass (3.2.19) Using rdoc (4.1.1) Using tzinfo (1.1.0) Using treetop (1.4.15) Using rack-test (0.6.2) Using warden (1.2.3) Using uglifier (2.5.0) Using sprockets (2.11.0) Using coffee-script (2.2.0) Using sdoc (0.4.0) Using activesupport (4.1.0) Using mail (2.5.4) Using actionview (4.1.0) Using activemodel (4.1.0) Using jbuilder (2.0.6) Using activerecord (4.1.0) Using actionpack (4.1.0) Using actionmailer (4.1.0) Using railties (4.1.0) Using sprockets-rails (2.1.3) Using coffee-rails (4.0.1) Using devise (3.2.4) Using rails (4.1.0) Using jquery-rails (3.1.0) Using sass-rails (4.0.3) Using turbolinks (2.2.2) Your bundle is complete! Gems in the groups development and test were not installed. It was installed into ./vendor/bundle Bundle completed (0.58s) Cleaning up the bundler cache. -----> Preparing app for Rails asset pipeline Running: rake assets:precompile Asset precompilation completed (2.05s) Cleaning assets Running: rake assets:clean -----> WARNINGS: Include 'rails_12factor' gem to enable all platform features See https://devcenter.heroku.com/articles/rails-integration-gems for more information. You have not declared a Ruby version in your Gemfile. To set your Ruby version add this line to your Gemfile: ruby '2.0.0' # See https://devcenter.heroku.com/articles/ruby-versions for more information. No Profile detected, using the default web server (webrick) https://devcenter.heroku.com/articles/ruby-default-web-server -----> Discovering process types Procfile declares types -> (none) Default types for Ruby -> console, rake, web, worker -----> Compressing... done, 21.5MB -----> Launching... done, v9 http://sc-demo-peoplecancode.herokuapp.com/ deployed to Heroku To git@heroku.com:sc-demo-peoplecancode.git e8bbe97..6f47150 master -> master
The second to last line shows you your URL. Open it in your browser. Your app is now on the internet. Please share the link in the comments :-)
Comments
Comment
You can login to comment
On: Frank wrote:
On: Philip Lohrmann wrote:
On: Michael Lajlev wrote:
On: Brunitob wrote:
On: silvina wrote:
Hello: thank you for the wonderful tutorial and my issue is with heroku. I had an account from long ago and this is the error i get silvinas-MacBook-Pro:static formsmama$ git push heroku master fatal: 'git.heroku.com/floating-dragon-42.git' does not appear to be a git repository fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.
I did add the new ssh key to heroku but the error still there.
On: silvina wrote:
Do we have to perform the configuration change every time we create a new project?
On: Brayan López wrote:
https://design1elearning.herokuapp.com/
On: David Cruz e Silva wrote:
I did bundle install and got the following error
The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for ruby but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run
bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java
. Fetching gem metadata from https://rubygems.org/.......... Fetching version metadata from https://rubygems.org/.. Fetching dependency metadata from https://rubygems.org/. Resolving dependencies... Using rake 12.0.0 Using concurrent-ruby 1.0.5 Using i18n 0.8.1 Using minitest 5.10.1 Using threadsafe 0.3.6 Using builder 3.2.3 Using erubis 2.7.0 Using miniportile2 2.1.0 Using rack 2.0.1 Using nio4r 2.0.0 Using websocket-extensions 0.1.2 Using mime-types-data 3.2016.0521 Using arel 7.1.4 Using bundler 1.14.6 Using byebug 9.0.6 Using coffee-script-source 1.12.2 Using execjs 2.7.0 Using methodsource 0.8.2 Using thor 0.19.4 Using debuginspector 0.0.2 Using ffi 1.9.18 Using multi_json 1.12.1 Using rb-fsevent 0.9.8 Installing pg 0.19.0 with native extensions Using puma 3.7.1 Using sass 3.4.23 Using tilt 2.0.6 Using sqlite3 1.3.13 Using turbolinks-source 5.0.0 Using tzinfo 1.2.2 Using nokogiri 1.7.0.1 Using rack-test 0.6.3 Using sprockets 3.7.1 Using websocket-driver 0.6.5 Using mime-types 3.1 Using coffee-script 2.4.1 Using uglifier 3.1.4 Using rb-inotify 0.9.8 Gem::Ext::BuildError: ERROR: Failed to build gem native extension./Users/David/.rvm/rubies/ruby-2.4.0/bin/ruby -r ./siteconf20170306-20385-1sgh9li.rb extconf.rb checking for pgconfig... no No pgconfig... trying anyway. If building fails, please try again with --with-pg-config=/path/to/pg_config checking for libpq-fe.h... no Can't find the 'libpq-fe.h header extconf.rb failed Could not create Makefile due to some reason, probably lack of necessary libraries and/or headers. Check the mkmf.log file for more details. You may need configuration options.
Provided configuration options:
To see why this extension failed to compile, please check the mkmf.log which can be found here:
/Users/David/.rvm/gems/ruby-2.4.0/extensions/x86_64-darwin-16/2.4.0/pg-0.19.0/mkmf.log
extconf failed, exit code 1
Gem files will remain installed in /Users/David/.rvm/gems/ruby-2.4.0/gems/pg-0.19.0 for inspection. Results logged to /Users/David/.rvm/gems/ruby-2.4.0/extensions/x8664-darwin-16/2.4.0/pg-0.19.0/gemmake.out
An error occurred while installing pg (0.19.0), and Bundler cannot continue. Make sure that
gem install pg -v '0.19.0'
succeeds before bundling.