Dogma/Geek Outlet for my techno babble.

Installing Rails on Ubuntu [WIP]

This page will serve as a guide to the installation of Rails 3.0.x (currently 3.0.7) on a clean installation of Ubuntu. The whole process will be done using an SSH connection, logged in as 'root'. I am pretty new to Linux and have not relied solely on a command prompt for a loooong time, so this really will be a idiot's guide.

Now, 'command prompt', that's a telling phrase. My first computer was a Commodore 64, since then and upto about 7-8 months ago, my primary machine has always been Windows based. From about November 2010 I've made a gradual switch to OS X. My only requirement in Windows now is to test applications in IE and to maintain a couple of .NET applications that don't have Mono compatible binaries. So, as a result, this guide will assume a client machine with at least OS X 10.6 installed. However after so many years in Windows, please forgive me if slip into some MS terminology from time to time. Directories, not folders!

Note: I'll be using '#' to identify a prompt on the Terminal/Bash session.

Note: I make no recommendation, guarantee or warranty. This is what works for me, hopefully it work for you, but in a world of infinite possibilities, version number and software components. Who can really guarantee anything?

Logging On

Okay, I'll be logging onto the cleanly installed Ubuntu server using the Terminal app, entering the command below. OS X comes with a SSH client already installed. I believe Windows users can install something called PuTTY.

# shh root@00.00.00.00

The command above will attempt to connect to the given IP Address (00.00.00.00), using the user account 'root'. You'll then be asked for the root password. The user account 'root' in simple terms is the system administrator, a bit like 'Administrator' in Windows. Be warned, if you intend to log in as 'root', Linux assumes to a large extent that you know what you're doing. Be careful!

Note: If this is your first time SSH'ng on to the system, before asking for your password, you'll likely have answer a question about certificates.

If it's all worked, your prompt will have changed to something like 'root@computer-name' and we can progress to the next step.

Clean System

I'm still not clear on the different variations of Linux. All I know is that my target machine is Ubuntu Server, which means it's a very minimal set up. No Apache, no MySQL.

What you do get is 'apt-get'. 'apt-get' is a revelation to a user with a traditional expectation of the installation of software and components. Imagine being able to type in a simple command that allows you download, install, update and remove software without ever leaving the prompt. That is what 'apt-get' is.

Note: Logging in as 'root' removes the worry of when and when not to use 'sudo'. 'sudo' is like perform this command with elevated privileges. It should be used with caution for that very reason, but I understand that using 'sudo' unnecessarily can also have a negative impact on permissions in certain situations. 'sudo' is prefixed to the beginning of a statement like 'sudo apt-get install apache2'.

Web Server

My first task was to install Apache. Before that, I wanted to test the waters and install a little application called Lynx. Lynx is a lovely little text based based that runs within your Bash session. I use Lynx a lot to test accessibility across my sites, but it's also useful for debugging connection issues with a remote site; can't connect to it remotely? can you connect to it locally?

To install Lynx, type:

# apt-get install lynx

Note: Apt-get will ask you whether you're sure want to install the specified software (yes or no). You can also chain different software packages together and have 'apt-get' install them recursively (e.g. 'apt-get install lynx apache2').

Upon installing Lynx, you can load it up by typing in 'lynx'. For me this brought up an error, at this moment I appear to be unable to access external sites through Lynx. I know I can connect to the Internet, because 'apt-get' works and I was able to successfully ping a couple of domains. Typing in the command below should have the same result at the moment:

# lynx localhost

Lynx has an optional parameter allowing you to go straight to a specific web site. 'localhost' doesn't work yet because we don't have a web server set up yet!

Type in the following to install Apache:

# apt-get install apache2

Apache is the web server, it'll deliver your/my/our content to an unsuspecting world. We can confirm the installation was successful by opening up Lynx with 'localhost' again.

# lynx localhost

This time Lynx should behave, displaying 'It works!' at the top of your page. The content you're looking at is being delivered by the new Apache installation.

Note: You can exit out of Lynx by pressing 'q', followed by 'y' to confirm.

Ruby

I was in two minds about what to do next; it felt like I should be installing MySQL which'll be my production database, but I really wanted get Ruby on there as soon as poss. Because Ruby is cool.

I installed Ruby from source for one reason only; the 'apt-get' package for Ruby is out of date. I'm using Rails 3 and I want to use Ruby 1.9.2. Usually, I have to say, I still prefer installing binaries or using package managers like 'apt-get'. It just seems a bit less messy, but here we go.

First we need to install a few applications to make our task easier, we'll also update 'apt-get'.

# apt-get update

# apt-get install build-essential zlib1g-dev wget

To install Ruby from source, we first need to download the source. I'm using the latest version at the time of writing:

# wget ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.2-p180.tar.gz

'wget' will download the given location to the present working directory (PWD). We now need to unpack it into a directory of the same name.

# tar zxvf ruby-1.9.2-p180.tar.gz

Go into the root of the Ruby source:

# cd ruby*

Note: 'cd' accepts the * wildcard. The above command assumes you only have one directory that starts 'ruby'.

Now install:

# ./configure

# make

# make install

Note: Interesting in gotcha. Unless the path has been added to your environment, you cannot simply run an application that sits within the PWD, you must specify it's relative path which is './'. This may take a little while.

We can confirm that the installation has worked by typing in the following:

# ruby -v

ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-linux]

Before we leave Ruby, we need to do one more thing. Rails 3 relies on Open SSL, something that is not enabled by default in Ruby. This is done by typing in the following:

# cd ext/openssl

# apt-get install libssl-dev

# ruby extconf.rb

# make

# make install

Not sure how to test if it's worked other than to install Rails, but we're not quite at that stage yet.

Gems

Gems are little packages that you can download and use with you Ruby code. The 'gem' application is a package manager very similar 'apt-get'. To install it from source enter the following commands:

# cd ~

# wget http://rubyforge.org/frs/download.php/74849/rubygems-1.8.2.tgz

# tar zxvf rubygems-1.8.2.tgz

# cd rubyg*

# ruby setup.rb

# gem update --system

The last command will probably report that there is nothing to update, if you're installing the latest package.

Databases

At this point, we have a web server and we have Ruby. The last thing we need before installing rails is a database. I have installed two flavours; Sqlite and MySql. I'm going to install Sqlite now and leave MySql until after Rails is up and running.

Rails uses Sqlite by default, because it is super easy to setup. I tend to use Sqlite while I'm in the developing stages of an application, before switching to MySql in production. I'm sure there is a reason why I switch to MySql in production, presumably security or performance related, but I do it out of mindless 'best practice'.

To install Sqlite, enter the following commands:

# apt-get install sqlite3 libsqlite3-dev

# gem install sqlite3

The installation can be tested by typing in 'sqlite3'. Type '.quit' to exit.

Rails

I could prolong this no longer, I had to see Rails working. To install Rails type:

# gem install rails

# gem update

I want to create a test application, just to make sure it's all working, but first we need to find a place to put it.

Folder Directory Structure

Still getting my head around the *NIX (Linux, Unix, OS X) directory structure and permissions, so I'm not sure if the following is best practice or not. I tend to store web sites in '/home/www', to create the structure and enter it, type:

# mkdir /home/www

# cd /home/www

Testing the Rails installation

Type the following to create a rails site called 'test-site':

# rails new test-site

# bundle install

And finally, the following line to boot up the built-in web server (WEBrick):

# rails s

By default WEBrick will listen to 0.0.0.0:3000 for requests. You can test this is one of two ways:

Lynx

WEBrick will take over your existing SSH session, so if you want to test the site on the remote machine, you'll have to create a new SSH connection and type the following:

# lynx 0.0.0.0:3000

External Browser

Unlike Visual Studio's development web server, WEBrick can be accessed externally. So there is no reason why you shouldn't be able to access 'test-site' from a browser on your local machine, by appending ':3000' to the end of the remote machine's IP address, in the address bar.

# http://00.00.00.00:3000

Which ever method you use, you should now be looking at the Rails default start page.

Subversion

I have an exiting Rails application that I want to use, which is stored in a SVN repository. So the SVN client will to be installed to access the files.

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

(required)

No trackbacks yet.

Switch to our mobile site