Monday, May 6, 2013

How To Install Ruby 2.0 & Rails 4.0 on Ubuntu Server 12.04

0. Start with Ubuntu Server LTS 12.04.2

ubuntu-12.04.2-server-i386.iso, running in VMware Player, with OpenSSH server installed during ubuntu setup.

1. Use rbenv.

The packaged ruby available via apt-get is not 2.0, so use rbenv instead (and use rbenv in general, anyway). Follow the installation instructions for ubuntu here: https://github.com/sstephenson/rbenv. Of course you'll need git in order to complete that:
sudo apt-get install git

You should be able to make it through the installation of ruby-build as a rbenv plugin, but then your actual attempt to install ruby will probably fail.

2. Install other libraries to fix the build & run problems.

Here's the basic workflow:
rbenv install --list
rbenv install 2.0.0-p0
Fail. Fix. Repeat.

The "rbenv install 2.0.0-p0" takes some time to download the ruby source first, then starts to build it. This only addresses problems during the build phase, not download problems.

2.1. configure: error: no acceptable C compiler found in $PATH

The basic Ubuntu Server does not come with many development necessities. But there's a wonderful package to get you started:
sudo apt-get install build-essential

2.2. The Ruby openssl extension was not compiled. Missing the OpenSSL lib?

Even though you have openssl on your server, you don't have the development libraries needed to build on top of it. So:

sudo apt-get install libssl-dev


However, when you get this error, you can see some other things in the log as well:

...
Failed to configure openssl. It will not be installed.
...
Failed to configure readline. It will not be installed.
...
Failed to configure zlib. It will not be installed.
...

There are several other "not installed" lines as well, but most of them are windows-related and I don't care about them. libssl-dev will actually install zlib1g-dev for you, which seems to take care of the zlib issue.

After installing libssl-dev, you'll be able to successfully install ruby. However, when you install rails, and then you try to do "rails console", you'll discover that it throws an exception due to readline:
.../completion.rb:in `require': cannot load such file -- readline (LoadError)
So you'll want to fix the readline issue as well:
sudo apt-get install libreadline-dev

Now you can do
rbenv install 2.0.0-p0
and your ruby should be ready for rails.

3. Install Rails Gems.

First, start using your new ruby.
rbenv rehash
rbenv local 2.0.0-p0

Aside: At this point, I like to tell the gem installer to ignore building the documentation. To do that, put the following in your ~/.gemrc file:
install: --no-rdoc --no-ri 
update:  --no-rdoc --no-ri


Now, install bundler and rails.
gem install bundler
gem install rails --version 4.0.0.rc1
... and don't forget to relink the shims:
rbenv rehash
At this point, you should be able to use the "rails" command.

4. Create a new Rails app.

Depending on your database requirements, you may need to install various database libraries or gems. However, you should also be able to create a new Rails app without using ActiveRecord with no problem.

4.1. Install nodejs.

You can create a rails app without any backing database by skipping active record dependencies.
rails new myapp -O
But even if you do that, you'll still run into an error:
cd myapp
rails console
.../execjs/runtimes.rb:51:in `autodetect': Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)
So now you should install nodejs:
sudo apt-get install nodejs
rails console
Success!

4.2 With Rails' default (SQLite)

The following command fails:
rails new myapp
with errors:

Installing sqlite3 (1.3.7)
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.
...
checking for sqlite3.h... no
sqlite3.h is missing. Try 'port install sqlite3 +universal'
or 'yum install sqlite-devel' and check your shared library search path (the
location where your sqlite3 shared library is located).
...
An error occurred while installing sqlite3 (1.3.7), and Bundler cannot continue.
Make sure that `gem install sqlite3 -v '1.3.7'` succeeds before bundling.

You can fix this via:

sudo apt-get install libsqlite3-dev
rm -r myapp
rails new myapp
cd myapp
rails console
Success!


No comments:

Post a Comment