Ruby on Rails 3 and MySQL on Ubuntu 10.10

Posted October 16, 2010 under Ubuntu

Ruby on Rails has become my favorite web framework recently. The sheer speed factor of development is incredible, and once you get past the learning curve, you can build anything in a short matter of time.

Recently my Asus laptop’s graphics card has become supported in the Ubuntu kernel so I now have a dual booting machine running both Windows 7 and Ubuntu. This is particularly handy now as I can use this laptop for development in Rails.

The steps aren’t hard to figure out on your own if you’re familiar with the tools, but this is what I did to get Rails 3 with MySQL on Ubuntu 10.10.

Installing Rails from Terminal

sudo apt-get install ruby-full

wget production.cf.rubygems.org/rubygems/rubygems-1.3.7.tgz

tar -xvf rubygems-1.3.7.tgz

cd rubygems-1.3.7/

sudo ruby setup.rb

sudo ln -s /usr/bin/gem1.8 /usr/bin/gem

sudo gem install rdoc

sudo gem install rails

Installing MySQL Server from Terminal

sudo apt-get install mysql-server libmysqlclient-dev libmysql-ruby

Setting up your first Rails project

It seems that Rails 3 depends on sqlite3 even if you don’t intend to use it as the backend for your application. We’ll create an example rails app to make sure everything is working.

sudo apt-get install libsqlite3-dev build-essential

rails new example

cd example/

bundle install

rake db:create

rails s

Now fire up your browser, and go to http://localhost:3000 and you should be greeted with a nice little rails homepage. :D

UPDATE: I recently just ran these commands over again and ran into a “File not found: lib” error. After a bit of googling, I found this and appended the solution into the instructions above: http://stackoverflow.com/questions/2709683/ruby-on-rails-beta-3-install-problem-on-snow-leopard

If you liked this post, follow me on Twitter or subscribe to my RSS feed.

Discussion

  • Brandon

    I noticed you use the package manager to install ruby. Since ruby and rails development moves faster than the package repositories, you may want to consider using rvm (http://rvm.beginrescueend.com/) to manage your installs. It makes for installing from source and switching between ruby versions fast and easy. Especially when you are changing between version 1.8.7 and 1.9.1 or now, 1.9.2

  • http://excid3.com Chris Oliver

    That’s definitely something I should probably be doing. I have been doing it out of laziness mainly, and have only needed 1.8.7 for what I have been working on. I’ll definitely check it out and make a post about it though! Thanks!

  • http://twitter.com/cedwardsmedia Corey Edwards

    Thanks for the info! I tried installing Ruby/Rails myself using the repo packages, and it worked, except it was old and outdated, so following the current Rails guides was pointless. Thanks to this tutorial, I’m now up to speed with the latest versions and ready to learn!

  • Liutauras

    Fast and easy!
    Thanks!

  • Pepe_lopera

    Was anyone gotten this error /rubygems-1.3.7/lib/rubygems/source_index.rb:68:in `installed_spec_directories’: undefined method `path’ for Gem:Module (NoMethodError)

  • Pingback: Coders' survival guide to Startup Weekend | codefixes.com

  • http://excid3.com Chris Oliver

    Are you using ruby 1.9 by chance?

  • Chris

    Thanks for the install instructions. After following the steps, running rake db:create generates the following error: “Could not find gem ‘sqlite3-ruby (>= 0, runtime)’ in any of the gem sources.”

    Any suggestion welcome :)

    Thank you
    Chris

  • http://excid3.com Chris Oliver

    You need to update your config/database.yml to use mysql instead, but if you wish to keep with sqlite3 you may need to do the following:

    sudo apt-get install sqlite3 swig libsqlite3-ruby libsqlite3-dev
    gem install sqlite3-ruby

  • Paulfenton

    everything seems to go well untill I run “sudo gem install rails”… when I do this the cursor moves to the start of the next line and just stops…. nothing appears to happen, it just waits untill I interrupt it.

    I am brand new to linux and I have a fresh installation of ubuntu 10.10.. I dont know why It wouldnt work…

  • http://excid3.com Chris Oliver

    Give it some time, rubygems are very slow sometimes and they display no progress information.

  • Paulfenton

    That seems strange, I wonder why it wouldn’t… it doesn’t even give me feedback that it has started..

  • http://excid3.com Chris Oliver

    I don’t know why they don’t have any progress either, but I just ran through it all again and the instructions do work.

    Now I seem to get a File not found: lib when it gets to installing rails which I have not come across before. Looking into that issue real quick.

  • Paulfenton

    Okay you were right the gems installed…

    Now I have a new problem :) … When running “bundle install” I get this error:

    Fetching source index for http://rubygems.org/
    /usr/lib/ruby/1.8/fileutils.rb:243:in `mkdir’: Permission denied – /home/paul/.gem/specs (Errno::EACCES)
    from /usr/lib/ruby/1.8/fileutils.rb:243:in `fu_mkdir’
    from /usr/lib/ruby/1.8/fileutils.rb:217:in `mkdir_p’
    from /usr/lib/ruby/1.8/fileutils.rb:215:in `reverse_each’
    from /usr/lib/ruby/1.8/fileutils.rb:215:in `mkdir_p’
    from /usr/lib/ruby/1.8/fileutils.rb:201:in `each’
    from /usr/lib/ruby/1.8/fileutils.rb:201:in `mkdir_p’

  • http://excid3.com Chris Oliver

    Try doing that as root, “sudo bundle install”.

  • Paulfenton

    I got that lib error as well… Were you able to run bundle install after getting that error?

  • Paulfenton

    “sudo bundle install” worked!!

  • http://ccgi.masterclose.plus.com/weblog/ Andrew Smith

    The latest version of rubygems in Ubuntu 10.10 is 1.3.7 so you can just “sudo apt-get install rubygems” rather than the five commands from wget to “sudo ln …”.

  • http://excid3.com Chris Oliver

    Ah I did not realize that since I have been so used to installing from source. :P Either way, it’s probably a good idea to install from source in case rubygems gets updated since Ubuntu hasn’t been on the ball with releasing new packages for it.

  • Chris

    rake db:create –database=mysql did the trick for me :)

  • jay

    awesome! thanks!

  • Pingback: reference links for rails and git on ubuntu lucid | web technologist

  • Ahmed Jolani

    Thank you very much!!!!!!! I was searching for that tutorial to continue my RoR tutorial in Lynda :) :)

  • Kingmilo

    Installing via apt-get runs into problems when trying to install rails, so I advise as per Chris to install from souce, thanks very much Chris.

  • Giovann

    I get a similar error with my Gemfile, when running bundle install. I am logged in as root though. I created my Gemfile before running the cammand
    bundle install
    /usr/lib/ruby/1.8/fileutils.rb:243:in `mkdir’: File exists – /home/cloud/public_html/Gemfile (Errno::EEXIST)
    from /usr/lib/ruby/1.8/fileutils.rb:243:in `fu_mkdir’
    from /usr/lib/ruby/1.8/fileutils.rb:217:in `mkdir_p’
    from /usr/lib/ruby/1.8/fileutils.rb:215:in `reverse_each’
    from /usr/lib/ruby/1.8/fileutils.rb:215:in `mkdir_p’
    from /usr/lib/ruby/1.8/fileutils.rb:201:in `each’
    from /usr/lib/ruby/1.8/fileutils.rb:201:in `mkdir_p’
    from /var/lib/gems/1.8/gems/bundler-1.0.7/lib/bundler.rb:259:in `configure_gem_home_and_path’
    from /var/lib/gems/1.8/gems/bundler-1.0.7/lib/bundler.rb:72:in `configure’
    from /var/lib/gems/1.8/gems/bundler-1.0.7/lib/bundler.rb:126:in `definition’
    from /var/lib/gems/1.8/gems/bundler-1.0.7/lib/bundler/cli.rb:225:in `install’
    from /var/lib/gems/1.8/gems/bundler-1.0.7/lib/bundler/vendor/thor/task.rb:22:in `send’
    from /var/lib/gems/1.8/gems/bundler-1.0.7/lib/bundler/vendor/thor/task.rb:22:in `run’
    from /var/lib/gems/1.8/gems/bundler-1.0.7/lib/bundler/vendor/thor/invocation.rb:118:in `invoke_task’
    from /var/lib/gems/1.8/gems/bundler-1.0.7/lib/bundler/vendor/thor.rb:246:in `dispatch’
    from /var/lib/gems/1.8/gems/bundler-1.0.7/lib/bundler/vendor/thor/base.rb:389:in `start’
    from /var/lib/gems/1.8/gems/bundler-1.0.7/bin/bundle:13
    from /usr/local/bin/bundle:19:in `load’
    from /usr/local/bin/bundle:19

  • http://www.benrhughes.com/oid/ Ben

    Thanks for this. After messing about with Rails on Windows I was expecting it to be:
    sudo apt-get install ruby
    sudo gem install rails

    Unfortunately the gems deb in the apt repo has something wrong with it and it only partially installs rails. Spent way too long trying to work it out before I found this post. So cheers :)

  • http://excid3.com Chris Oliver

    Glad I could help!

  • http://twitter.com/drjermy drjermy

    Awesome howto that took me from nowhere to installed in minutes. Thanks.

  • http://twitter.com/alanbraz Alan Braz

    very good, simple, fast and strait forward! Thanks a lot!

  • Missedflop

    mine did that… and it waits for a few minutes… but eventually continues running

  • Sravan

    thanks. its helpful

  • Ajay_0162

    awesome.First time using Rails and it looked so simple.. :)

  • http://excid3.com Chris Oliver

    Hope you like it! It’s wonderful. :D

  • Ajay_0162

    hey Chris can you tell me how to install spree on rails.I saw the documentation but it is not so good as yours.So please help me buddy

  • http://excid3.com Chris Oliver

    Check out the readme here: https://github.com/spree/spree

    It pretty much goes through all the steps you’ll need. If I can find a free hour or two some weekend, I could try to put together a tutorial for you, but it probably won’t be for a while.

  • Mark R

    No matter how many times I’ve tried it, when I get to either “rails new example” or “rails server”, I get:

    Could not find gem ‘sqlite3 (>= 0, runtime)’ in any of the gem sources listed in your Gemfile.

    This is with a clean install of Ubunto 10.10 running in vmware. Any ideas?

  • http://excid3.com Chris Oliver
  • http://twitter.com/verticalgambit JP Arnold

    Thanks this was sooooo much easier than the 5 hrs it took me to set Ruby environment on my Win XP laptop

  • http://excid3.com Chris Oliver

    Haha I bet! I’ve never run Rails on Windows, but a couple of my friends have and it’s been really annoying trying to get things running I have heard.

  • Goksel

    Very helpful post!
    If you need guides for ruby installation on Mac OS X (with mysql) you can check http://geryit.com/blog/2011/01/installing-mysql-with-rails-on-mac-os-x-snow-leopard/

  • http://profiles.google.com/nazar.kuliev nazar kuliev
  • http://profiles.google.com/nazar.kuliev nazar kuliev
  • http://www.onlineinvestingai.com/blog/ George

    I think the biggest problem with running on Widows is that it takes 10 minutes to start the server or run the tests.

  • http://excid3.com Chris Oliver

    That is awful. Are you running Rails 3 and Ruby 1.9.2? Speeds shouldn’t be that much different. Test do take quite a while to run, but still.

  • http://www.onlineinvestingai.com/blog/ George

    Yes, those are my versions. I read that under unix they run in a few
    seconds, so I installed Ubuntu.

  • http://www.facebook.com/jagan123 Jagan Ganti

    followed the above steps and started the server.In the about your application informationb,I get this error:

    RuntimeError

    Please install the mysql2 adapter: `gem install activerecord-mysql2-adapter` (no such file to load — active_record/connection_adapters/mysql2_adapter)ERROR: Could not find a valid gem ‘activerecord-mysql2-adapter’ (>= 0) in any repository

  • http://excid3.com Chris Oliver

    Which version of Rails did you install?

  • Cleverlemming

    Worked perfectly on Ubuntu 10.10. Thank you. 

  • Dthusira

    if you are using mysql then follow this step…worked for me.. 
    Go to Gemfile in your application folder 
    then adjust the gem “mysql2″ to  ’mysql’
    run it….

  • Jay

    I think you should add a “sudo” to the “bundle install”.  (I needed it as I took a break between steps.)

  • http://excid3.com Chris Oliver

    Good catch. I’ll make a not that the command might need sudo, but I don’t want to get people into the notion of always using sudo when they run gem and bundle being a good thing.

  • Nixnash

    hey ,I am a php developer , getting started to learn ruby on rails…I found your tutorial very helpful.
    Thanks

  • Pingback: Setting up Nginx to work with Rails, Capistrano and SSL. | stew (@) rtmatheson.com

  • Nara

    Chris,

    I recently installed Rails 3.1 on Ubuntu 11.04. The built-in sqlite3 version worked fine, but when I switched database.yml to mysql2 (also tried mysql, same issue), it failed in ActiveRecord::ConnectionNotEstablished. Clearing connections, rebooting etc. did not help. More details are in the Ruby on Rails Talk google group.

    Let me know if you know of a solution. Thanks.

    Nara

  • http://excid3.com Chris Oliver

    Hey Nara, what does your database.yml look like? That might be the cause or there could be another typo somewhere.

    Feel free to email me as well.

  • shital

    hey I’m new with  ruby and rails.I tried it but my problem at
    $rake db:createrake aborted!
    Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes.
    (See full trace by running task with –trace)
    I couldn’t browse localhost:3000
    I don’t usnderstand how to do it.
    Plz,Help me

  • http://excid3.com Chris Oliver

    Rails needs a javascript runtime now. Something like node.js needs to be installed in order to use it.

    Follow this tutorial: https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager

  • erturne

    Total ruby noob here. These instructions worked for me using the latest rubygems (1.8.10 as of Oct 30, 2011). I’m interested in Brandon’s comment on using rvm to manage installs. Have you tried that yet?

    I know, Ubuntu 10.10 is so last year! I’m not willing to give up Gnome until Unity is more mature.

  • http://excid3.com/ excid3

    @erturne rvm is great! Installation requires git and curl I think but that is all. You can simply copy paste the install script into terminal and then install whatever Ruby versions you like from source. I use it everyday on my mac for development.

    The only gotcha is that its tricky to setup with Apache so I wouldn’t plan on using it for production.

  • erturne

    I think I’ll not worry about rvm for now; I have lots of other things on my RoR learning curve! File its existence in my brain for the time when I do decide that I need different versions of Ruby. ;-)

    I’ve been doing Grails (and Groovy) development for the past two years. Since Grails was inspired by RoR I suspect that I’ll pick it up quickly. I started writing a web app in Grails to scratch one of my own personal itches, but changed my mind and thought it would be a great time to get some Ruby on Rails under my belt.

    Thanks again for writing this post. It made it very simple to get RoR installed!

  • itchap

    Nice guide, however if you want to use MySQL without installing SQLite, try using this command instead when creating a new app: sudo rails new app_name -d mysql

  • http://kiranatama.com/ Agus @ Ruby on Rails

    very nice guide .. maybe for a moment I’ll make a video guide for this guide..

    thanks