After my recent fiddling with caching and a little preliminary reading for an interesting side project I’m considering, I decided to install memcached. As will all things on linux, its really easy to install… unless its not. Every time I try to get one of these things going it always takes a while to figure out what packages are needed so the gem can function. So here they are:
sudo aptitude install libmemcached-dev libsasl2-dev libmemcached-dbg
I’m not sure that you need the debug libraries in there but its better to have them and not need them, than need them and not have them. Right?
Now you are ready to install the gem :
sudo gem install memcached –no-rdoc –no-ri
If you miss that libsasl2 library like I did the first time, all manner of badness happens as you can see below. But now you know how to avoid it.
ERROR: Error installing memcached:
ERROR: Failed to build gem native extension./usr/bin/ruby1.8 extconf.rb
Libmemcached already built; run ‘rake clean’ first if you need to rebuild.
creating Makefilemake
gcc -I. -I/usr/lib/ruby/1.8/x86_64-linux -I/usr/lib/ruby/1.8/x86_64-linux -I. -fPIC -I/usr/lib/ruby/gems/1.8/gems/memcached-0.19.2/ext/include -L/usr/lib/ruby/gems/1.8/gems/memcached-0.19.2/ext/lib -fno-strict-aliasing -g -g -O2 -fPIC -fno-strict-aliasing -g -g -O2 -fPIC -c rlibmemcached_wrap.c
Just so you know, after completely screwing up my first install, and realising that the suggested “rake clean” in the memcached directory was failing as well, I sorted it out by deleting the entire /usr/lib/ruby/gems/1.8/gems/memcached-0.19.2 directory. Very ugly. Doing it right the first time is recommended.
So now that its installed, lets test it to see that everything is ok using the example from the README:
mike@sleepycat:~$ memcached -p 11211 &
[1] 15778
mike@sleepycat:~$ irb
irb(main):001:0> require ‘rubygems’
=> true
irb(main):002:0> require ‘memcached’
=> true
irb(main):003:0> $cache = Memcached.new(“localhost:11211″)
=> #<Memcached:0x7feb57cf8b50 @not_found=#<Memcached::NotFound: Memcached::NotFound>, @options={:no_block=>false, :logger=>nil, :ketama_weighted=>true, :retry_timeout=>30, :timeout=>0.25, :auto_eject_hosts=>true, :support_cas=>false, :show_backtraces=>false, :rcv_timeout=>0.25, :buffer_requests=>false, :credentials=>nil, :prefix_delimiter=>””, :sort_hosts=>false, :server_failure_limit=>2, :verify_key=>true, :distribution=>:consistent_ketama, :tcp_nodelay=>false, :hash=>:fnv1_32, :default_ttl=>604800, :use_udp=>false, :cache_lookups=>true, :poll_timeout=>0.25, :default_weight=>8, :binary_protocol=>false, :connect_timeout=>4, :hash_with_prefix_key=>true}, @default_ttl=604800, @servers=[“localhost:11211:8”], @not_stored=#<Memcached::NotStored: Memcached::NotStored>, @struct=#<Rlibmemcached::MemcachedSt:0x7feb57cf8ad8>>
irb(main):004:0> value = ‘hello’
=> “hello”
irb(main):005:0> $cache.set ‘test’, value
=> nil
irb(main):006:0> $cache.get ‘test’
=> “hello”
irb(main):007:0>





the name that will be given to the port forwarding configuration that these commands set up.

