Ubuntu Gutsy - MySQL and RoR

In the Ubuntu Gutsy setup articles (page 1 and page 2) we secured, updated and personalised the install ready to add some working software.

As such, we can now install MySQL and Ruby on Rails with subversion and Postfix support.


MySQL install

Let's go ahead and install MySQL:

sudo aptitude install mysql-server mysql-client libmysqlclient15-dev libmysql-ruby1.8 -y

MySQL Password

During the installation of MySQL, you will be presented with this screen:

MySQL install Screen

Setting the MySQL Root password is a recommended step in setting up your Slice. However, it is, of course, up to you.

Should you decide that protecting your production database is a good idea, then simply enter your chosen password as directed.

Note: The install will not ask for a repetition of the password so make sure you get it right when you enter the password!

InnoDB

If you are not going to use InnoDB with your MySQL databases, then it is a good idea to turn it off right at the start. It uses a lot of memory and, if not going to be used, this is wasted memory.

Open the main MySQL config file:

sudo nano /etc/mysql/my.cnf

Search my.cnf for the heading:

# * InnoDB

Then, to turn off InnoDB, simply uncomment (remove the #) on this line:

skip-innodb

Ruby on Rails install

Now we'll install ruby from the apt repositories and then install rubygems:

sudo aptitude install ruby1.8-dev ruby1.8 ri1.8 rdoc1.8 irb1.8 libreadline-ruby1.8 libruby1.8 libopenssl-ruby -y

Symlink

We need to create some symlinks from the install to locations every programme would look. Each line below is a separate command:

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

Version

Once done, we can have a look at the Ruby version:

ruby -v
...
ruby 1.8.6 (2007-06-07 patchlevel 36) [x86_64-linux]

Excellent. An up to date version.

Rubygems

Now we can install rubygems.

Update

This articles has been updated to use the latest (at the time of writing) version of rubygems which is v1.2.0.

This version of rubygems has finally fixed the memory issues seen on smaller Slices.

You can now install, update and administer all your rubygems from within a 256Slice with no issues.

Method

We are not going to use the package manager for this as the pre-package version of rubygems is 0.9.4 and, at the time of writing, the latest working version is 1.2.0.

Although you can update rubygems to the latest version from the command line, it is actually quicker and easier to install from source. This is especially true for smaller (256) Slices as the older version of rubygems had a horrendous memory issue when updating.

In fact, the previous version of this article did install via the package manager which was fine until the major updates to rubygems was released: Thanks to Janne, bovn, abenamer and Joel for the comments on workarounds, etc until it could be updated.

Source

Anyway, let's go ahead and download the source code into our sources directory. If you haven't got a sources directory, you can simply create one:

mkdir ~/sources
cd ~/sources
...
wget http://rubyforge.org/frs/download.php/38646/rubygems-1.2.0.tgz

Then unpack it and move into the newly created folder:

tar xzvf rubygems-1.2.0.tgz
cd  rubygems-1.2.0

Now we can go through the simple process of compiling it and creating a symlink:

sudo ruby setup.rb
...
sudo ln -s /usr/bin/gem1.8 /usr/bin/gem

Version

Once done, we can check the gem version with a:

gem -v
...
1.2.0

Excellent. Up to date.

Update

Whenever you need to update the installation just enter the following:

sudo gem update
sudo gem update --system

Rails

Now we can install rails with the command:

sudo gem install rails

The basic Ruby on Rails install is now finished.

ImageMagick

If you want to use Ruby and ImageMagick, we can extend the install as follows:

sudo aptitude install imagemagick librmagick-ruby1.8 librmagick-ruby-doc libfreetype6-dev xml-core -y

Testing

Now we can do a test to see if the install works (thanks to brainspl.at for this idea).

irb
#irb(main):001:0>
require 'RMagick'
#=> true
#irb(main):002:0>
require 'mysql'
#=> true
#irb(main):003:0>
exit

Obviously, if the output does not return 'true' for each 'require' command, then something has gone wrong. If the result is correct and returns 'true' then we have a working ruby install.

Postfix and subversion

Finally, we need to install postfix and subversion so we can 'check-out' plugins and send mail from our Rails applications:

sudo aptitude install postfix subversion -y

Answer the postfix questions as you see fit but the defaults usually suffice for our purposes as we are only using it to send emails and not for receiving mail.

That's it

You now have a MySQL, Ruby on Rails and postfix/subversion stack setup and ready to roll.

Now you are ready to install the server of your choice whether that be Litespeed, Nginx or Apache.

PickledOnion.

Article Comments:

MichaelT commented Sat Nov 24 04:44:48 UTC 2007:

Well done again. I followed through without a hiccup.

Steve commented Sun Nov 25 14:41:00 UTC 2007:

An excellent how to.

It seems installing mysql installs and runs exim4. If we install postfix is there a way to uninstall exim and have mysql use postfix instead? Some Googling seemed to imply uninstalling exim would also uninstall mysql.

PickledOnion commented Mon Nov 26 12:46:55 UTC 2007:

Hi Steve,

As far as I can recall from the setup, installing Postfix removes exim4.

It is one of the reasons I put the Postfix install in this this location as it then sets up the install for easier administration (IMO) than using exim4.

Of course, if anyone prefers exim4 they can simply leave out the Postfix installation.

PickledOnion.

Tom Styles commented Mon Dec 10 12:24:06 UTC 2007:

Hi Pickled Onion, Just finished getting our Nottinghamshire County Council slice into order, thanks to your tutorials it was pretty easy, and I've learnt a lot too.

One thing I felt was missing was the set up for PHPmyadmin. It took me a while to figure it all out and yet there aren't that many steps to it. I'm sure if you put an article together it would save many people a headache.

Keep up the good work. Tom

jeremy commented Wed Dec 19 23:28:09 UTC 2007:

I cannot thank you enough for this tutorial.

Trevor commented Mon Jan 14 18:31:02 UTC 2008:

I just went through some painful nonsense with Postfix, and it seems that the default bin location in main.cf is incorrect for Gutsy -- /usr/libexec/postfix. Turning on Postfix daemon didn't work until I changed that line to be /usr/lib/postfix

Wolfram Arnold commented Tue Jan 29 21:52:00 UTC 2008:

The Symlinks are not necessary if you install the corresponding 'dependency' packages. Debian uses 'dependency packages' to update to later version of software. Here they would be:

sudo aptitude install ruby ri rdoc irb -y

Dave South commented Fri Feb 01 23:42:40 UTC 2008:

I would also recommend running mysqlsecureinstallation after installing mysql-server. It removes anonymous users, disallows remote root mysql access (very important), and removes the test database.

Dave South commented Tue Feb 05 20:38:40 UTC 2008:

Markdown changed my post. The command is mysql_secure_installation.

Fred Lee commented Wed Feb 20 18:26:55 UTC 2008:

I recently did a brand new install of MySQL on Gutsy. And, for the first time, I am having problems. Usually, it is a really smooth. But, this time, it seems that my root user does not get created. I only have the debian-sys-maint user. I also have persmissions problems accessing the /tmp folder. Has anyone else experienced this?

Tod Thomson commented Tue Mar 18 07:37:56 UTC 2008:

sudo ln -s /usr/bin/ruby1.8 /usr/local/bin/ruby

You don't need this as it is done already.

sudo ln -s /usr/bin/ri1.8 /usr/local/bin/ri

sudo ln -s /usr/bin/rdoc1.8 /usr/local/bin/rdoc

sudo ln -s /usr/bin/irb1.8 /usr/local/bin/irb

these should probably be /usr/bin as per the ruby and gem symlinks...

d commented Thu Mar 20 02:52:39 UTC 2008:

uh... so... I know i've got rails now, but where should i put my rails app?

kemuri commented Tue Mar 25 13:18:46 UTC 2008:

Hmm, i followed every step, except the rmagic one, and the require 'mysql' irb command returns file not found..

What can be the problem?

Steven Ross commented Tue Apr 08 12:26:24 UTC 2008:

@d you put your railsapp anywhere (probably your ~/ ) and start it with ruby script/server you should probably follow a rails tutorial, that will get you up and running.

Greg P commented Tue Apr 08 18:47:06 UTC 2008:

@kemuri

maybe you didnt install libmysql-ruby as per the very first line?

Dejay Clayton commented Wed Apr 09 18:16:08 UTC 2008:

Great tutorial. Just a word of warning, though - ironically, mysqlsecureinstallation uses insecure methods of setting the root password. For example, although this isn't a problem for typical slicehost users, mysqlsecureinstallation actually writes out the entered root password into a plain-text file in the root directory. If the specified root password has a '#' anywhere in it, the script will bomb with an 'access denied' error, due to the unsophisticated manner in which the script passes variables to mysql commands.

Dejay Clayton commented Wed Apr 09 18:25:46 UTC 2008:

Oh, just as a follow up, after running mysql-secure-installation, make sure there are no .my* files in the root directory. If there are, delete them - they will have the root mysql password in plain text.

alimills commented Sun Apr 20 08:50:27 UTC 2008:

When installing RubyGems, do not install version 1.1.1 (as of today, the latest) because of memory issues mentioned http://forum.slicehost.com/comments.php?DiscussionID=1656&page=1#Item_1 and http://forum.slicehost.com/comments.php?DiscussionID=672&page=1. Install 1.1.0 (http://rubyforge.org/frs/download.php/34638/rubygems-1.1.0.tgz) instead. Version 1.1.0 is good to go.

Brandon commented Thu Apr 24 05:57:26 UTC 2008:

PickledOnion: I would recommend moving your warning about gems 1.1.1 up to before we install it. I had already installed 1.1.1 when I got to that warning, and had to go back and remove it by hand and install 1.1.0 instead.

Also, gem install rails didn't do anything until I did gem sources --add http://gems.rubyonrails.org.

Don commented Thu Apr 24 22:46:09 UTC 2008:

I just ran into a memory problem trying to install rails on a brand-new Hardy Heron slice (256MB).

I installed gems version 1.1.0, as recommended above. My next command was: "sudo gem install rails."

It installed the 7 gems, but then it got stuck on the ri documentation (for activerecord-2.0.2).

I logged in through a different terminal and saw this memory usage: total used free shared buffers cached Mem: 256 253 2 0 0 3 -/+ buffers/cache: 249 6 Swap: 511 259 252

Those numbers are changing slightly as I update the "free -m" command, so I think it will work itself out eventually. If there's a suggestion on how to avoid this for the future, I'd love to see it.

Thanks!

John commented Thu May 01 18:00:07 UTC 2008:

I just tried to install rails 1.2.6 and found that it coulnt be found. It appears it's been removed from the default sources. This worked for me:

sudo gem install rails -v=1.2.6 --source=http://gems.rubyforge.org/

Michael Koukoullis commented Fri May 02 04:06:41 UTC 2008:

What happened to the MySQL fine tuning instructions in this article? I used them frequently but never bothered to write them down. Have they been removed? Can you pout them back up please?

PickledOnion commented Fri May 02 09:21:32 UTC 2008:

Michael,

I removed them as they made the article more complicated than it had to be.

I will be putting a MySQL section up which will include many options for optimisation.

PickledOnion

RickD commented Fri May 16 18:32:46 UTC 2008:

I just got hit with the same thing as Don- when I do a 'gem install rails' my CPU and memory are pinned at 100% for about 5-7 minutes- things calm down right after. It scared me at first- but seems not to have hurt anything.

RickD commented Fri May 16 18:35:48 UTC 2008:

Also, you might want to add: on newer installs you might already have a bad 'gem' - you can tell if doing a 'gem -v' throws an error.

All you have to do to fix that is 'sudo rm /usr/bin/gem' and then create the symlink with 'ln -s /usr/bin/gem1.8 /usr/bin/gem'

Andrew commented Sat May 24 22:21:42 UTC 2008:

was getting the following error with the updated gems:

/usr/bin/gem:23: uninitialized constant Gem::GemRunner (NameError)

solution was here: http://www.nickpeters.net/2007/12/31/fix-for-uninitialized-constant-gemgemrunner-nameerror/

In case it disappears: "Simply add the line to the file /usr/bin/gem (may be different on a mac) require 'rubygems/gem_runner' after require 'rubygems' This fixed the issue in both Ubuntu and OS 10.4."

Brandon Zylstra commented Sat Jun 14 16:23:33 UTC 2008:

WARNING: some apps are written with the expectation that InnoDB will be available, and can only be expected to be reliable with InnoDB. In my case a PHP app I installed needs it, but didn't complain that it wasn't there and installed just fine with MyISAM. Just before putting it into production I discovered it needs to have InnoDB for some things to work properly.

Pain in the neck! Check before disabling this if you're installing an app someone else wrote, especially if it's designed for high usage and handles data that is critical to get right (like financial transactions!).

Alex commented Mon Aug 18 15:13:33 UTC 2008:

Any chance we will see instructions on how to install/upgrade from source? This seems like it might be a worthy topic to cover given recent security holes in ruby. As of August 08, new vulnerabilities were announced, and Ruby 1.8.6-p286 and all prior versions are vulnerable. I do not believe the version in the aptitude repository is up to date. Check http://ruby-lang.org for more info.

Daniel Waite commented Wed Aug 27 18:41:43 UTC 2008:

I agree with Brandon Zylstra.

DO NOT DISABLE INNODB unless you know what you are doing.

I have a empty Rails application that failed when trying to read database tables. I enabled INNODB and it worked fine.

Ani SEO commented Sun Mar 28 16:36:25 UTC 2010:

I am pretty new to using commandline to administer the server. But, this article is very very helpful. thanks.

-Sudha Ani SEO

Events in Pittsburgh commented Sun Feb 06 19:14:22 UTC 2011:

Hmm, i followed every step, except the rmagic one, and the require 'mysql' irb command returns file not found.. What can be the problem?

Plano dental commented Wed May 11 09:01:57 UTC 2011:

Interesting post and thanks for sharing. Some things in here I have not thought about before. Thanks for making such a cool post which is really very well written. I will be referring a lot of friends about this.

Preston commented Fri Jun 17 20:14:15 UTC 2011:

Thanks for the clear instructions. I have RoR running fine with WEBrick on port 3000 using mysql. And I have Apache2 set up to run RoR, at least I get the default application description screen.

Austin Computer Repair commented Thu Dec 22 09:44:53 UTC 2011:

Thanks for these clean instructions. Would try Ubuntu Gutsy in a few days. Glad ta have found this topic, will be very helpful.

V2 cigs coupon commented Wed Feb 20 18:20:26 UTC 2013:

Great instructions, Ubuntu Gutsy IM gonna try.

Want to comment?


(not made public)

(optional)

(use plain text or Markdown syntax)