Install Ruby on Rails on Ubuntu Hardy Heron 8.04
Posted by Vince Wadhwani on Apr 28, 2008
I recently had to do a fresh installation of Ruby on Rails and Nginx/Mongrel on Hardy Heron server. I compared notes with my last HowTo along the way and decided to write it up in case anybody needs help with the process. If you're struggling with installing Rails, follow along and see if this helps. If you want to scale far and wide on your own I'd suggest popping by Ezra's page and checking out his book. The rest of you.. please read on!
Step 1: As usual, the first thing we'll want to do is make sure your version of Hardy Heron 8.04 is up to date.:
sudo apt-get update sudo apt-get dist-upgrade
Step 2: We'll be installing some software that needs to be built so we'll need to get packages required for compiling. In one swoop, you can type this command and get everything you need:
sudo apt-get install build-essential
Step 3: Once you've got the tools, it's time to grab MySQL and Ruby. Just copy and paste this command into your terminal if you're in a hurry.
sudo apt-get install ruby ri rdoc mysql-server libmysql-ruby ruby1.8-dev irb1.8 libdbd-mysql-perl libdbi-perl libmysql-ruby1.8 libmysqlclient15off libnet-daemon-perl libplrpc-perl libreadline-ruby1.8 libruby1.8 mysql-client-5.0 mysql-common mysql-server-5.0 rdoc1.8 ri1.8 ruby1.8 irb libopenssl-ruby libopenssl-ruby1.8 psmisc
If you hadn't previously installed MySQL you'll be asked to set a root password. Don't forget what you choose!
Step 4: Grab the latest ruby gems and install them. As always be sure to check rubyforge.org to make sure you're grabbing the latest one. As of this writing it's 1.1.1 but it never hurts to check.
wget http://rubyforge.org/frs/download.php/35283/rubygems-1.1.1.tgz tar xvzf rubygems-1.1.1.tgz cd rubygems-1.1.1 sudo ruby setup.rb
Once it's done you can remove the .tgz file and erase the rubygems-1.1.1 directory too.
Step 5: Make symlinks. Just in case the symlinks did not get created for you (the symlink for gem was missing for me so don't assume) go ahead and create them with these commands:
sudo ln -s /usr/bin/gem1.8 /usr/local/bin/gem sudo ln -s /usr/bin/ruby1.8 /usr/local/bin/ruby sudo ln -s /usr/bin/rdoc1.8 /usr/local/bin/rdoc sudo ln -s /usr/bin/ri1.8 /usr/local/bin/ri sudo ln -s /usr/bin/irb1.8 /usr/local/bin/irb
Step 6: Install ruby on rails!! I installed this on a minimal Slicehost install and it took forever because of the limited ram. I'm going to suggest you run the command and skip the rdoc and ri installs unless you really want them. (in which case the below command is simply sudo gem install rails)
sudo gem install rails --no-rdoc --no-ri
Step 7: Let's install mongrel now that we've got ruby gems and rails working. If you've done this before you'll be happy to see that it's getting easier as rubygems is now grabbing dependencies and also checking your architecture.
sudo gem install mongrel --no-rdoc --no-ri
At this point you have a complete rails stack ready for local development on Hardy, Sidux, or Debian. If you want to get things going for a server (install nginx, mongrel cluster) then read on. Otherwise you're done, so happy developing!
Step 8: The first thing in preparing to deploy this sucker is to grab a real web server. I'm still using Nginx.. haven't been disappointed with performance in the slightest.
sudo apt-get install libpcre3 nginx libfcgi-dev libfcgi-ruby1.8 libfcgi0ldbl
When it asks you what webserver to pre-configure leave everything blank. We're using nginx not apache this time. (Apache 2 with Ruby on Rails for Gutsy is covered here.)
Step 9: Let's install and configure mongrel cluster. This is a lengthy set of tasks each of which is important so please pay special attention and seek help in your favorite forums if you aren't familiar with linux. For new users: tab-autocomplete is your friend.
sudo gem install mongrel_cluster --no-rdoc --no-riCopy the init file over to /etc/init.d/ by typing this all on one line:
sudo cp /usr/lib/ruby/gems/1.8/gems/mongrel_cluster-1.0.5/resources/mongrel_cluster /etc/init.d/mongrel_cluster
You may need to change the above command if a version other than mongrel_cluster-1.0.5 was installed.
Next, add a path statement to mongrel_cluster file just above the CONF_DIR variable:
sudo vi /etc/init.d/mongrel_cluster PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local:/usr/local/sbin:/usr/local/binYou may also want to change the USER=mongrel to USER=www-data but I'll leave that up to you.
Let's modify permissions and make sure we boot mongrel on startup:
sudo chmod +x /etc/init.d/mongrel_cluster sudo update-rc.d mongrel_cluster defaults
We're not quite done yet -- we need to setup the mongrel cluster correctly [source]. This assumes your primary rails app is in /var/www/myrailsapp. Modify the below line according to your setup.
sudo mongrel_rails cluster::configure -e production \ -p 8000 -N 3 -c /var/www/myrailsapp -a 127.0.0.1 \ --user www-data --group www-data
Now let's create a symlink to that file from within /etc where all our configs live:
sudo mkdir /etc/mongrel_cluster cd /etc/mongrel_cluster/ sudo ln -s /var/www/myrailsapp/config/mongrel_cluster.yml
You can download a sample mongrel_cluster file HERE. Regardless, I think it's a good idea to download it and compare it to what the above command produced.
Step 10: We're *almost* done. Next step is to configure Nginx. Here's a sample nginx.conf file for your /etc/nginx/ folder. It's set up to handle one rails app and phpmyadmin. Adding additional servers just means more server blocks. I strongly suggest backing up your original nginx.conf file before copying this over.
The last step is to turn everything off and then turn it on again:
Mongrel (stop): sudo /etc/init.d/mongrel_cluster stopMongrel (start): sudo /etc/init.d/mongrel_cluster start
Nginx (stop):sudo /etc/init.d/nginx stop
Nginx (start): sudo /etc/init.d/nginx start
Bonus: Right now you're ready to go, but you should consider installing the mysql gem as well to boost performance. Do that by grabbing the headers and installing the gem.
sudo apt-get install libmysqlclient-dev sudo gem install mysql --no-rdoc --no-ri
Troubleshooting: If your rails app fails to start:
- Make sure your database settings are correct in your config/database.yml file. Especially look at the socket and mysql password.
- Be sure that your rails app has the correct permissions. You can do that by running sudo chown -R www-data:www-data /var/www/railsapp This command may change depending on how you set up nginx and mongrel!
- Check to see all the gems you need for your app are installed
- Check to see if your app starts in development mode with a simple sudo ruby script/server. If not, check the output message
- Always check your production log. If you don't have one, create it using sudo touch production.log and set it using sudo chmod 0666 production.log
That should be it. I always seem to miss some small detail or another so feel free to leave a comment if I overlooked or skipped a step. Happy coding!
FYI:
sudo ln -s /usr/bin/gem 1.8 /usr/local/bin/gem errors but sudo ln -s /usr/bin/gem1.8 /usr/local/bin/gem worked
(removed space between “gem” and “1.8”
Kyle
Fixed, thanks!
vince
At step 6. “sudo gem install rails” I get the message “ERROR: could not find rails locally or in a repository”
What now?
Korat
Hi Korat,
That’s very strange.. you should try running the command again – there used to be a problem with older versions of gem where that was known to happen.
Try running it again and also post the output of gem -v
vince
For step 6 “sudo apt-get install rails” works.
Korat
Hi! I just want to leave a word of thanks here. This website has the best and most updated info for VPS how-tos. Thanks!
Zan
BAM! Up and running in less than 10 minutes… all thanks to your write up.
Thank you sir! (ps i really love the layout of your blog)
Zigga What! Zigga Who?
Great article, Looks like an excellent solution.
g.zhen.ning
You are the egg man.. you are the walrus. Thanks :-)
Jesse
In step 9 at the command that starts with “sudo mongrel_rails cluster::configure” I got lost.
Where is my primary rails app going to be found? I know it’s not in the file written there.
And what else in that command am I going to need to change?
(sorry if it seems like a stupid question, but I’m rather new to linux)
Bean
@Bean, the full command has the path t our rails app in it (/var/www/myrailsapp).
There’s also a sample file you could/should download, modify, and copy into the config folder of your rails app. (find it at the end of step 9..)
vince
Everytime I run this command from step 9:
sudo mongrel_rails cluster::configure -e production \ -p 8000 -N 3 -c /var/www/myrailsapp -a 127.0.0.1 \ –user www-data –group www-data
I get this result:
!!! Path to config file not valid: config/mongrel_cluster.yml cluster::configure reported an error. Use mongrel_rails cluster::configure -h to get help.
Tried everything I can think of. Any ideas?
jonathan
@jonathan: haven’t had time to investigate your issue, but as a work-around I’d suggest downloading the sample file and modifying it. (make sure you also set correct permissions via chown www-data:www-data mongrel_cluster.yml)
vince
I have copied the nginx.conf file and everything starts up nicely but I keep getting the “It works!” default page either in phpmyadmin or my rails app. Oddly enough it grabs the favicon.ico but not loading my rails app or index.php for phpmyadmin. Is there something I need to be doing with /etc/nginx/sites-available/default
??
seb
hrm.. seems I have apache installed, might just explain it..
how did you install phpmyadmin without apache? sorry, a neophyte trying my best :S
seb
@jonathan Oddly enough I had this issue too when I wasn’t physically in my rails app directory.. though this should not be the case, it is what happened to me
seb
excelente tutorial, me ha servido de mucha ayuda. gracias
william betancur
@everyone: Thanks for the kind words! I’m happy to be able to contribute back in some way. :)
@seb, there’s another tutorial on the site for gutsy with apache (link in this article) and also gutsy w/nginx for rails and PHP. It’s all in the order you install the pacakges in. If you do it in the right order the webserver requirement for phpmyadmin should be satisfied which will prevent debian/ubuntu from grabbing apache.
vince