So, you want to develop Ruby on Rails applications? While loads of (introductory) tutorials are available for developing Ruby on Rails applications, there seems to be some uncertainty about setting up a lean and up-to-date local development environment.
This tutorial will guide you through the steps of setting up an Ubuntu local development machine for Ruby on Rails. Part 2 of this tutorial, which will be published here later, will help you through the steps to set up an Ubuntu VPS. For now, knowing that VPS stands for virtual private server is sufficient. It will be able to host your newly developed Ruby on Rails applications. But let’s focus on the local development machine first.
Further Reading on SmashingMag:
- Setup A Ubuntu VPS For Hosting Ruby On Rails Applications
- Getting Started With Ruby On Rails
- Successful Freelancing With Ruby On Rails
- 10 Useful Tips For Ruby On Rails Developers
Ruby On Rails? Ubuntu?
What are Ruby on Rails and Ubuntu? In short, Ruby on Rails is a Web development framework that lets you create Web applications using the Ruby programming language. As the official website states, “Ruby on Rails is an open-source Web framework that’s optimized for programmer happiness and sustainable productivity. It lets you write beautiful code by favoring convention over configuration.”
Ubuntu, meanwhile, is a “Debian-derived Linux distribution that focuses on usability.” It has been the most popular Linux distribution for the last couple of years. And even better, both Ruby on Rails and Ubuntu are open source and, therefore, completely free to use.
A Quick Overview
In this tutorial, we’ll install Ruby and RubyGems using the Ruby Version Manager (RVM) script. And we’ll install Rails and Capistrano in turn using RubyGems. The machine will also feature version control, provided by Git and a PostgreSQL database. A fresh installation of Ubuntu and a working Internet connection are assumed, but these steps should work on most (Debian- and Ubuntu-based) Linux distributions.
At the time of writing, the latest versions are Ubuntu 10.10, Ruby 1.9.2 and Rails 3.0.7. This tutorial was also tested on Ubuntu 10.04 and the upcoming Ubuntu 11.04 release.
During this tutorial, we will make extensive use of the Linux command line. Therefore, I have added a small glossary at the end of this article describing the relevant Linux commands.
Getting Up To Date
First, let’s get up to date. Log into your machine as a user with administrative (or sudo) rights, and open a terminal window. The commands rendered below need to be entered in this terminal window. The dollar sign (
$) is your command prompt, and the rest is as simple as typing the command and hitting “Enter.”
The first three commands will then update the package lists, upgrade currently installed packages, and install new packages and delete obsolete packages. This will give you a machine that is fully up to date. The final command will reboot the machine, which is good practice after updating a large number of packages.
$ sudo apt-get update $ sudo apt-get upgrade $ sudo apt-get dist-upgrade $ sudo reboot
Prepare Your Machine For RVM
After the machine has rebooted, log back in and open a terminal window. The RVM script needs some packages in order to be installed, namely Curl and Git. Curl is a tool to transfer data using a range of protocols (such as HTTP and FTP). And “Git is a free and open-source distributed version control system, designed to handle everything from small to very large projects with speed and efficiency.” Git is the choice for version control among most Ruby on Rails developers.
$ sudo apt-get install curl $ sudo apt-get install git-core
Git will be used by the RVM script, and we’ll be using it in part 2 of this tutorial. So, after installing the packages, let’s take a little time to configure it. Configuring Git is easy: just provide a user name and email address.
$ git config --global user.name "Your Name" $ git config --global user.email firstname.lastname@example.org
$ git config --global user.name "John Doe" $ git config --global user.email email@example.com
Now we can install RVM. RVM stands for Ruby version manager and “is a command-line tool that allows you to easily install, manage and work with multiple Ruby environments, from interpreters to sets of Gems.” The following command takes care of the installation of the script. RVM will get installed in the home directory of the currently logged-in user.
$ bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)
Navigate to the home directory, and edit the user bash profile. This ensures that the RVM script gets loaded every time the corresponding user logs in. To edit the bash profile, we’ll use Nano. Nano is a simple command-line text editor, and we will use it again in this tutorial.
$ cd $ nano .bashrc
Add the following line to the end of the user bash profile. After you have made the changes, save the file by pressing CTRL + O, and exit Nano by pressing CTRL + X. If you ever want to exit Nano without saving changes, hit CTRL + X and then N.
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"
Manually load the script into the current shell session with the following command or open a new terminal window. This way, the
rvm command will be made available.
$ source .bashrc
You can verify whether the RVM script is working by entering the following command:
$ type rvm | head -1
If everything is set up correctly, the shell should return that
rvm is a function. If it doesn’t, go over to the RVM website and look under “Troubleshooting your install” to see what you can do to make it work.
Prepare Your Machine For Ruby And RubyGems
RVM comes with a handy command to view the dependencies that are needed to compile and install Ruby and RubyGems from source.
$ rvm notes
See the dependencies listed under the regular version of Ruby, and install these. Some packages might already be installed.
$ sudo apt-get install build-essential bison openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev
Install Ruby And RubyGems Using RVM
First, we have Ruby, “a dynamic, open-source programming language with a focus on simplicity and productivity. It has an elegant syntax that is natural to read and easy to write.”
Then we have RubyGems. This is “the premier Ruby packaging system. It provides a standard format for distributing Ruby programs and libraries, an easy-to-use tool for managing the installation of Gem packages, a Gem server utility for serving Gems from any machine where RubyGems is installed, and a standard way of publishing Gem packages.”
Like the RVM command described above, there is also a command to see what versions of Ruby are available for installation using RVM. Have a look at the available Ruby versions using this command:
$ rvm list known
Install a regular version of Ruby. Because Ruby gets compiled from source, this step might take a while.
$ rvm install 1.9.2
Start using the installed Ruby, and set this version as a default.
$ rvm --default use 1.9.2
Check the Ruby and RubyGems versions to see whether they have been properly installed.
$ ruby -v $ gem -v
If necessary, manually updating RubyGems and the Gems is possible.
$ gem update --system $ gem update
Install Rails Using RubyGems
Rails Gem itself is all that’s left for Ruby to be put on Rails. Installing this is one of the easiest parts of this tutorial. It is installed using RubyGems, invoked by the
gem command. When the installation finishes, check the Rails version to see whether Rails has been properly installed.
$ gem install rails $ rails -v
Install Capistrano Using RubyGems
Capistrano is “an open-source tool for running scripts on multiple servers. Its main use is deploying Web applications. It automates the process of making a new version of an application available on one or more Web servers, including supporting tasks such as changing databases.” You can install Capistrano using RubyGems. Check the Capistrano version to see whether it has been properly installed.
$ gem install capistrano $ cap -V
PostgreSQL is “a sophisticated object-relational DBMS, supporting almost all SQL constructs, including subselects, transactions, and user-defined types and functions.” Install PostgreSQL together with a dependency. This dependency is needed to install the
pg Gem later on, which is responsible for the connection between PostgreSQL and Ruby on Rails.
$ sudo apt-get install postgresql libpq-dev
When the packages have been installed, move on to configuring PostgreSQL by securing pSQL. pSQL is the PostgreSQL interactive terminal, and it is used for administrative database tasks.
By default pSQL does not require a password for you to log in. We’ll change this by editing the authentication method and then reloading the PostgreSQL configuration. But first, assign a password to super-user
postgres. Log into pSQL, assign a safe password to
postgres, and then log out of pSQL.
$ sudo -u postgres psql # password postgres # q
Now modify the authentication configuration by changing
md5. This ensures that a password is needed to log into pSQL and that the password is encrypted. The two lines that need to be edited can be found near the end of the configuration file. After that, reload the changed configuration into PostgreSQL.
$ sudo nano /etc/postgresql/8.4/main/pg_hba.conf $ sudo /etc/init.d/postgresql reload
Most other PostgreSQL settings are stored in another configuration file. These settings can also be optimized, but that is beyond the scope of this tutorial. Keep in mind that a reload of the PostgreSQL configuration is required in order for changes to become active.
$ sudo nano /etc/postgresql/8.4/main/postgresql.conf
The installation of the local development machine is now done.
Testing The Set-Up
To make sure everything works, let’s develop a really small application and see a bit of Ruby on Rails in action. This process covers the following steps:
- Create a database user for the test application,
- Create a database for the test application,
- Create a test application using PostgreSQL as the database,
- Install the necessary Gems for the test application,
- Configure the database connections for the test application,
- Generate a simple scaffold for the test application,
- Migrate the results of the scaffold to the test application database,
- Start the built-in server,
- Check the test application in a Web browser,
- Stop the built-in server.
Once we have verified that everything works, we go through the following steps:
- Delete the database for the test application,
- Delete the database user for the test application,
- Remove the test application.
All of these steps should be performed on the local development machine. The conventions used to test the VPS are as follows (the database user name and database name derive from the name of the application):
Name of application:
Name of database user:
Password of database user:
Name of database:
First, create a database user for the test application using the
createuser command. We are using
postgres as the admin (or super-user) for PostgreSQL. The
P flag allows us to provide a password. The
> sign stands for the questions that will be prompted.
$ sudo -u postgres createuser -P > Enter name of role to add: test_app > Enter password for new role: apple > Enter it again: apple > Shall the new role be a superuser? (y/n) n > Shall the new role be allowed to create databases? (y/n) n > Shall the new role be allowed to create more new roles? (y/n) n > Password: your-postgres-user-password
Then, create a database for the test application that is owned by the test application user. While we could use Rake to create a database, we will use PostgreSQL, so that we learn some basic PostgreSQL administration. Create a new database by invoking the
createdb command in conjunction with the
O flag, the database user name and the new database name itself. The addition of
development at the end makes it the default database name. Here, you will be prompted for the PostgreSQL super-user password again.
$ sudo -u postgres createdb -O test_app test_app_development > Password: your-postgres-user-password
Now that the database has been set up, it is time to create the actual Ruby on Rails application. Navigate to your home directory, and create a new Rails application, named
test_app, using PostgreSQL as the database back end. The
d flag allows us to specify the preferred database software.
$ cd $ rails new test_app -d postgresql
Go into the Rails application directory, and install the necessary Gems using Bundler. Bundler takes care of “an application’s dependencies through its entire life across many machines systematically and repeatably,” and it “works out of the box with Rails 3.”
$ cd test_app $ bundle install
Use Nano to edit the database configuration file by adding
apple as a password under the development database configuration. Because we have followed convention, described in Box 1.1 above, the database user name and database name have already been taken care of.
$ nano config/database.yml
Now generate a basic scaffold. This will create a
User model and a
users controller. The inputs will consist of a name and an email address, which are both strings in the PostgreSQL database.
$ rails generate scaffold User name:string email:string
Use Rake to migrate the scaffold to the development database. Rake is an acronym for Ruby Make. It is a “simple Ruby build program with capabilities similar to Make,” which allows you to create and migrate databases.
$ rake db:migrate
By now, it is time to check the (working) application in a Web browser. Start the built-in server, and use a Web browser to navigate to
https://localhost:3000/. In particular, look at the application’s environment, and then look at
https://localhost:3000/users. Create, edit, view and delete some users.
$ rails server
When everything appears to be working correctly, you can stop the built-in server.
$ press CTRL+C
If everything has worked, then the test application database and test application database user can be removed. The
dropdb command removes a PostgreSQL database.
$ sudo -u postgres dropdb test_app_development > Password: your-postgres-user-password
dropuser command removes a PostgreSQL user.
$ sudo -u postgres dropuser > Enter name of role to drop: test_app > Password: your-postgres-user-password
Finally, navigate to your home directory, and recursively remove the test application. This leaves you with a fresh local development machine, ready for developing Ruby on Rails applications.
$ cd $ rm -r test_app
In part 2 of this tutorial, which will be published here later, we will help you through the steps necessary to set up an Ubuntu VPS that is capable of hosting (multiple) Ruby on Rails applications.
Linux Command Line Glossary
The relevant Linux commands for this tutorial are listed below in alphabetical order. A Linux command usually takes the form of
command -option(s) argument(s). For example,
rm -r test_app. For a more detailed description, use the manual pages, which can be accessed using
sudo [command]Used to execute a command as an administrative user.
apt-get dist-upgradeIn addition to performing the function of upgrade, this is used to intelligently handle dependencies.
apt-get install [package]Used to install (or upgrade) packages.
apt-get updateUsed to resynchronize the package index files from their sources.
apt-get upgradeUsed to install the newest versions of all packages currently installed.
bash < <( curl [location] )A combination of commands for obtaining and executing a script (from the Internet).
cd [location]Used to change the current working directory. Without an argument, it goes to the user’s home directory.
nano [file]Used to edit configuration files.
rebootUsed to reboot the system.
rm -r [directory]Used to remove a directory (recursively).
source [script]Used to force bash to read a specified script.
type [command]Used to display the kind of command that the shell will execute.
- The Linux Command Line
- The Linux Command Line Book
- Ruby on Rails Guides
- Ruby on Rails Tutorial
- Ruby on Rails Tutorial Book
- Git Community Book
- Pro Git
- Git Reference