Installing CouchDB on Ubuntu 8

Apache CouchDB is a very nice schema-free document-oriented database that I'm playing with lately.
Here's a quick guide to get CouchDB running on Ubuntu 8:

Prepare your environment
sudo aptitude install automake autoconf libtool subversion-tools help2man spidermonkey-bin build-essential erlang erlang-manpages libicu38 libicu-dev libreadline5-dev checkinstall libmozjs-dev wget

Create a couchdb user
sudo adduser --no-create-home --disabled-password --disabled-login couchdb

Download and build the latests version
wget http://mirrors.uol.com.br/pub/apache/incubator/couchdb/0.8.1-incubating/apache-couchdb-0.8.1-incubating.tar.gz

tar -xzvf apache-couchdb-0.8.1-incubating.tar.gz

./configure --bindir=/usr/bin --sbindir=/usr/sbin --localstatedir=/var --sysconfdir=/etc

make

Install it
sudo make install

sudo chown couchdb:couchdb -R /var/lib/couchdb /var/log/couchdb

sudo update-rc.d couchdb defaults

Start the service
sudo /etc/init.d/couchdb start

CouchDB will be running on port 5984. By default it listens only for connections from the local host. To change that edit /etc/couchdb/couch.ini and restart CouchDB.

Sample code (Ruby):

#!/usr/bin/env ruby
 
require 'rubygems'
require 'net/http'
require 'json'
 
module Couch
  class Server
    def initialize(host, port, options = nil)
      @host = host;
      @port = port;
      @options = options;
    end
 
    def delete(uri)
      request(Net::HTTP::Delete.new(uri))
    end
 
    def get(uri)
      request(Net::HTTP::Get.new(uri))
    end
 
    def put(uri, json)
      req = Net::HTTP::Put.new(uri)
      req["content-type"] = "application/json"
      req.body = json.to_json
      request(req)
    end
 
    def post(uri, json)
      req = Net::HTTP::Post.new(uri)
      req["content-type"] = "application/json"
      req.body = json.to_json
      request(req)
    end
 
    def request(req)
      res = Net::HTTP.start(@host, @port) {|http|
        http.request(req)
      }
      if (not res.kind_of?(Net::HTTPSuccess))
        handle_error(req, res)
      end
      res
    end
 
    private
 
    def handle_error(req, res)
      e = RuntimeError.new("#{res.code}:#{res.message}\nMETHOD:#{req.method}\nURI:#{req.path}\n#{res.body}")
      raise e
    end
  end
end
 
server = Couch::Server.new("localhost", "5984")
 
# Cleanup
server.delete("/helloworld/") rescue
 
# Database create
server.put("/helloworld/", "")
 
# Insert
server.put("/helloworld/en", {:txt => "Hello world"})
server.put("/helloworld/es", {:txt => "Hola mundo"})
server.put("/helloworld/pt", {:txt => "Olá mundo"})
 
# Select
puts server.get("/helloworld/en")
puts server.get("/helloworld/es")
puts server.get("/helloworld/pt")
Posted in Uncategorized | 1 Comment