Last updated 28 May 2016
All about Ruby on Rails on the Cloud9 hosted development environment.
With detailed instructions for getting started on Cloud9 with Rails including advice to get you fully productive.
If you’re new to Rails, see What is Ruby on Rails?, the book Learn Ruby on Rails, and recommendations for a Rails tutorial.
This is an article from the RailsApps project. The RailsApps project provides example applications that developers use as starter apps. Hundreds of developers use the apps, report problems as they arise, and propose solutions. Rails changes frequently; each application is known to work and serves as your personal “reference implementation.” Purchase the Capstone Rails Tutorials and you’ll support the project.
Cloud9 provides a hosted development environment. Simply put, you get access to a remote computer from your web browser.
The Cloud9 computer runs Ubuntu Linux, a popular operating system for Rails development. The online computer is instantly created with everything in place to immediately begin working on projects in Rails. This guide will orient you and provide additional details so you can be productive with Rails.
There is no cost to set up an account. You will only be charged if you decide to add extra storage or memory.
For ordinary Rails development, you don’t need extra storage or memory. For typical projects, the Cloud9 service is free.
The Cloud9 service gives you everything you need for Rails development, including:
For pair programming or other collaboration:
The Cloud9 service offers enormous value. However, these limitations may make it unsuitable for you:
In principle, Cloud9 allows you to do Rails development from any device that runs a web browser. Cloud9 does not officially support tablets or phones. To be productive:
Like any other Rails development environment:
Cloud9 can be used on any operating system that provides a web browser and an Internet connection.
As an alternative to installing Ruby directly on your own computer, Cloud9 can be beneficial:
Developers who travel frequently, or work at multiple sites, like cloud-based development because any computer provides access to a fully-configured, productive development environment. Additionally, cloud-based development means you will remain productive if your laptop is lost, stolen, or fails in service.
Most Macintosh users will prefer to install Ruby on Rails on Mac OS X directly. However, Cloud9 can be beneficial for the reasons listed above.
The Cloud9 shell environment will be familiar to any Ubuntu Linux user. Unlike Linux virtual private servers from companies such as Linode, the Cloud9 service is free and the system is maintained for you so you’ll spend more time as a developer and less time as a sysadmin. Cloud9 is particularly appropriate for newcomers who want to learn Unix but might be overwhelmed by the effort required to install and configure Ubuntu Linux.
Installing Rails on Windows is frustrating and painful. Students often give up on learning Rails because installation of Ruby on Windows is difficult and introduces bugs or creates configuration issues. Even when you succeed in getting Rails to run on Windows, you will encounter gems you cannot install. For these reasons, Cloud9 is an ideal option for a Rails development environment on a Windows laptop.
A Chromebook is a personal computer running the Google Chrome OS operating system. Chromebooks are among the least expensive laptops available, selling for as low as $200 (US). Chromebooks are intended for cloud-based computing with no local storage and all applications running in the web browser. As such, Cloud9 is ideally matched to Chromebooks. At $200 (US), a Chromebook with Cloud9 is probably the least expensive way for a student to get a fully functional Rails development environment.
The company makes money by selling memory and file storage. See the Cloud9 Picking a Plan article for details.
You can create any number of public projects (called “community workspaces”) and one private project (called a “premium workspace”). A free plan gives you 512MB of RAM and 1GB of disk storage. When you create a private project, you’ll use up the 512MB of RAM and 1GB of disk storage provided for a free plan. If you want more than one private project, you’ll have to upgrade to a paid plan.
Build as many Rails applications as you like by creating public “community workspaces.” If you want to keep your project private, the 512MB of RAM and 1GB of disk storage provided with a free plan is enough for a simple Rails application.
Visit the Cloud9 website to sign up.
There is no cost to set up an account. No credit card is required.
You can create an account using your own email address and password or you can establish an account using your GitHub account. I recommend creating a GitHub account (if you don’t already have one), then sign up using your GitHub account. This will link your Cloud9 and GitHub accounts for convenience.
After you establish an account and sign in, you’ll see the “Dashboard” page with an option to “Create a new workspace.”
A Cloud9 “workspace” is a virtual computer with a minimum of 512MB of memory and 1GB of disk storage, which is enough to build and run simple Rails applications.
From the “Dashboard” page, click “Create a new workspace.”
Enter a name for your workspace. Give it the name of the project or application you are building. This may be the same name you use for a GitHub repository.
You can add a short description of your workspace. This would be a good place to note that is a “Ruby on Rails project.”
If you’re a student learning Rails, choose the “Public” option for your workspace. Cloud9 public profiles and workspaces can be found in Google but it is not likely someone will find your project unless you tell them where to look. If you are working on a company project or your own startup, you may want to select the “Private” option. Selecting “Private” will use up the 512MB of memory and 1GB of disk storage provided with your free plan.
Cloud9 offers several “templates” for your workspace. A “template” is a preconfigured development environment. For Rails developers, Cloud9 offers a “Ruby” template, a “Ruby on Rails Tutorial” template, and a “Blank” template.
With any of these three templates, you’ll have a development environment that is already set up with a recent version of Ruby and Rails. At the time this was written, Ruby 2.2.3 and Rails 4.2.4 were the current versions and Cloud9 was providing Ruby 2.2.2 and Rails 4.2.1. Cloud9 is conscientious about updating versions of Ruby and Rails. It is never far behind the most currently releases and you can begin a project without updating versions.
For this article, choose the “Blank” template. If you choose the “Ruby” template, the workspace will be created with a default Rails application. The “Blank” template does not contain a prebuilt Rails application.
Click the “Create workspace” button and Cloud9 will build your virtual computer in a few seconds.
See the Cloud9 Creating a Workspace documentation for more.
It is not obvious how to delete a workspace you no longer need. Here’s what you need to know. On your Dashboard page, click the name of a workspace and you will see a summary page for the workspace. Click the gear next to the “Open” button to open a settings page. You’ll see a “Delete workspace” button.
As soon as you’ve built a new workspace, you’ll see the Cloud9 IDE (Integrated Development Environment).
You can find your way back to the IDE by visiting the Dashboard page and clicking the “Open” button for a workspace.
The Cloud9 IDE is very complex, because it is a full development environment that does the duty of a console, a file browser, a text editor, and a chat room.
Take time to get familiar with the interface. For example:
Drag the bar at the top of the console window to resize it. You’ll often need a bigger console window.
Click the little windows icon to expand or minimize the console window. You’ll often need a full console window.
Click the “plus” icon to open a tab for a second console window. You’ll often need multiple console windows. For example, you might leave a Rails server running in one console while entering Rails commands in another.
See the Cloud9 documentation for more about the IDE features.
Let’s use the console to explore the Cloud9 workspace and create a Rails application.
If you haven’t used the Unix command line interface (CLI) before, spend some time with The Command Line Crash Course to become comfortable with Unix shell commands.
When your workspace is created, Cloud9 creates a directory named workspace/.
Expand the console window and use Unix commands to explore the directory structure:
danielkehoe@my-project:~/workspace $ pwd /home/ubuntu/workspace danielkehoe@my-project:~/workspace $ echo $SHELL /bin/bash danielkehoe@my-project:~/workspace $ ls -la total 16 drwxrwxr-x 3 ubuntu ubuntu 4096 Sep 18 09:41 ./ drwxr-xr-x 18 ubuntu ubuntu 4096 Sep 18 09:41 ../ drwxr-xr-x 3 ubuntu ubuntu 4096 Sep 18 09:41 .c9/ -rw-rw-r-- 1 ubuntu ubuntu 699 Mar 26 14:16 README.md danielkehoe@my-project:~/workspace $
The current directory is /home/ubuntu/workspace.
The Unix command echo $SHELL
shows you that you are using the Bash shell.
The ls -la
command lists all the hidden files and folders.
Use the Unix command ls -lag ~
to see the contents of the home directory:
danielkehoe@my-project:~/workspace $ ls -la ~ total 88 drwxr-xr-x 19 ubuntu ubuntu 4096 Sep 18 10:01 ./ drwxr-xr-x 12 root root 4096 Sep 18 09:40 ../ -rw-r--r-- 1 ubuntu ubuntu 313 Mar 26 14:16 .bash_aliases -rw-r--r-- 1 ubuntu ubuntu 220 Apr 9 2014 .bash_logout -rw-r--r-- 1 ubuntu ubuntu 1665 Mar 26 14:16 .bashrc drwxr-xr-x 3 ubuntu ubuntu 4096 Sep 18 09:41 .c9/ drwxrwxr-x 5 ubuntu ubuntu 4096 Sep 18 10:00 .gem/ -rw-r--r-- 1 ubuntu ubuntu 90 Sep 18 10:02 .gemrc -rw-r--r-- 1 ubuntu ubuntu 228 Sep 18 09:41 .gitconfig -rw-r--r-- 1 ubuntu ubuntu 499 Mar 26 14:16 .gitignore drwx------ 2 ubuntu ubuntu 4096 Jun 10 12:32 .gnupg/ -rw-r--r-- 1 ubuntu ubuntu 499 Jun 10 13:02 .hgignore -rw-r--r-- 1 ubuntu ubuntu 68 Sep 18 09:41 .hgrc drwxr-xr-x 3 ubuntu ubuntu 4096 Jun 10 12:56 .node-gyp/ drwxr-xr-x 278 ubuntu ubuntu 12288 Jun 10 12:57 .npm/ drwxr-xr-x 13 ubuntu ubuntu 4096 Jun 10 12:56 .nvm/ -rw-r--r-- 1 ubuntu ubuntu 758 Jun 10 12:35 .profile drwxr-xr-x 2 ubuntu ubuntu 4096 Sep 18 09:41 .ssh/ drwxr-xr-x 3 ubuntu ubuntu 4096 Jun 10 13:02 lib/ drwxrwxr-x 3 ubuntu ubuntu 4096 Sep 18 09:41 workspace/
If you have some experience with a Unix environment or Rails, you’ll recognize configuration files for some familiar tools:
When you create a new workspace, the IDE opens in the workspace/ directory but you can also access the home directory, one level above the workspace/ directory.
You’ll see that Cloud9 has set an informative Unix command prompt for your shell:
danielkehoe@my-project:~/workspace $ whoami ubuntu
Your username appears with workspace name, as well as the current directory, followed by the familiar $
Unix command prompt.
For brevity, for the rest of this tutorial, I’ll show the command prompt as simple $
.
The Cloud9 workspace includes a pre-installed version of rvm, the Ruby Version Manager, a tool to switch Ruby versions. Several pre-installed versions of Ruby are available:
$ rvm list rvm rubies ruby-1.8.7-p374 [ x86_64 ] ruby-1.9.3-p547 [ x86_64 ] ruby-2.1.5 [ x86_64 ] =* ruby-2.2.2 [ x86_64 ] # => - current # =* - current && default # * - default
For this article, we’ll use the default Ruby 2.2.2 version.
If you are following instructions for an old tutorial, or working on an old Rails application, you can use an older version of Ruby by changing the default:
$ rvm --default use ruby-1.9.3-p547
If you wish, you can use RVM to install the newest Ruby version. The RVM website explains how to install RVM. Ruby 2.2.3 was current when this was written. You can check for the current recommended version of Ruby. However, unless you know there is a problem with the default version of Ruby, it is simpler to use the default version.
Your Cloud9 workspace has a recent version of Rails already installed. Rails 4.2.1 was offered when this was written.
If you are following a tutorial that specifies any 4.2 version of Rails, such as Rails 4.2.0 or Rails 4.2.4, you can use the version that is already installed in your Cloud9 workspace.
If you are following instructions for an old tutorial, or working on an old Rails application, you can install an older version of Rails. You can also update to the current version of Rails.
Cloud9 has already installed Rails directly into the global gemset. It is best to leave the global gemset unchanged and create a new gemset for a different Rails version.
Here’s how to make a gemset for the current stable release:
$ rvm use ruby-2.2.2@rails4.2 --create $ rvm gemset list
If you want the most recent stable release:
$ gem install rails $ rails -v
If you want the newest beta version or release candidate, you can install with --pre
.
$ gem install rails --pre $ rails -v
Or you can create a gemset for a different version of Rails.
For example, if you want the Rails 3.2.18 release:
$ rvm use ruby-2.2.2@rails3.2 --create $ gem install rails --version=3.2.18 $ rails -v
Here’s how to create a new Rails application in a Cloud9 workspace.
First, be sure you are in the /home/ubuntu/workspace folder:
$ pwd /home/ubuntu/workspace
It’s best to create your Rails application in this /home/ubuntu/workspace folder.
Here’s how to create a project-specific gemset, installing Rails, and creating a new application.
$ mkdir myapp $ cd myapp $ rvm use ruby-2.2.2@myapp --ruby-version --create $ gem install rails $ rails new .
We’ll name the new application “myapp.” Obviously, you can give it any name you like.
With this workflow, you’ll first create a root directory for your application, then move into the new directory.
With one command you’ll create a new project-specific gemset. The option “—ruby-version” creates .ruby-version and .ruby-gemset files in the root directory. RVM recognizes these files in an application’s root directory and loads the required version of Ruby and the correct gemset whenever you enter the directory.
When we create the gemset, it will be empty (though it inherits use of all the gems in the global gemset). We immediately install Rails. The command gem install rails
installs the most recent release of Rails.
Finally we run rails new .
. We use the Unix “dot” convention to refer to the current directory. This assigns the name of the directory to the new application.
This approach is different from the way most beginners are taught to create a Rails application. Most instructions suggest using rails new myapp
to generate a new application and then enter the directory to begin work. Our approach makes it easy to create a project-specific gemset and install Rails before the application is created.
The rails new
command generates the default Rails starter app. If you wish, you can use the Rails Composer tool to generate a starter application with a choice of basic features and popular gems.
For a “smoke test” to see if everything runs, display a list of Rake tasks.
$ rake -T
There’s no need to run bundle exec rake
instead of rake
when you are using RVM (see RVM and bundler integration).
You can launch and run the application with the Rails server:
$ rails server -p $PORT -b $IP
If you start the Rails server by using rails server
without any arguments, Cloud9 will display a helpful message that you may be using the wrong port and IP address for your server. You must specify the port and IP address using the environment variables provided by Cloud9.
To view the the running application, click the “Preview” link in the IDE menu (at the top of the page). There is a “Run” link, too, but it doesn’t work if you have created your Rails application in a folder within the workspace/ folder. You can fix the “Run” link by clicking the “CWD” button on the Run panel and selecting the directory of your application. You can save “Run” links by giving them a name (left most field in the Run panel).
You can also open a browser tab or window and enter the URL for the application, as hosted by Cloud9. When you launch the Rails server, Cloud9 displays a helpful message showing the URL where you can view your application.
You can stop the server with Control-c to return to the command prompt.
Most of the time you’ll keep the web server running as you add or edit files in your project. This a reason to open multiple console windows.
Changes will automatically appear when you refresh the browser or request a new page. There is a tricky exception, however. If you make changes to the Gemfile, or changes to configuration files, the web server must be shut down and relaunched for changes to be activated.
Working on a remote server means you can lose a connection (or close your console window) while the server is still running. If that happens, you may find you cannot start the server because you get an error, “A server is already running.” Use the provided PID file to find the Unix process ID and then stop the server with the Unix kill -9 PID
command.
$ rails server . . . A server is already running. Check /home/action/workspace/myapp/tmp/pids/server.pid. Exiting $ cat /home/ubuntu/workspace/myapp/tmp/pids/server.pid 4640 $ kill -9 4640
The Unix kill -9
command will stop the abandoned server process.
Git is an essential tool for developing with Rails. With Git, you can roll back code changes as needed, collaborate on code with others, and deploy your application for hosting on Heroku. See the article Rails with Git and GitHub for more information.
Git is pre-installed on your Cloud9 workspace. Check that Git is installed:
$ git version git version 1.9.1
If you created your account and logged in with GitHub, Git is already preconfigured with your user name and email.
Check that Git is configured correctly:
$ git config -l --global . . . user.name=Real Name user.email=me@example.com
You can make changes if necessary:
$ git config --global user.name "Real Name" $ git config --global user.email "me@example.com"
Use your real name so people will associate you with your work when they meet you in real life. There’s no reason to use a clever name unless you have something to hide. Here’s another tip: Life will be easier if you use the same email address for Git, your GitHub account, and Heroku.
You can use your Cloud9 workspace for pair programming.
Your pairing partner must have a Cloud9 account. Use the “Share” link (in the upper right corner of the page) to add members to the workspace. See the Cloud9 Share a Workspace article for more about adding members to a workspace.
Begin a pairing session by selecting the “Collaborate” link (on the right side of the page).
If you are physically present with your partner, at a shared desk or in the same room, Cloud9 is a good way to edit a project using two keyboards and two displays.
For remote collaboration, open a browser window for video chat (such as Google Hangouts) or use Skype for conversation.
The starter application you create with rails new
is very basic.
Use the Rails Composer tool to build a full-featured Rails starter app.
You’ll get a choice of starter applications with basic features and popular gems.
Here’s how to generate a new Rails application using the Rails Composer tool:
Using the conventional approach:
$ rails new myapp -m https://raw.github.com/RailsApps/rails-composer/master/composer.rb
Or, first creating an empty application root directory:
$ mkdir myapp $ cd myapp $ rvm use ruby-2.2.2@myapp --ruby-version --create $ gem install rails $ rails new . -m https://raw.github.com/RailsApps/rails-composer/master/composer.rb
You can add the -T
flags to skip Test::Unit if you are using RSpec for testing.
You can add the -O
flags to skip Active Record if you are using a NoSQL datastore such as MongoDB.
The RailsApps project provides example apps that show how real-world Rails applications are built. Each example is known to work and can serve as your personal “reference implementation”. Each is an open source project. Dozens of developers use the apps, report problems as they arise, and propose solutions as GitHub issues. There is a tutorial for each one so there is no mystery code.
Example Applications for Rails 4.2 | Tutorial | Comments |
---|---|---|
Learn Rails | Learn Ruby on Rails | introduction to Rails for beginners |
Rails and Bootstrap | Rails Bootstrap Tutorial | starter app for Rails and Bootstrap |
Rails and Foundation | Rails Foundation Tutorial | starter app for Rails and Zurb Foundation |
Mailing List with Active Job | Mailing List with Active Job Tutorial | background processing |
OmniAuth and Rails | OmniAuth Tutorial | OmniAuth for authentication |
Devise and Rails | Devise Tutorial | Devise for authentication |
Role-Based Authorization | Role-Based Authorization Tutorial | Simple authorization |
Pundit and Rails | Rails Pundit Tutorial | Pundit for authorization |
Sign Up and Download | Sign Up and Download Tutorial | Devise with authorization to download a PDF file |
Stripe Checkout | Stripe Checkout Tutorial | selling a product with Stripe Checkout |
Cloud9 provides support for users.
You can also find help on Stack Overflow. Your issue may have been encountered and addressed by others.
Daniel Kehoe wrote the article.
Get the book Learn Ruby on Rails to and support RailsApps if the article was helpful.
Comments
Is this helpful? Your encouragement fuels the project. Please tweet or add a comment. Couldn't get something to work? For the example apps and tutorials, it's best to open an issue on GitHub so we can help you.
comments powered by Disqus