install snippets

Installing Ultraviolet and Oniguruma

Tagged onigurama, ultraviolet, ruby, ubuntu, install, oniguruma  Languages bash

First install Oniguruma

Oniguruma is a regular expression engine that Ultraviolet uses to parse text; Ruby also uses Oniguruma by the way. If you don't have Oniguruma on your system you'll get this error while installing Ultraviolet (at least on Ubuntu Linux):

oregexp.c:2:23: error: oniguruma.h: No such file or directory

This tells you that you should download and install Oniguruma. For me version 5.8.0 was the only version that worked, so execute this command to get the right version of Oniguruma:

$ wget

You now have the source package on your computer, so decompress it with the following command:

$ tar zxvf onig-5.8.0.tar.gz

If everything went fine, change current directory:

$ cd onig-5.8.0/

Next, run configure:

$ ./configure

Watch the output closely and fix any errors reported, then run make:

$ make

To build and install Onigurama run:

$ sudo make install

I managed to get the following errors from Ultraviolet with other versions of Oniguruma, but these went away after installing 5.8.0 and re-installing Oniguruma:

Parsing error in // ==UserScript==: wrong number of arguments (2 for 0) #<Textpow::SyntaxNode:0xb7c91780>

Installing Ultraviolet and dependencies

Next install Ultraviolet with RubyGems:

$ sudo gem install -r ultraviolet --include-dependencies

Select which gem to install for your platform (i486-linux)
 1. oniguruma 1.1.0 (mswin32)
 2. oniguruma 1.1.0 (ruby)
 3. Skip this gem
 4. Cancel installation
> 2
Building native extensions.  This could take a while...
Successfully installed ultraviolet-0.10.0
Successfully installed textpow-0.9.0
Successfully installed oniguruma-1.1.0
Successfully installed plist-3.0.0

Test that Ultraviolet works by running the following code with irb:

$ irb

require 'rubygems'
require 'uv'
puts Uv.syntaxes.join( "\n" )
puts Uv.themes.join( "\n" )
input = <<HTML

puts Uv.parse( input, "xhtml", "html", true, "slush_poppies")


You might get this error:

require 'uv'
LoadError: cannot open shared object file: No such file or directory - /usr/lib/ruby/gems/1.8/gems/oniguruma-1.1.0/lib/

This message is a bit confusing. It means Ruby can't find, not as you could believe.

To fix this, check if the library has been linked:

$ ldconfig -p|grep libonig

If the library is not linked, add the path to the directory where the file is located to /etc/


include /etc/*.conf

Then run:

$ ldconfig

Another way of fixing this problem would be to tell the build script to install it to /usr/lib.

How to install the exception_logger Rails plugin and protect the logs with basic authentication

Tagged ruby, exception_logger, install, routes, rails, basic, authentication  Languages ruby

This snippet explains how to install and use the Rails exception_logger plugin. I'll also show you how to protect your logs by extending the plugin with basic authentication.

script/plugin source
script/plugin install exception_logger

I'm using Rails Edge on this project, so I had to install classic pagination also:

script/plugin install svn://

Next create and execute the migration file:

./script/generate exception_migration
rake db:migrate

Before starting the server we need to setup the routes:

map.exceptions '/logged_exceptions/:action/:id', :controller => 'logged_exceptions', :action => 'index', :id => nil

You also need to include the ExceptionLoggable in your ApplicationController:

class ApplicationController < ActionController::Base
  include ExceptionLoggable

Start your server and access the exception log at /logged_exceptions.

Exceptions can contain email addresses, passwords, credit card numbers, so you'll want to protect /logged_exceptions from the public. This can be done by adding the following code to the end of environment.rb:

config.after_initialize do
  require 'application' unless Object.const_defined?(:ApplicationController)
  LoggedExceptionsController.class_eval do
    before_filter :authenticate


    def authenticate
      authenticate_or_request_with_http_basic do |username, password|
        username == "foo" && password == "bar"

With this code we add a before filter that shows a login dialog to anyone trying to access /logged_exception/. Note that this requires Rails 2.0 basic authentication to work, so make sure you have the proper version installed.

Installing nginx on Debian

Tagged nginx, install, debian  Languages bash


Find latest version of nginx

Install a compiler otherwise: ./configure: error: C compiler gcc is not found

The command:

sudo apt-get install build-essential

Install pre-requisites otherwise you'll get:

Configuration summary + threads are not used + PCRE library is not found + OpenSSL library is not found + md5 library is not used + sha1 library is not used + zlib library is not found

The command:

sudo apt-get install libpcre3 libpcre3-dev libpcrecpp0 libssl-dev zlib1g-dev

Much better:

Configuration summary + threads are not used + using system PCRE library + using system OpenSSL library + md5 library is not used + sha1 library is not used + using system zlib library

Compile and install nginx

$ ./configure \
        --sbin-path=/usr/local/sbin \
        --conf-path=/etc/nginx/nginx.conf \
        --pid-path=/var/run/ \
        --error-log-path=/var/log/nginx/error.log \
        --http-log-path=/var/log/nginx/access.log \
        --with-http_ssl_module \
        --http-client-body-temp-path=/tmp/nginx_client \
        --http-proxy-temp-path=/tmp/nginx_proxy \
$ make
$ sudo make install

Run the install script

cd /usr/local/src


tar zxvf nginx-0.5.35

cd nginx-0.5.35

Create an nginx user and group

$ useradd -g www-data -d /var/www nginx

Create the web server directory

mkdir /var/www
chown root.www-data /var/www
chmod ug=rwx,o= /var/www

Test configuration

nginx -t
2008/03/09 20:51:05 [info] 5034#0: the configuration file /etc/nginx/nginx.conf syntax is ok
2008/03/09 20:51:05 [info] 5034#0: the configuration file /etc/nginx/nginx.conf was tested successfully

Start nginx


Installing Rails, mongrel and mongrel_cluster on Debian

Tagged rails, ruby, debian, install, sqlite3, mongrel, mongrel_cluster  Languages bash


Install RubyGems

tar zxvf rubygems-1.0.1.tgz

cd rubygems-1.0.1

ruby setup.rb

Install Rails

gem install rails

Install sqlite3 (optional)

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

Install mongrel and mongrel_cluster

$ gem install mongrel mongrel_cluster

$ mongrel_rails cluster::configure -e production \
  -p 8000 \
  -a \
  -N 3 \
  -c /var/www/xyz/current

$ mongrel_rails cluster::start

$ useradd -g www-data -d /var/www mongrel

Surviving reboots

sudo mkdir /etc/mongrel_cluster

sudo ln -s /var/www/xyz/config/mongrel_cluster.yml /etc/mongrel_cluster/xyz.yml

sudo cp /usr/local/lib/ruby/gems/1.8/gems/mongrel_cluster-1.0.5/resources/mongrel_cluster /etc/init.d/

sudo chmod +x /etc/init.d/mongrel_cluster

sudo /usr/sbin/update-rc.d -f mongrel_cluster defaults

mongrel_cluster_ctl status

Stale pids

If your mongrels crash or if you kill them, mongrel_cluster won't start your mongrels because mongrel_cluster believes the processes are still running, instead mongrel_cluster complains and does nothing:

** !!! PID file tmp/pids/ already exists.  Mongrel could be running already.  Check your log/mongrel.8000.log for errors.
** !!! Exiting with error.  You must stop mongrel and clear the .pid before I'll attempt a start.

To fix this simply add the --clean switch to the /usr/local/lib/ruby/gems/1.8/gems/mongrel_cluster-1.0.5/resources/mongrel_cluster startup script:

mongrel_cluster_ctl start -c $CONF_DIR --clean

How to install the stemmer4r gem on Mac OS X and Linux

Tagged stemming, stemmer4r, install, osx, linux, gem  Languages ruby

The stemmer4r gem is fubar. Warning draft snippet...

# gem install stemmer4r
Bulk updating Gem source index for:
Building native extensions.  This could take a while...
ERROR:  While executing gem ... (Gem::Installer::ExtensionBuildError)
    ERROR: Failed to build gem native extension.

ruby extconf.rb install stemmer4r

Gem files will remain installed in /usr/lib/ruby/gems/1.8/gems/stemmer4r-0.6 for inspection.
Results logged to /usr/lib/ruby/gems/1.8/gems/stemmer4r-0.6/ext/stemmer4r/gem_make.out

1. Change path of Ruby executable

cd /usr/lib/ruby/gems/1.8/gems/stemmer4r-0.6/ext/stemmer4r/
vim extconf.rb

#!/usr/bin/ruby -w


#ruby -w

2. Compile libstemmer_c

cd /usr/lib/ruby/gems/1.8/gems/stemmer4r-0.6/ext/stemmer4r/libstemmer/

3. Compile stemmer4r

cd /usr/lib/ruby/gems/1.8/gems/stemmer4r-0.6/ext/stemmer4r/

Change path:

Or wherever you have it installed

ruby extconf.rb

4. Build stemmer4r gem

gem build stemmer4r.gemspec

gem install stemmer4r-0.6.gem


gcc -shared -rdynamic -Wl,-export-dynamic   -L"/usr/lib" -o stemmer4r.o libstemmer_c/libstemmer.o  -lruby1.8  -lpthread -ldl -lcrypt -lm   -lc
/usr/bin/ld: libstemmer_c/libstemmer.o(libstemmer.o): relocation R_X86_64_32 against a local symbol' can not be used when making a shared object; recompile with -fPIC
libstemmer_c/libstemmer.o: could not read symbols: Bad value
collect2: ld returned 1 exit status
make: *** [] Error 1


[email protected]:/usr/lib/ruby/gems/1.8/gems/stemmer4r-0.6/ext/stemmer4r/libstemmer_c# make
include mkinc.mak
libstemmer.o: $(snowball_sources:.c=.o)
        $(AR) -cru [email protected] $^

How to install and use the Sphinx search engine and acts_as_sphinx plugin on Debian Etch

Tagged sphinx, search, acts_as_sphinx, debian, etch, rails, install, libstemmer  Languages bash

Inspiration for this snippet was taken from this post on the Sphinx forum, plus this blog post.

Compiling Sphinx

First install the prerequisites:

sudo aptitude install libmysql++-dev libmysqlclient15-dev checkinstall

Next download sphinx, libstemmer and install everything and the fish:

cd /usr/local/src

tar zxvf sphinx-0.9.9.tar.gz 

cd sphinx-0.9.9/

# Add stemming support for Swedish, Finnish and other fun languages.
tar zxvf libstemmer_c.tgz

./configure --with-libstemmer

make install

Configure Sphinx

Create a sphinx.conf file in your Rails config directory, as described here, or use this template.

Install acts_as_sphinx plugin

./script/plugin install

Add acts_as_sphinx to your model:

class Documents

Indexing content

rake sphinx:index

(in /var/www/
Sphinx 0.9.8-rc2 (r1234)
Copyright (c) 2001-2008, Andrew Aksyonoff

using config file './sphinx.conf'...
indexing index ''...
collected 5077 docs, 0.6 MB
sorted 0.1 Mhits, 100.0% done
total 5077 docs, 632096 bytes
total 0.160 sec, 3950427.25 bytes/sec, 31729.86 docs/sec

Reindexing content

sphinx:index shouldn't be run while the searchd process is running, so use rake sphinx:rotate instead, which restarts the searchd process after indexing.

Starting the daemon

mkdir -m 664 /var/log/sphinx
rake sphinx:start

(in /var/www/
Sphinx 0.9.8-rc2 (r1234)
Copyright (c) 2001-2008, Andrew Aksyonoff

using config file './sphinx.conf'...
Sphinx searchd server started.


Documents.find_with_sphinx 'why did I write this'

How to install and use the mysql-python library

Tagged python, mysql, mysql-python, install  Languages python

First download mysql-python from

Extract it and run:

python build
sudo python install

If you get this error you need to install python-dev package:

In file included from _mysql.c:29:
pymemcompat.h:10:20: error: Python.h: No such file or directory
_mysql.c:30:26: error: structmember.h: No such file or directory
In file included from /usr/include/mysql/mysql.h:44,
                 from _mysql.c:40:
_mysql.c:2808: warning: return type defaults to 'int'
_mysql.c: In function 'DL_EXPORT':
_mysql.c:2808: error: expected declaration specifiers before 'init_mysql'
_mysql.c:2886: error: expected '{' at end of input
error: command 'gcc' failed with exit status 1

Installing the python-dev package on Debian is done with apt-get or synaptic:

apt-get install python-dev

Installing the library should now work:

python build
python install

Next test the library in the python console:

import MySQLdb

# Note that this example uses UTF-8 encoding
conn = MySQLdb.connect(host='localhost', user='...', passwd='...', db='...', charset = "utf8", use_unicode = True)
cursor = conn.cursor()

cursor.execute ("SELECT * FROM cities")
rows = cursor.fetchall ()

for row in rows:
  print "%s, %s" % (row[0], row[1].encode('utf-8'))

print "Number of rows returned: %d" % cursor.rowcount

Don't forget to close the cursor and connection, and if you're inserting data commit before closing, because autocommit is disabled by default:

cursor.close ()
conn.commit ()
conn.close ()

For more information about MySQLdb see this article.

How to fix "fatal: 'gitosis-admin.git': unable to chdir or not a git archive"

Tagged git, fatal, gitosis, install, setup  Languages bash

Are the SSH keys valid?

When, not if, you get this error you most probably have the wrong keys listed in /home/git/.ssh/authorized_keys:

$ git  clone [email protected]:gitosis-admin.git 
Initialized empty Git repository in /Users/christian/Documents/Projects/gitosis-admin/.git/
fatal: 'gitosis-admin.git': unable to chdir or not a git archive
fatal: The remote end hung up unexpectedly

Try removing the keys in authorized_keys one by one, and verify that the names of the users match the ones in .gitosis.conf

Try using the full path to the repository

$ git clone [email protected]::/home/git/repositories/xxx.git

Check if the repository exists

$ cd /home/git/repositories/xxx.git
$ git clone /home/git/repositories/xxx.git

What does SSH say?

ssh -v [email protected]

Use Google

How to install memcached on OSX Snow Leopard

Tagged memcached, install, snowleopard  Languages bash

These instructions are from

cd /usr/local/src
curl -O
tar xzvf libevent-1.4.12-stable.tar.gz 
cd libevent-1.4.12-stable
make verify
sudo make install

cd /usr/local/src
curl -O
tar xzvf memcached-1.4.1.tar.gz 
 cd memcached-1.4.1
make test
sudo make install

memcached -d -P pidfile -l

How to install Nginx from source, Ruby Enterprise Edition, and Phusion Passenger

Tagged nginx, install, passenger, ree  Languages bash

Ruby Enterprise Edition:

cd /usr/local/src
wget t
tar zxvf ruby-enterprise-1.8.7-2009.10.tar.gz

ln -fs /opt/ruby-enterprise-1.8.7-2009.10 /opt/ruby-enterprise/


wget -P
tar -xzf nginx-0.7.63.tar.gz

Phusion Passenger:

gem install passenger
/opt/ruby-enterprise/bin/passenger-install-nginx-module --auto --nginx-source-dir=/tmp/nginx-0.7.63 --prefix=/opt/nginx --extra-configure-flags=--with-http_ssl_module