Fix for Rails 3.x.x error “[FATAL] failed to allocate memory” in OS X Lion

This error happens when mysql2 gem is either installed for some other version of mysql or when it cannot find required dynamic library. To fix this error, uninstall mysql2 gem and install as follows

$ env ARCHFLAGS="-arch x86_64" gem install mysql2 -v='0.3.11' -- --with-mysql-dir=/usr/local/mysql --with-mysql-lib=/usr/local/mysql/lib --with-mysql-include=/usr/local/mysql/include --with-mysql-config=/usr/local/mysql/bin/mysql_config

Please change ‘/usr/local/mysql’ to your mysql installation path. Thank you.

Set up Rails 3.2.2 with Passenger, rvm and ngnix

Ruby on Rails is becoming more feature rich and powerful by every release. Naturally the steps to get it working in production environment are also being changed. I’ve been trying to set up Rails 3.2.2 for a while and here’s the method that finally worked. This method should work for the new Rails release, 3.2.3 too.

Install Server Operating System

We have a large pool of operating systems to select for our server. Though Debian Squeeze and CentOS have proved their stability in serving rails applications, I would prefer a LTS version of Ubuntu Server edition. I did a small survey, Many experienced programmers said they are using Ubuntu server as it’s easy to maintain, Packages are in plenty and has better stability. Also Canonical promises to give us 5 years of support for LTS edition operating systems. Current LTS edition is Ubuntu Server 10.04

Install RVM

Ruby Version Manager(RVM) helps to manage multiple ruby versions in a single machine. RVM helps us to quickly switch Ruby/Rails versions. Before installing RVM, we need to install git, curl and autoconf. Use the following command to do it:

$ sudo apt-get -y install git curl autoconf

Then install and configure RVM,


$ bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)

$ echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function' >> ~/.bash_profile

Source ~/.bash_profile to add rvm as a function to shell

$ source ~/.bash_profile

Install Ruby

Before installing Ruby, we need to install all dependency packages. The following command lists the dependency packages.

$ rvm requirements 

We can see a line with lots of package names, something like the following. Execute it directly with sudo in shell to install packages:


sudo /usr/bin/apt-get install build-essential openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison subversion 

Next step is to install Ruby. Current latest version of Ruby 1.9.3. Let’s install it

$ rvm install 1.9.3

It will take a few minutes to fetch source, configure and compile.

After completing installation, we can set 1.9.3 as the default version:

$ rvm use 1.9.3 --default

Install passenger

Passenger is a free module for Apache and nginx to run Ruby applications. Luckily it’s available as a Ruby gem and it’s easy to install and configure it.

Install passenger gem

$ gem install passenger

Install and configure nginx

$ rvmsudo passenger-install-nginx-module

This command downloads nginx source code, builds it and finally configures passenger for us. Default location of ngnix is /opt/nginx and we can find the configuration in /opt/nginx/conf/nginx.conf

If you open nginx configuration, you can see the following lines have been already added into it in ‘http’ configuration:


passenger_root /home/ershad/.rvm/gems/ruby-1.9.3-p125/gems/passenger-3.0.11;
passenger_ruby /home/ershad/.rvm/wrappers/ruby-1.9.3-p125/ruby;

Install Rails

Before installing rails, we will create a gemset. Gemset is feature of RVM where we can create multiple gemset to store different gems of different versions.

Let’s create a gemset for Rails 3.2.2


$ rvm gemset create rails322

$ rvm gemset use rails322

Installing rails is pretty straight forward.

$ gem install rails -v 3.2.2

If you are not interested in the documentation that comes along with rails gem, use the following command instead

$ gem install rails -v 3.2.2  --no-ri --no-rdoc 

Install nginx init script

nginx init script by Jason Giedymin helps us to administer web server easily.

$ cd
$ git clone git://github.com/jnstq/rails-nginx-passenger-ubuntu.git
$ sudo mv rails-nginx-passenger-ubuntu/nginx/nginx /etc/init.d/nginx
$ sudo chown root:root /etc/init.d/nginx

Deploying application
Let’s store all rails applications under /var/rails_apps/ directory. Let’s make such a folder

$ sudo mkdir -p /var/rails_apps
$ sudo chmod 777 /var/rails_apps/ #giving full file permissions

Let’s create a sample rails application in rails_apps directory

$ cd /var/rails_apps
$ rails new helloworld
$ cd helloworld
$ vim Gemfile # and uncomment the line to include 'therubyracer' gem. We need a javascript runtime
$ bundle install
$ bundle exec rake assets:precompile #Precompile assets to public/ dir

Next step is point ngnix to this location. Add the following snippet in /opt/nginx/conf/nginx.conf


server {
listen 80;
server_name example.com;
rails_env production;
root /var/rails_apps/helloworld/public; # <--- be sure to point to 'public'!
passenger_enabled on;
}

Restart the server

 sudo /etc/init.d/nginx restart

Your application must be alive and running now! :)

Points to remember

1) Rails application doesn’t get updated when we change the code. This is because we need to restart passenger explicitly

Restarting passenger is easy, we just have to create a file ‘restart.txt’ in tmp/ dir of the application. For example


$ cd /var/rails_apps/helloworld

$ touch tmp/restart.txt

2) Always precompile assets after generating controller or scaffold

3) Make sure you are migrating in ‘production’ environment. This can be done using the following command.

 rake db:migrate RAILS_ENV=production

4) When you get errors related to routes, check the list of all routes

 $ rake routes 

5) When something goes wrong, see log/production.log

6) If you happen to get passenger errors related to missing gems, just add those gems in Gemfile and use the following command

 $ bundle install --path vendor/bundle 

That’s it. Happy hacking with Ruby on Rails. Thank you.


				

Python script for automatic synchronisation of ‘Read it later’ list and webpage

Adding to 'Read it later' list

One of the powerful features of firefox is that it has thousands of useful addons. We are going to deal with such an addon – Read it later. It allows us to book mark page to read later with just a click. I was happy living with it.

But on one fine day, Hrishiettan told me about sharing the links we read and its advantages. Sharing links will help many people to find interesting articles easily and it will also act as a link-index in case if we want to refer it again. The idea is really good and he told he would start working on a php app.

On another day, I told about this to Raghesh sir. He told there’s an option to export our list to html in ‘Read it later’. That’s it! They have an API too! Got the API key from their website, cleared all existing links as they contain irrelevant news links, and wrote following script. It now updates the links in http://ershadk.com/links/ every 5 minutes automatically from my ‘Read it later’ list.  Thank you :)

ps: The standard way of xml parsing didn’t work, that’s why I wrote it rude :)

 

A python script to generate SQL queries for importing links/blogroll in another wordpress blog

Here’s the problem: There are 30 precious links in my old blog under blogroll. Exporting feature of wordpress does not export the contents in blogroll/links. The task is to import those links in this new blog with minimum effort.

Well, Adding 30 links manually ain’t difficult – it may take a maximum of 15 minutes to complete the task. But it’s too repetitive and boring. The best way is obviously writing a script.

Here’s a small python script that generates SQL queries to insert old blogroll items in new database. It will ask for old blog URL and new database name – that’s it, it will search your old blog for blogroll links and print INSERT statements in stdout suitable for executing in phpmyadmin or directly in mysql shell :)

Hope you like it, Thank you :)

New home!

It was one of my dreams to have an own domain. With god’s grace, It’s cherished now.

Thank a lot to Manuettan and his firm Coolwrks for kindly hosting this website. If he hadn’t provided me free hosting, I wouldn’t have even registered this domain :)

Please update your feed /email subscriptions from old blog to this one. Thank you :)

Tathva memories @ NITC

“.. I’m leaving this campus today. I will miss the late-night hangouts with Kartik and Pankaj.. I will miss the stay in Kartik’s room with Arjun and Rohit (If you are reading this, All the best for your IAS, brother). I will miss Ramnath, Arnab and all my  friends here and this great institution.. I’m sure I will be back here for the next event.. With a million beautiful memories in 4 days.. Adieu NITC..”

Tags:

Anagram Solver in C

Hi, Here’s a small anagram solver written in C. It works based on generating permutations of given string and comparing them using a dictionary. For fast search in dictionary, the program first creates an index of the starting location of each alphabet. It takes a few minutes to process long words.

Here’s the source code. Suggestions are always welcome, Greetings :)

Update: There’s a little problem in using the term ‘anagram solver’ here. An anagram solver finds words from another valid word – But this program finds words from the characters of given valid/invalid word.  Thanks to technikhil for the info :)

Note: Realized that this implementation of anagram solver is inefficient. Will post a better one soon.

Why I started loving Archlinux

We like simplicity. We like everything new.

It’s been some years since I switched completely to GNU/Linux. I always wondered at its stability, security, features, community and at the people who use it. GNU/Linux helped me to have exciting computing experience all the time filled with wonders and fun. I forgot about the existence of Antivirus software. Forgot the time I feared autorun.inf and weird executable files. Forgot BSOD and EULA. Now, everything is open. Met many new people, similar interests bonded us. People who use GNU/Linux have always been very helpful. They share knowledge instead of pirated software. They teach us to fish. A kind of special relationship rooted between us, in a world connected by computer networks and its underlying complex abstractions. At this moment, I would like to thank everyone who made lives of people like me colourful.

I had been using Debian Squeeze for a while and was happy with that. Had its complete repo offline which helped me to install any package in spite of the very slow internet connection I had then. Though the things went smooth, I faced some issues during that time with the compilation of new software from source. Fixing dependency issues were pain. The new source of package requires new version of dependencies which were not available in repo. Compiling them from source always created new pains. Updating software/repo was not possible because of the slow speed of internet. Taking laptop to college and updating from there also didn’t work due to various technical and non-technical reasons. I felt like I was completely forbidden from trying new versions of software. I was in a kind of lock then. Suddenly, one fine day, I got a broadband internet connection!

I don’t have words to express how much happiness I felt. Now the scenario has completely changed. I can now change the Debian repo to testing or sid, try new packages, configure backports or update what ever I want. But, I thought it was time for a change. Changes are the spices of life, indeed.

I wanted to install another distro from the mainstream. First, I thought of Fedora 15 which is a very good OS and hacks by Rajeeshettan in it always inspired me. Also I had used Fedora 14 for several months. But in Fedora too, I missed an element of simplicity and playfulness. The point with these type of operating system is that it comes with all basic packages preconfigured and giving us less opportunities to play with those.

Next choice was obviously Archlinux. I have been hearing about it since I met my another GNU/Linux guru – Ashikettan. He blogs about Arch much and talks about it often. Also, I had tried it once months before when I had speedy internet connection. The best thing about Archlinx is that it is a rolling distro, and new packages are always readily available!

To install, I got the 64bit iso image from Arch website and made thumb bootable with abock. The installation went smooth and had a nice time configuring various things along with my favourite desktop environment – KDE. I was not really able to use KDE since the version in Debian Squeeze was a bit buggy and it crashed often. I was using Gnome 2.x then. Now, I’m really happy with KDE 4.7.0 with a distro which can be updated easily using its sweet package manager – pacman. Arch also has a wonderful knowledge base – The ArchWiki. ArchWiki has many tutorials to play with for beginners and for expert users.

To talk about packages in Archlinux, I don’t have words! I remember trying to install new version of Ipython in debian which comes with a new tool called qtconsole. Qtconsole can show the outputs of matplotlib embedded inside the window instead of showing as a pop-up. It was a bit pain fixing various python modules that depended upon on other packages in debian. But in Archlinux, a simple ‘pacman -S ipython’ after a system upgrade with ‘pacman -Syu’ solved the issue. (Please note that though ‘pacman -Sy ipython’ alone installs new version of Ipython, it may lead to dependency issues sometimes. Always install with ‘-S’ and have an system upgrade with ‘-Syu’ often). Upgrading the system obviously updates kernel, now Arch has kernel 3.0 in repo!

Please do not have a feeling that I’m saying other distro/package managers are bad or less useful than Arch/pacman, I was simply having a comparison under special conditions – Indeed, every GNU/Linux thing is great and well developed!

Another great feature I found in Archlinux is the rc.conf file. We can easily configure various system wide things in /etc/rc.conf file, like which all kernel modules to load, which all daemons to run at start, hostname, timezone, network interfaces, gateways and so on. There’s also an application called ‘rc.d’ which is similar to ‘service’ in debian/ubuntu using which we can start various services like sshd, etc. Also, the support in IRC channel (#archlinux @ irc.freenode.net) and in Arch forums are very helpful.

Finally, as a student, I find Archlinux very useful and informative, Also it’s so much of fun using it. I humbly recommend this sweet GNU/Linux distro to everyone who likes to add more spice to their GNU/Linux computing. I just started with Archlinux and hope I could post more about it in future. Thank you so much for reading. Greetings!

It was really a wonderful experience at MES :)

Yesterday, I went to MES College of Engineering to deliver a small talk on programming and free software, it was entirely a new experience for me. A bigger crowd than expected was present. It went fine :) Thanks to Raghesh sir, Raju sir, S@IT and to friends there :)

I’m happy to share my little slides. It was created according to numerous suggestions by Raghesh sir. For fun, the presentation was started with a SMS trigger from audience :) The code for that is like ‘get-thing-done’ and doesn’t have the quality to upload. It’s developed using python with gammu module. Please feel free to tell me if you want to have a look at the code.

Attribution: ‘Input -> Fun -> Output’ is an idea got from Niyam Bhushan during his talk at NIT Calicut on FOSS Meet day.

Thank you :)

A cryptographic tool that converts weak password into a strong password before encrypting

Here’s a small encryption/decryption tool that works based on AES algorithm. No matter how weak your password is, the program converts it to a  32 digits hexadecimal number  before encrypting. This is achieved by finding the MD5 hash of the password using hashlib python module. This hash is used to encrypt the file.

The source file is here

The idea in my mind while writing this program was this:

Suppose a cracker gets an encrypted file and he figures out it’s encrypted using AES somehow. Then he starts brute force attack on it to find the key and extract information. If the password used to encrypt that file is weak and if it’s based on a dictionary word, the cracker can easily figure out the password. Hence the password  given by the user must be made stronger by the encrypting program. And the best way to make a password strong is by using  digest algorithms since it’s unique to a string.

What do you think? Am I wrong? Thank you :)

 

© Ershad Kunnakkadan
CyberChimps