Ruby on Rails Ubuntu Server VM with VMPlayer

I had to setup a Ubuntu (8.04) Server virtual machine and get Rails up-to-speed on it this morning, so I thought I would document my steps for those who find the need to repeat. Here is what I needed:

What you need to start:

  1. VMware Player installed. Download it from VMware directly and follow their instructions (aptitude has older version 1.x)
  2. Download the Ubuntu 8.04 Server ISO file for your machine. For torrent users, download the current i386 torrent file.
  3. Download my VMX and VMDK files (vmplayer config and empty virtual disk).

Step 1: Setup Directory and Config File

Create a new directory somwhere (I called mine ‘Ubuntu840Server’) and copy the Ubuntu ISO file and the VMX and VMDK (don’t forget to unzip) files into it. You will need to do some quick editing to the Ubuntu840Server.vmx file, so fire up your favorite editor (in Ubuntu Desktop, right-click and Open with “Text Editor”). The only sections you may need to edit are:

  • ide1:1.filename, make sure it matches the name of your ISO file, something like “ubuntu-8.04.1-serverr-i386.iso”
  • fileSearchPath, change to the path of the directory you created

Step 2: Run VMWare Player

Run VMware Player and open the Ubuntu840Server.vmx. If all the config settings are correct and the ISO files is in the directory, you should boot into Ubuntu Installer.

Step 3: Install Ubuntu Server

I won’t go into details here because Ubuntu has step-by-step instructions on how to install. But the short story is: accept the defaults.

There is one selection that will make installation easier, select LAMP Server when asked for the Software selection. LAMP stands for Linux/Apache/MySQL/PHP. It will install all those packages and configure them automatically for you.

Note: even though I selected a LAMP server, for some reason it did not install Apache/Mysql/PHP. If this happens to you, just do the following after the server re-boots:

sudo apt-get install apache mysql-server php5 libapache2-mod-php5 php5-mysql

Step 4: Get the Latest Updates

If everything went well, you should reboot into Ubuntu Server and see the login: prompt. Go ahead and login with the username and password you supplied during the installation. Make sure you have the latest updates for Ubuntu by running (this may take a while):

sudo apt-get update
sudo apt-get upgrade

Step 5: Install OpenSSH

I like to administer my VMs from the terminal via SSH. Ubuntu Server does not come preconfigured with an SSH server, so lets install one:

sudo apt-get install openssh-client
sudo apt-get install openssh-server

You don’t really need the openssh-client, but I like to have it in case I ever need to ssh from the server to somewhere else. I can’t recommend enough that you secure your SSH server. If you server is exposed to the internet, it will be attacked, and one of the favorite vulnerabilities is SSH. Read this article on how to Secure an OpenSSH Server!

Step 6: Install phpMyAdmin

Since we are using MySQL, having a graphic administration program is nice and it’s hard to beat phpMyAdmin. I installed it from aptitude, but it did not set the proper aliases for apache2, so there are a few extra steps:

sudo apt-get install phpmyadmin

# create a site file for apache2
sudo pico /etc/apache2/sites-available/phpmyadmin

# enter the following in pico
Alias /phpmyadmin/ "/usr/share/phpmyadmin/"
Alias /phpmyadmin "/usr/share/phpmyadmin/"
<Directory "/usr/share/phpmyadmin/" >
  Options Indexes FollowSymLinks MultiViews
  AllowOverride None
  Order allow,deny
  Allow from all
  Deny from none

# close and save pico, then enter this to enable the phpmyadmin site
sudo a2ensite phpmyadmin
sudo /etc/init.d/apache2 restart

Step 7: Install Ruby

Next we need to install ruby. We’ll use the aptitude pre-built binaries to save time and make it easier to update in the future, but you may wish to install it from source. We also install the sqlite3 database because it is the default used by Rails 2.X and many test packages, but you can omit if you want.

sudo aptitude install ruby1.8-dev ruby1.8 ri1.8 rdoc1.8 irb1.8 libreadline-ruby1.8 libruby1.8 libopenssl-ruby sqlite3 libsqlite3-ruby1.8 build-essential mysql-client libmysqlclient15-dev postfix

Note that we installed the ‘build-essential’ package. This package is necessary for you to install any other software from source (like native msql drivers and phusion passenger). We also install the postfix package, you can just accept the defaults.

Next you will need to set some symlinks so that the libraries will be located when needed.

sudo ln -s /usr/bin/ruby1.8 /usr/bin/ruby
sudo ln -s /usr/bin/ri1.8 /usr/bin/ri
sudo ln -s /usr/bin/rdoc1.8 /usr/bin/rdoc
sudo ln -s /usr/bin/irb1.8 /usr/bin/irb

Now, when you should see your ruby package when you check the version from the terminal.

karl@ubuntuserver:~$ ruby -v
ruby 1.8.6 (2007-09-24 patchlevel 111) [i486-linux]

Step 8: Install RubyGems

RubyGems is a package manager like aptitude that makes is trivial to add/update/delete resources that ruby (and Rails) will need. It’s also how you will install and update Rails. We are going to install RubyGems from source as the aptitude package is quite out-of-date. RubyGems is a self-updating application, and when installed from aptitude, aptitude will complain and you may accidentally downgrade RubyGems.

# create a sources directory in your home
mkdir ~/sources
cd ~/sources

#download gem package, see rubygems homepage for the latest

# decompress the files and setup rubygems
tar xzvf rubygems-1.2.0.tgz
cd rubygems-1.2.0
sudo ruby setup.rb

# add a symlink
sudo ln -s /usr/bin/gem1.8 /usr/bin/gem

#update rubygems
sudo gem update --system
sudo gem update

Step 9: Install Ruby on Rails

All this work, and we are finally here. Too bad installing Rails is so easy. Just this one line.

sudo gem install rails
and a couple more gems you will probably need…
# install the native (faster) mysql driver
sudo gem install mysql

# if you want native (faster) sqlite3 drivers, for production
sudo apt-get install libsqlite3-dev
sudo gem install sqlite3-ruby

You can install any more gems you may need at this time with ‘sudo gem install package-name‘, but if you are using rails 2.1 or greater, you can now add rubygem requirements to your environment.rb file and use rake to automatically installed gem dependencies.

Step 10: Install Phusion Passenger

There are many ways to host a Rails application, but at this time, Phusion Passenger gets the nod for the easiest to deploy and our experience has been rock-solid in reliability. These install steps are from the Passenger website installation instructions:

sudo gem install passenger
sudo apt-get install apache2-prefork-dev
sudo passenger-install-apache2-module
# and follow the instructions

# after phusion installs, you will see instructions to edit your Apache configuration file
# to add lines to the end of the file
sudo pico /etc/apache2/apache2.conf

# add the lines given in the installation screen, because they can be different for each machine, I can't post the instructions here

# restart apache webserver
sudo /etc/init.d/apache2 restart

You will also be given a sample virtual host file to setup your Rails application. At this point you can copy an existing Rails app over to the new server, or follow along for a quick setup of a new Rails app.

Step 11: Create a Rails Application (optional)

We are going to create a very quick Rails application to test that our installation is working.

cd ~/
rails tester
script/generate scaffold Blog name:string content:text
rake db:migrate RAILS_ENV=production

# setup apache
sudo a2dissite default # we don't need this anymore
sudo pico /etc/apache2/sites-available/tester

#enter the following in pico
<VirtualHost *:80>
  ServerName localhost
  DocumentRoot /home/user_name/tester/public

# activate this site
sudo a2ensite tester
sudo /etc/init.d/apache2 restart

Now you can visit your site by going to http://address_of_server/ and you should see your rails application!

Posted in Rails, Ruby. Tagged with , , .

4 Responses