I attended a local Ruby Meetup last week where they showed how to get all the dependencies for a smart RoR setup on Ubuntu. I’ve only tested it on 12.04 myself, but something similar should work for 10.04. I have doubts whether installing on 8.04 is this easy for the current versions of RVM, Ruby and Rails.
The installation instructions came from a gist from Cajun_Code. Following those instructions makes this process fairly straight forward. We’ll install RVM, Ruby v1.9.3, Rails, SQLite3, nginx and thin to get started.
What is a Full RoR Environment?
- RVM – latest stable
- Ruby – latest stable
- Rails – latest stable
- nginx – load balancer – latest stable
- thin – application server – distro version
It is important to install RVM before installing Ruby or Rails. RVM is Ruby Version Manager and lets us have multiple complete Ruby and Rails versions on the same machine. It also leaves the operating system ruby alone, so that OS tools like Puppet that are based on the OS Ruby version continue to work as expected for the system administrator. This is very important to have a stable system AND maintainable development environments that don’t clash.
With the latest RVM, we can install multiple different ruby and rails environments.
Git is the most popular DVCS. If you do any software development, becoming familar with git is a good idea, even if your company uses SVN or CVS. Git has interfaces to both cvs and SVN, so once you learn git, you can just point to those other repositories and keep working as you already know. Ok, it isn’t quite that simple since git has more capabilities, but for day to day stuff, it really is that simple.
When you know which dependencies are required before starting everything seems easier. Install these:
sudo apt-get update;
sudo apt-get install curl bison build-essential autoconf zlib1g-dev libssl-dev libxml2-dev libreadline6-dev git;
curl -L get.rvm.io | bash -s stable
This will create an RVM environment under ~/.rvm/.
The last command installs RVM under your userID by default. If you use the following command with sudo, it will be installed for everyone sharing the machine in a central location, /usr/local/rvm/. The downside is that it will need root equiv to manage going forward, which might not be desired.
curl -L get.rvm.io | sudo bash -s stable
Regardless of which command you use, follow the further installation instructions on the screen. These help pointers were provided during the installation.
The next item was a reminder to run
to setup environment settings. This needs to be done in any open terminals. Any new terminals on the machine wll get the new settings automatically.
It is a good idea to review the notes for your specific rvm install.
Further, there is another command to ensure your system meets the requirements for rvm.
On my system, the requirments had many more dependencies for Ubuntu like subversion and ncurses-dev. I installed them before moving further, just to be safe.
Install Stable Ruby
Using rvm list known, I saw that Ruby 1.9.3 is the current latest stable release. That’s what we should install unless we have a specific need for some other Ruby version.
rvm install 1.9.3
This will take a while on most systems. The download will be quick. It is the later steps that will take some time. You can add a -j 2 to let your compiler use more CPU cores. Just change the number to match the number of CPUs on your machine.
On my virtual machine with 1 Core2Extreme core allocated, it took about 15 minutes.
Install Stable Rails
Next we want to install Rails3+, but we want to use a gemset so we can switch different versions of gems easier.
rvm gemset create rails3
will create a gemset called “rails3”. We will be careful to reference this specific tag for our figure gem installs, like rails.
rvm use 1.9.3@rails3 gem install rails gem install sqlite3
This installed rails-3.2.6 and sqlite3 (1.3.6).
At this point you should be ready to start programming in Ruby on Rails, but we aren’t ready to deploy any small apps. Using the development environment for deployments is a really, really bad idea.
Web Server, Reverse Proxy and Load Balancing
For development, the built-in web server is probably just fine.
For production, using a ruby on rails friendly server called thin with nginx running as a reverse proxy/load balancer is a good idea. I find the performance and flexibility of this configuration fantastic.
Installation for both are trivial, but the configuration for nginx is non-trivial. For any web server that is constantly under attack, using the latest available stable version from the developer is a good idea, not the older ( sometimes extremely older ) package manager version.
- Add the nginx PPA
- Update the repository lists
- install nginx and thin
sudo add-apt-repository ppa:nginx/stable sudo apt-get update sudo apt-get install nginx gem install thin
We installed thin using gem. We’ll need to maintain it using gem going forward.
Nginx provides some fantastic anti-hog capabilities plus it can be configured to prevent external sites from borrowing your image files by forcing the referrer to be only your servers and can easily redirect different clients to different versions of your app as needed. Nginx has many other useful capabilities.
Nginx configuration is beyond this article, but doesn’t have to be all that complicated. This article will explain a little more and this article explains how to restrict access to certain pages from the outside world using nginx.
Thin is the application server for our ruby apps. I like to run 2-3 app servers and let nginx manage connections to each server based on responsiveness. Nginx will recognize if a server fails to respond and will redirect traffic to other, faster servers. These servers can be running anywhere on your network or on the internet, but all traffic from those servers will be redirected through the nginx server back to the requesting client.
About a year ago, this little blog was hit with much more traffic than our network could stand. Here’s the discussion about that and our updates to improve performance using Thin and Nginx.
Here’s the current blog systems architecture, but we only have 2 app-servers running.
Testing showed that running more actually slowed down the responses.
If you’ve followed along, you should have rvm, ruby, rails, thin, nginx installed and be able to run multiple versions of ruby and rails on the same system.