Nitrous.IO and Heroku: A Perfect Pair

Share this article

256-spedometer

This article is a follow-up to Nitrous.IO: Rails Development in the Cloud published earlier. If you have not read that article, I recommend you do so. I will wait.

Although it is not a prerequisite, this tutorial assumes you have an account on Nitrous.IO and have setup a Box for Ruby.

Start with a Nitrous Box

If you have not created a Box yet on Nitrous.io, select the Boxes menu and create a new Box.

The Nitrous Box must be connected to your Github account. These instructions in the Nitrous Help Center are the best guide to accomplish that task.

Login to Heroku

This assumes you already have an account with Heroku. If not create one now. Heroku provides a very detailed API (called Toolbelt) and it is part of the Nitrous instance when you create a Box.

$ heroku login

You are expected to provide your login email and password. This should be greeted with an acknowledgement:

Authentication successful.
If this is the first time using this Nitrous Box to deploy to Heroku, you need to add your SSH key to Heroku. If you are not sure whether this step has been accomplished in the past, it will not hurt to just run the command again.
$ heroku keys:add

Create Your App

Create a Rails Application

Next you can simply create a Rails application. We will specify a PostgreSQL database (because that is what all the ‘cool kids’ are using nowadays).

$ rails new APPNAME --database=postgresql
$ cd APPNAME
Setup Git

The usual (and should be quite familiar) initial commit in Git to get it setup goes like this:

$ git init
$ git config --global user.name "Cody Wrighter"
$ git config --global user.email "C.Wrighter@ParaHacker.com"
$ git add .
$ git commit -m "Initial Commit on Nitrous Box"
Create a New Application on Heroku

With the Heroku Toolbelt you create an app on Heroku with this simple command:

$ heroku create

It will respond with the name of the app on Heroku. I recommend you copy the peculiar (and often poetic) application name for use in a later step.

Since Bundler is also included in the Nitrous instance, add these lines in the Gemfile:

ruby '2.0.0'  # to maintain parity between 'development' and 'production'

gem 'pg'  #this may already exist in the Gemfile
gem 'rails_12factor', group: :production

You should use RVM to install Ruby 2.0.0 on Nitrous. RVM is also included in every Nitrous box, so this is as simple as rvm install 2.0.0 followed by rvm use 2.0.0

Setting up a PostgreSQL database on Heroku is next.

Using the Heroku Toolbelt,

$ heroku addons:add heroku-postgresql:dev
will add a PostgreSQL database (the free level) and return a HEROKU_POSTGRES_COLOR_URL. Take note of the actual COLOR that is returned.

Then

$ heroku pg:promote HEROKU_POSTGRESQL_URL
to establish the database (You need to log into your Heroku Dashboard to get the PostreSQL details).

Normally, when creating a Rails application, the next step would be to edit the config/database.yml file. But with Heroku, your production database details are applied automatically on the Heroku server regardless of what you place in the database.yml file.

To use a Heroku PostgreSQL database for development also, follow the instructions here. If you prefer a local SQLite database for development, those instructions are at the end of this article.

Now it is time for Bundler to work its magic for us and pull all the pieces together.

$ bundle install

Setup the Asset Pipeline

Add a line in your config/application.rb

config.assets.initialize_on_precompile = false

Commit in Git

$ git add .
$ git commit -m "Heroku and Postgresql setup"

Deploy the Application to Heroku

Deploying to Heroku is the simplest part of this whole process. I truly appreciate that because it is the part that I do most often in a continuous development cycle.

$ git push heroku master

Setting Up the Server on Heroku

If you want to remain within the FREE realm, use a single dyno on Heroku.

$ heroku ps:scale web=1
$ heroku ps

Visit the App on the Server

At this point I like to perform a quick “smoke test” just to be sure we didn’t miss any of the essential steps. This saves a lot of headaches later when you are troubleshooting the application because you can be assured it works at the base level. This is where you will use the [sometimes poetic, sometimes profound] name Heroku created for your application. You can paste the name from your clipboard (if you followed my earlier advice)

$ curl http://<HEROKU_APP_NAME>.herokuapp.com/

Or you can navigate to the instance (based on the name that it returned earlier): I just open another tab in my browser that is currently connected to Nitrous.

You should get back the Rails default index.html page. We have not yet created any models, controllers, or routes so the “About your application’s environment” link will yield a server error.

That is to be expected.

To view the application logs, with Heroku ToolBelt, use heroku logs. The ‘status=200’ on the very last line is what we expect to see.

Now we need to migrate the database.

$ heroku run rake db:migrate

A Quick Assesment of Our Progress

You have successfully created and deployed a Rails application ENTIRELY FROM WITHIN A BROWSER !

Leveraging Nitrous.IO as a cloud-based development platform and Heroku as a cloud-based SAAS host, you can be completely divorced and indiscriminate about your client hardware and/or software.

Set Up the Development Server

In order to transform this empty, benign Rails application into your next big money-making Disruptive Web Product, we need to do some heavy testing-coding-refactoring Agile development. Let’s now get our Development environment setup.

In the Gemfile add gem 'unicorn', group: :development and run bundle install to install it locally.

To test (and for each iteration during development) run this command

$ bundle exec unicorn -p 3000 -E development

Use the Nitrous menu Preview -> Port 3000 to test your development environment

One Final Commit
$ git add .
$ git commit -m "use unicorn for local development"
$ git push heroku

If you prefer unicorn for production, here are details on setting it up.

Final Cleanup

Of course, it is important to rm the public/index.html file.

I recommend you also maintain the project in your Github account. As a public repository you can get help from the Open Source community in the form of contributions and Pull Requests. With a Private repository, you can collaborate with other developers on your team and maintain the central source control.

The Nitrous Box must be connected to your Github account. This great article in the Nitrous Help Center explains how to setup the Github Private Key.

First create the repository on Github. Then issue these two commands:

$ git remote add github git@github.com:<github_username>/nitrous_heroku.git
$ git push -u github master

Next, write some tests. And then you will create some models, controllers and views and begin actually developing the application.

Run the unicorn server with this command

$ bundle exec unicorn -p 3000 -E development

Use the Nitrous menu Preview -> Port 3000 to test your development environment






Update the Development Environment

If you prefer to use sqlite in development (which I do) then add these steps.

Modify the config/database.yml for ‘development’ (and ‘test’) like this:

development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  timeout: 5000

test:
  adapter: sqlite3
  database: db/test.sqlite3
  pool: 5
  timeout: 5000

Add sqlite3 to the Gemfile

group :development, :test do   #sqlite3 gem is not supported on Heroku
  gem 'sqlite3'
end

Call on Bundler

$ bundle install

Then Migrate the database

$ export RAILS_ENV=development
$ rake db:migrate

Give it a test drive by calling the unicorn server

$ bundle exec unicorn -p 3000 -E development

And then use the Nitrous menu Preview -> Port 3000 to open it in a browser

You now have complete development and production environments, along with git-based deployment, and you didn’t need to install anything on your local computer.

I am excited to usher-in a new Browser-based workflow.

Frequently Asked Questions (FAQs) about Nitrous.io and Heroku

What is the difference between Nitrous.io and Heroku?

Nitrous.io and Heroku are both cloud-based development platforms, but they serve different purposes. Nitrous.io is a cloud-based development environment that allows developers to write, run, and debug code in the cloud. It supports multiple programming languages and provides a collaborative environment for teams. On the other hand, Heroku is a Platform as a Service (PaaS) that enables developers to build, run, and operate applications entirely in the cloud. It supports several programming languages and offers a range of services for app deployment, scaling, and management.

How do I install the Heroku Toolbelt?

The Heroku Toolbelt, also known as Heroku CLI (Command Line Interface), is a tool that allows you to create and manage Heroku apps directly from the terminal. To install it, you need to download the installer specific to your operating system from the Heroku website. Once downloaded, run the installer and follow the on-screen instructions. After installation, you can access the Heroku CLI by opening your terminal and typing ‘heroku’.

Can I use Nitrous.io and Heroku together?

Yes, Nitrous.io and Heroku can be used together to streamline your development process. You can write and debug your code in Nitrous.io, then deploy your application to Heroku for hosting. This combination allows you to leverage the strengths of both platforms, making your development process more efficient and effective.

What are Heroku Buildpacks?

Heroku Buildpacks are sets of scripts that specify how your application and its dependencies should be built on Heroku. They are responsible for transforming your deployed code into a slug, which is a compressed and pre-packaged copy of your application optimized for distribution to the dyno manager.

How do I use the Heroku CLI?

The Heroku CLI is a powerful tool that allows you to manage your Heroku apps directly from the terminal. After installing the Heroku CLI, you can use it by opening your terminal and typing ‘heroku’ followed by the command you want to execute. For example, ‘heroku create’ will create a new Heroku app, and ‘heroku open’ will open your app in a web browser.

What programming languages does Nitrous.io support?

Nitrous.io supports a wide range of programming languages, including but not limited to Ruby, Python, JavaScript, Go, and PHP. This makes it a versatile tool for developers working in different programming environments.

How do I deploy my application to Heroku?

Deploying your application to Heroku involves pushing your code to a Heroku repository using Git. Once your code is pushed, Heroku will automatically build and deploy your application. You can then access your application by navigating to its URL in a web browser.

Can I collaborate with my team on Nitrous.io?

Yes, Nitrous.io provides a collaborative environment where you and your team can work together on the same codebase. It supports real-time collaboration, allowing multiple developers to write and debug code simultaneously.

What services does Heroku offer for app deployment, scaling, and management?

Heroku offers a range of services for app deployment, scaling, and management. These include Heroku Runtime, Heroku DX (Developer Experience), Heroku Data Services, and Heroku Elements. These services provide a comprehensive solution for building, running, and scaling applications.

How do I troubleshoot issues with my Heroku app?

Heroku provides several tools for troubleshooting issues with your app. These include Heroku logs, which provide a real-time stream of your app’s operational events, and Heroku metrics, which provide insights into your app’s performance. You can access these tools through the Heroku Dashboard or the Heroku CLI.

Thom ParkinThom Parkin
View Author

A self-proclaimed ParaHacker and avid Logophile, Thom has been writing software since the days when ALL telephones had wires. Leaving Fortran and COBOL behind and having become bored with Java, Thom is currently a Serial Rails Developer living in The Sunshine State. When not working on Open Source projects, developing articles and tutorials for Learnable, or helping to manage the Sitepoint Forums as a Team Leader, Thom is playing Euro-Games. With a reputation for puns and wordplay, Thom calls himself an avid Logophile and Polyphiloprogenitive Programming Polyglot.

nitrous.io
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week
Loading form