February 11, 2012
This document supecedes Deploying a Ruby on Rails Development Environment onto Ubuntu Linux 8.04 or 10.04, which I had written almost two years ago. It omits some useful background information provided by my earlier document.
There are a number of excellent web resources for this installation procedure, which I referenced in preparing this guide. For further information, please consult these documents as listed in the Ruby on Rails Installation Bibliography.
While these instructions were performed on Ubuntu 10.10, they are likely to work well on most recent Linux distributions, and also (with appropriate modifications) on a Mac OS X system.
Comments and especially corrections are welcome. Please email me at edwin@edwinmeyer.com
The following instructions are intended specifically for deployment of a Ruby on Rails development (not a production) environment onto Ubuntu.
Ruby Version Manager (aka Ruby enVironment Manager), created by Wayne Seguin, allows a different Ruby version and set of gems (including Rails version) to be specified for each individual Rails project. Its use is rapidly becoming a best practice for Rails development. This document outlines only the specification of the latest Ruby and Rails versions (1.9.3p0 and 3.2, respectively, as of this writing) for all Rails projects.
Except as noted, the following instructions are consistent with those on the Installing RVM page as of February 5, 2012.
sudo apt-get install curl git-core
# Install packages required for RVM, including Git
bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)
# run a script which installs RVM
source ~/.bash_profile
# Reload your shell environment
rvm notes
# Outputs installation notes
Note: If the above works for you, skip over the following instructions for adding a similar line to .bashrc found in older RVM installation instructions. However, I have no .bash_profile file, so I performed the following:
echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"' >> ~/.bashrc
# Add line to .bashrc
source ~/.bashrc
# re-run .bashrc to to load rvm into present session. Or just open a new command window.
Enter the following on a single line, as specified by the rvm requirements
command output. Probably overkill, but won’t hurt.
sudo apt-get install build-essential 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 automake libtool bison subversion
rvm install 1.9.3
# install the latest and greatest as of this writing.
rvm --default use 1.9.3
# => ~/.rvm/rubies/ruby-1.9.3-p0/bin/ruby — Shows the location of this ruby version.
Note: This and future console sessions will use 1.9.3 unless overridden by a project-specific .rvmrc file or the rvm use <another ruby>
command.
ruby -v
# => ruby 1.9.3p0 (2011-10-30 revision 33570) [i686-linux]
ruby -ropenssl -rzlib -rreadline -e "puts :success"
# => success
Not absolutely necessary, but the following simple procedure per Daniel Kehoe ensures that you are using the latest gem and rake version:
gem -v
# version 1.8.15 comes with Ruby 1.9.3
gem update --system
# update to latest version — no change as of this writing.
rake --version
# version 0.9.2 comes with Ruby 1.9.3
gem update rake
# version 0.9.2.2 as of this writing.
The following procedure, again per Daniel Kehoe creates a special gemset named rails32
(for the ruby-1.9.3-p0
version) and sets it as the default. A slight difference to use a named gemset rather than the default
gemset, but it will eliminate confusion if you later use different Ruby and Rails versions.
rvm ruby-1.9.3-p0
@rails32 --create --default
rvm gemset list
# list all current gemsets: global and rails32 (selected) for ruby-1.9.3-p0
gem list
# list all gems for rails32, initially only bundler.
gem install rails
# the most recent stable release — 3.2.1 as of this writing
rails -v
# display the version
Note: Daniel Kehoe recommends that the Node.js server-side JavaScript environment be installed using the sudo apt-get install nodejs
command. Do so if an ExecJS::RuntimeUnavailable
exception occurs when running a Rails app. Make sure that the directory path displayed by which nodejs
is included in your $PATH variable. (The alternative, to add gem 'therubyracer'
to a Rails app’s Gemfile, is deprecated because of the large code size of 'therubyracer'
.)
mkdir rails_work
# create a directory to hold rails projects. It can be placed anywhere, with any name
cd rails_work
# be there
rails new test_app
# create the rails application test_app
in the rails_work
directory
cd test_app
# to the application root directory
rails generate controller Test try_me
# Create the TestController
controller with the try_me
action and associated view
rails server
# start a WEBrick server listening on local port 3000
In a web browser: http://localhost:3000/
# Displays the “Welcome aboard” static web page with links to useful information
Control-C
# exit the server
Again: http://localhost:3000/test/try_me
# displays the skeletal “Test#try_me” view.
That’s good enough for a smoke test in my book. On to (optionally) setting up a MySql database.
I installed a MySql server on my development environment, because that is what most of the production sites I develop to run on. Rails comes preconfigured with SQLite3. Skip this step if that is sufficient for you.
Install the MySQL database and its connector. (apt-get retrieves from the Ubuntu-specified repositories — Not necessarily the latest MySql version, but good enough.)
sudo apt-get install mysql-server mysql-client libmysql-ruby libmysqlclient-dev
Following the download, a window is presented asking for a password for the MySql ‘root’ user. Just press Enter to allow MySql access without a password. (This is OK for a development system where the database will not be accessible over the net, but never omit a password in a production system.)
Note: The enter password window was presented to me three times, for different packages. I pressed Enter each time.
Or you may enter a password. (If you do, you will need to enter the password into each Rails app database.yml configuration file.)
Note: The GUI MySQL admin tool is also installed. Not otherwise covered here, it can be invoked by typing sudo mysql-admin
gem install mysql2
# MySQL C Driver for Ruby
which mysql
# => /usr/bin/mysql
mysql --version
# => mysql Ver 14.14 Distrib 5.1.49, fomysqlr debian-linux-gnu (i686) using readline 6.1
sudo mysql
show databases;
# output should include the information_schema
system database plus mysql
exit
Note: If you entered a password above, type: mysql -p<password entered above -- no space after '-p'>
. You might also need to supply the user -u<user>
option. The default is root
.
Note 2: The MySql “daemon” is now configured to start automatically at Ubuntu startup.
sudo service mysql stop
# => mysql stop/waiting
— server was started upon installation; this stops it
sudo service mysql start
# => mysql start/running
— restart server
The following demo Rails app, originally by Micheal Benedict Arul, is almost trivial to create and deploy, yet it tests basic aspects of the Ruby on Rails installation, most critically database access. Brief instructions for creating and running the demo under Ubuntu using MySql follow.
cd rails_work
# switch to the rails projects directory as created above, if necessary
rails new articlesystem -d mysql
# create the articlesystem RoR app in the rails_work
directory. Omit -d mysql
if you are using SQLite3.
Note: rails new
automatically performs a bundle install
. However, perform this from the command line after any changes to the list of gems in Gemfile
.
sudo mysql
# also enter the password as above if needed
create database articlesystem_development;
show databases;
# should include articlesystem_development
in the database list
exit
Note: If you need to change the mysql username or enter a non-null password, you will have to use gedit or another text editor (I use geany) to insert/change these in the section for the articlesystem_development
database in the configuration file articlesystem/config/database.yml
. How to do this is not explained here.
cd articlesystem
rails generate scaffold Article title:string article:text
# creates code for the articles table. Isn’t it simple?
rake db:migrate
# create articles
database table with columns as specified by the migration created by rails generate scaffold
You might find yourself wanting to remove an application you have previously created, perhaps to recreate it afresh. Here’s how the existing articlesystem application and its MySql development database is purged.
cd rails_work
# switch to the project directory containing the rails app to be deleted, if necessary
rm -rf articlesystem
# delete the existing application directory hierarchy — use the -rf option with care!
sudo mysql
# plus password if needed
show databases;
# Should display articlesystem_development
drop database articlesystem_development;
# delete existing articlesystem_development
database
exit
cd articlesystem
# switch to the articlesystem
project root directory, if necessary
rails server
# start a WEBrick server listening on local port 3000.
Note: If you are still running the server from step 7, first type control-C
to exit the server and get a command prompt — necessary to release port 3000 for reuse. You can immediately reuse this command window rather than create a new one.
Note 2: Alternatively, issue the rails server -d
command with the -d
option to start it as a detached process. However, this prevents you from seeing server output useful for debugging. Also, you may later have some difficulty finding and killing the process.
Note 3: The previous version of this article included a step for installing and using the mongrel web server. However, that is unnecessary, especially since mongrel is no longer the rails app server of choice for production environments. To start rails using a non-WEBrick server, add the name of the server to the rails server
command, e.g. rails server mongrel
.
The following steps access the articlesystem
application on the local system from a browser.
http://localhost:3000/articles
# Shows a page titled “Listing articles” as created by the Rails scaffolding system
Create and view a new article
Click: "New Article"
Enter text in Title & Article fields.
Click: "Create"
If you now see “Article was successfully created.” and the title and article text that you entered, you can be assured that you have successfully deployed Ruby on Rails onto your Ubuntu system.
That’s all for now, folks!
The following are some of web pages I have consulted for preparation of this document. While I can not vouch for their accuracy, You may find them useful. All are specific to Rails 3, Ruby 1.9, and Linux.
Ubuntu, Ruby, RVM, Rails, and You — by Ryan Bigg, the co-author (with Yehuda Katz) of “Rails 3 in Action” – Manning, 2011
Ruby on Rails 3.1 installation tutorial for Linux Ubuntu and Linux Mint — Contains a link to a 35 page guide in pdf format with copious screen shots by Mircea Goia. Also contains instructions for installing Phusion Passenger and Apache 2.
Installing Rails 3 on Ubuntu 10.04 Lucid Lynx — A concise and slightly dated installation summary.
Read This Before Installing Rails 3.2 — by Daniel Kehoe. The most recently updated (4 February 2012) of these references.
Top Recommended Resources for Rails — also by Daniel Kehoe. An excellent concise list of various rails-related web resources.
Installing RVM — The official RVM installation instructions. Follow these in case of any doubt.
Ruby — The official Ruby language site.
Ruby on Rails — The official Ruby on Rails framework site.
Managing Rails Versions and Gems — An excellent summary of managing Ruby, Rails, and other related components by Daniel Kehoe.