Tag Archives: ubuntu

sqlite3.h is missing – Ruby on Rails

Just an error I ran into while running bundle install on a new ubuntu virtual:

Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

/usr/bin/ruby1.8 extconf.rb 
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).
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

Provided configuration options:
	--with-opt-dir
	--without-opt-dir
	--with-opt-include
	--without-opt-include=${opt-dir}/include
	--with-opt-lib
	--without-opt-lib=${opt-dir}/lib
	--with-make-prog
	--without-make-prog
	--srcdir=.
	--curdir
	--ruby=/usr/bin/ruby1.8
	--with-sqlite3-dir
	--without-sqlite3-dir
	--with-sqlite3-include
	--without-sqlite3-include=${sqlite3-dir}/include
	--with-sqlite3-lib
	--without-sqlite3-lib=${sqlite3-dir}/lib
	--enable-local
	--disable-local

To fix it, simply run the following:

sudo apt-get install libsqlite3-dev

Could not find a JavaScript Runtime – Ruby on Rails

Ran into the following issue today starting a new app on a virtual Ubuntu box:

Could not find a JavaScript runtime

Thankfully there’s a quick fix, simply install nodejs:


chris@chris-VirtualBox:~/Desktop$ sudo apt-get install nodejs


Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  postgresql-client-8.4 postgresql-8.4
Use 'apt-get autoremove' to remove them.
The following extra packages will be installed:
  libc-ares2 libev4 libv8-3.1.8.22
The following NEW packages will be installed:
  libc-ares2 libev4 libv8-3.1.8.22 nodejs
0 upgraded, 4 newly installed, 0 to remove and 77 not upgraded.
Need to get 1,938 kB of archives.
After this operation, 6,066 kB of additional disk space will be used.
Do you want to continue [Y/n]? y
Get:1 http://au.archive.ubuntu.com/ubuntu/ oneiric/universe libv8-3.1.8.22 i386 3.1.8.22-1ubuntu2 [1,386 kB]
Get:2 http://au.archive.ubuntu.com/ubuntu/ oneiric/main libc-ares2 i386 1.7.4-1 [37.3 kB]        
Get:3 http://au.archive.ubuntu.com/ubuntu/ oneiric/universe libev4 i386 1:4.04-1 [29.5 kB]       
Get:4 http://au.archive.ubuntu.com/ubuntu/ oneiric/universe nodejs i386 0.4.9-1ubuntu3 [486 kB]  
Fetched 1,938 kB in 18s (106 kB/s)                                                               
Selecting previously deselected package libv8-3.1.8.22.
(Reading database ... 158699 files and directories currently installed.)
Unpacking libv8-3.1.8.22 (from .../libv8-3.1.8.22_3.1.8.22-1ubuntu2_i386.deb) ...
Selecting previously deselected package libc-ares2.
Unpacking libc-ares2 (from .../libc-ares2_1.7.4-1_i386.deb) ...
Selecting previously deselected package libev4.
Unpacking libev4 (from .../libev4_1%3a4.04-1_i386.deb) ...
Selecting previously deselected package nodejs.
Unpacking nodejs (from .../nodejs_0.4.9-1ubuntu3_i386.deb) ...
Processing triggers for doc-base ...
Processing 1 added doc-base file...
Registering documents with scrollkeeper...
Processing triggers for man-db ...
Setting up libv8-3.1.8.22 (3.1.8.22-1ubuntu2) ...
Setting up libc-ares2 (1.7.4-1) ...
Setting up libev4 (1:4.04-1) ...
Setting up nodejs (0.4.9-1ubuntu3) ...
update-alternatives: using /usr/bin/node to provide /usr/bin/js (js) in auto mode.
Processing triggers for libc-bin ...
ldconfig deferred processing now taking place
chris@chris-VirtualBox:~/Desktop$

Let me know if you have any issues.

ERROR: column “category” does not exist

I’ve recently switched from SQLite3 to PostGreSQL and came across the following error this morning:

my_app_development=> select category from codes;
ERROR:  column "category" does not exist
LINE 1: select category from codes;

The following post solved the issues in one sentence:

http://archives.postgresql.org/pgsql-novice/2007-01/msg00032.php

Yes. In postgres, unquoted column and table names are converted to lower case.

The user also added that when using PostGreSql you should either be always, or never using quotes around column names.

Creating Postcode Based Locations – Rails

My goal for today was to populate a record with all of the Australian postcodes and their corresponding suburb and state descriptions. To do this I had to complete the following:

  • 1: Download the postcode/location csv
  • 2: Create new model to contain locations
  • 3: Create a script to import a csv
  • 4: Run the script in order to populate the database
  • 5: Cleanse the data

Step #1: Download the .csv
I came across the following files after browsing a few of the Australian tech forums. I chose to use the Corra ones due to the claim that there are no license restrictions.

Once you’ve chosen your .csv, just save it in your project folder i.e. C:my_app

Step #2: Create a Model for your Locations
Secondly, you’ll need to create a new migration for your locations. I’ve simply called mine Code:

def change
      create_table :codes do |t|
        t.string "Pcode"
        t.string "Locality"
        t.string "State"
        t.string "Comments"
        t.string "DeliveryOffice"
        t.string "PresortIndicator"
        t.string "ParcelZone"
        t.string "BSPnumber"
        t.string "BSPname"
        t.string "Category"
        t.timestamps
     end
end

Step #3: Create a Script to Import the .csv
The third step is to create a script that allows you to import the csv. This is made simple thanks to a comment left by “Gianluca” on the following blog post:
http://erikonrails.snowedin.net/?p=212

Simply create a new file within your tasks folder and add the following code:

#my_app/lib/tasks/import.rake

require "csv"
desc "Import CSV file into an Active Record table"
task :csv_model_import, [:filename, :model] => :environment do |task,args|
  firstline=0
  keys = {}

  CSV.foreach(args[:filename]) do |row|
    if (firstline==0)
      keys = row
      firstline=1
      next
    end

    params = {}

    keys.each_with_index do |key,i|
      params[key] = row[i]
    end

    Module.const_get(args[:model]).create(params)
  end
end

Step #4: Run the script in order to populate the database
To run the script simply run the following command:

chris@chris-VirtualBox:~/my_app$ rake csv_model_import[codes.csv,Code]

Step #5: Cleanse the data
Cleansing the data is a little tedious, however one tip is to remove all locations that do not have a category value of “Delivery Area”.

Ahwell, that’s all I’ve got for now – let me know if you have any trouble.

Connect to Database – PostGreSQL

This is just a quick post on how to connect to particular database via the console in PostGreSQL. First, make sure you’ve logged in – just swap my_app for your role name:

chris@chris-VirtualBox:~/site$ psql postgres my_app

Password for user site: 
psql (8.4.10)
Type "help" for help.

Finally, all you have to do to connect is the following:

postgres=> c my_app_development

psql (8.4.10)
You are now connected to database "my_app_development".
my_app =>

If you’re having trouble working out what database name you’re trying to connect to you can view a list of all the current database with the following command:

my_app => l

 List of databases
       Name       |  Owner   | Encoding |  Collation  |    Ctype    |   Access privileges   
------------------+----------+----------+-------------+-------------+-----------------------
 postgres         | postgres | UTF8     | en_AU.UTF-8 | en_AU.UTF-8 | 
 my_app_development | site     | UTF8     | en_AU.UTF-8 | en_AU.UTF-8 | 
 my_app_production  | site     | UTF8     | en_AU.UTF-8 | en_AU.UTF-8 | 
 my_app_test        | site     | UTF8     | en_AU.UTF-8 | en_AU.UTF-8 | 
 template0        | postgres | UTF8     | en_AU.UTF-8 | en_AU.UTF-8 |

my_app => q -- q to exit database list

Stop an Application – Heroku

Just a quick post on how to stop an app on Heroku:

root@chris-VirtualBox:~/site# heroku maintenance:on
Maintenance mode enabled.

This will display a static page to all visitors but still allows for migrations etc:

Heroku - Application Offline for Maintenance

Heroku - Application Offline for Maintenance

To re-enable the app simply use the following:

root@chris-VirtualBox:~/site# heroku maintenance:off
Maintenance mode disabled.

Heroku Upload – Permission Denied

I ran into a bit of trouble uploading an app to heroku for the first time, I was unable to authenticate.

root@chris-VirtualBox:~/site# git push heroku master
Permission denied (publickey).
fatal: The remote end hung up unexpectedly

This turned out to be an issue with my public key, in order to fix it simply create a new one:

root@chris-VirtualBox:~/site# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
...

Re-upload it:

root@chris-VirtualBox:~/site# heroku keys:add /root/.ssh/id_rsa.pub
Uploading ssh public key /root/.ssh/id_rsa.pub

And finally re-push everything:

root@chris-VirtualBox:~/site# git push heroku master
Enter passphrase for key '/root/.ssh/id_rsa': 
Counting objects: 579, done.
Compressing objects: 100% (513/513), done.
Writing objects: 100% (579/579), 6.09 MiB | 83 KiB/s, done.
Total 579 (delta 101), reused 0 (delta 0)

-----> Heroku receiving push
-----> Ruby/Rails app detected

This Heroku page actually provides quite a bit of info if you’re still having problems: http://devcenter.heroku.com/articles/rails3

PostgreSQL Installation Error – Ruby

I ran into a bit of problem moving an app to a test Heroku host – I needed to install PostgreSQL. I added the following to my Gemfile:

#Gemfile
#gem 'sqlite3'
gem 'pg'

I then tried to run bundle install:

root@chris-VirtualBox:~/site# bundle install

However I received the following error:

root@chris-VirtualBox:~/site# bundle install


Fetching source index for http://rubygems.org/
Using rake (0.9.2) 
Using multi_json (1.0.3) 
Using activesupport (3.1.1) 
Using builder (3.0.0) 
Using i18n (0.6.0) 
Using activemodel (3.1.1) 
Using erubis (2.7.0) 
Using rack (1.3.4) 
Using rack-cache (1.1) 
Using rack-mount (0.8.3) 
Using rack-test (0.6.1) 
Using hike (1.2.1) 
Using tilt (1.3.3) 
Using sprockets (2.0.2) 
Using actionpack (3.1.1) 
Using mime-types (1.16) 
Using polyglot (0.3.2) 
Using treetop (1.4.10) 
Using mail (2.3.0) 
Using actionmailer (3.1.1) 
Using arel (2.2.1) 
Using tzinfo (0.3.30) 
Using activerecord (3.1.1) 
Using activeresource (3.1.1) 
Using bundler (1.0.21) 
Using rack-ssl (1.3.2) 
Using json (1.6.1) 
Using rdoc (3.10) 
Using thor (0.14.6) 
Using railties (3.1.1) 
Using rails (3.1.1) 
Using acts-as-taggable-on (2.1.1) 
Using awesome_nested_set (2.0.2) 
Using bcrypt-ruby (3.0.1) 
Using carrierwave (0.5.7) 
Using cocaine (0.2.0) 
Using coffee-script-source (1.1.2) 
Using execjs (1.2.9) 
Using coffee-script (2.2.0) 
Using coffee-rails (3.1.1) 
Using orm_adapter (0.0.5) 
Using warden (1.0.6) 
Using devise (1.4.8) 
Using escape (0.0.4) 
Using geocoder (1.0.5) from git://github.com/alexreisner/geocoder.git (at no_grouping) 
Using jquery-rails (1.0.16) 
Using libv8 (3.3.10.2) 
Using nokogiri (1.5.0) 
Using paperclip (2.4.5) 
Installing pg (0.12.2) with native extensions 
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

        /usr/bin/ruby1.8 extconf.rb 
checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
 --with-pg-config=/path/to/pg_config
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

Provided configuration options:
	--with-opt-dir
	--without-opt-dir
	--with-opt-include
	--without-opt-include=${opt-dir}/include
	--with-opt-lib
	--without-opt-lib=${opt-dir}/lib
	--with-make-prog
	--without-make-prog
	--srcdir=.
	--curdir
	--ruby=/usr/bin/ruby1.8
	--with-pg
	--without-pg
	--with-pg-dir
	--without-pg-dir
	--with-pg-include
	--without-pg-include=${pg-dir}/include
	--with-pg-lib
	--without-pg-lib=${pg-dir}/lib
	--with-pg-config
	--without-pg-config
	--with-pg_config
	--without-pg_config


Gem files will remain installed in /usr/lib/ruby/gems/1.8/gems/pg-0.12.2 for inspection.
Results logged to /usr/lib/ruby/gems/1.8/gems/pg-0.12.2/ext/gem_make.out
An error occured while installing pg (0.12.2), and Bundler cannot continue.
Make sure that `gem install pg -v '0.12.2'` succeeds before bundling.

It turned out that because I was on Ubuntu I needed to install the libpq-dev package:

root@chris-VirtualBox:~/site# apt-get install libpq-dev


Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  linux-headers-2.6.38-8 linux-headers-2.6.38-8-generic
Use 'apt-get autoremove' to remove them.
The following extra packages will be installed:
  comerr-dev krb5-multidev libgssapi-krb5-2 libgssrpc4 libk5crypto3
  libkadm5clnt-mit7 libkadm5srv-mit7 libkdb5-4 libkrb5-3 libkrb5-dev
  libkrb5support0 libpq5 libssl-dev
Suggested packages:
  krb5-doc krb5-user postgresql-doc-8.4
The following NEW packages will be installed:
  comerr-dev krb5-multidev libgssrpc4 libkadm5clnt-mit7 libkadm5srv-mit7
  libkdb5-4 libkrb5-dev libpq-dev libpq5 libssl-dev
The following packages will be upgraded:
  libgssapi-krb5-2 libk5crypto3 libkrb5-3 libkrb5support0
4 upgraded, 10 newly installed, 0 to remove and 106 not upgraded.
Need to get 3,142 kB of archives.
After this operation, 8,520 kB of additional disk space will be used.
Do you want to continue [Y/n]? y
Get:1 http://au.archive.ubuntu.com/ubuntu/ natty-updates/main libk5crypto3 i386 1.8.3+dfsg-5ubuntu2.2 [76.7 kB]
Get:2 http://au.archive.ubuntu.com/ubuntu/ natty-updates/main libgssapi-krb5-2 i386 1.8.3+dfsg-5ubuntu2.2 [100 kB]
Get:3 http://au.archive.ubuntu.com/ubuntu/ natty-updates/main libkrb5-3 i386 1.8.3+dfsg-5ubuntu2.2 [327 kB]
Get:4 http://au.archive.ubuntu.com/ubuntu/ natty-updates/main libkrb5support0 i386 1.8.3+dfsg-5ubuntu2.2 [22.2 kB]
Get:5 http://au.archive.ubuntu.com/ubuntu/ natty-updates/main libgssrpc4 i386 1.8.3+dfsg-5ubuntu2.2 [54.6 kB]
Get:6 http://au.archive.ubuntu.com/ubuntu/ natty-updates/main libkadm5clnt-mit7 i386 1.8.3+dfsg-5ubuntu2.2 [38.7 kB]
Get:7 http://au.archive.ubuntu.com/ubuntu/ natty-updates/main libkdb5-4 i386 1.8.3+dfsg-5ubuntu2.2 [38.1 kB]
Get:8 http://au.archive.ubuntu.com/ubuntu/ natty-updates/main libkadm5srv-mit7 i386 1.8.3+dfsg-5ubuntu2.2 [51.6 kB]
Get:9 http://au.archive.ubuntu.com/ubuntu/ natty/main comerr-dev i386 2.1-1.41.14-1ubuntu3 [41.5 kB]
Get:10 http://au.archive.ubuntu.com/ubuntu/ natty-updates/main krb5-multidev i386 1.8.3+dfsg-5ubuntu2.2 [82.5 kB]
Get:11 http://au.archive.ubuntu.com/ubuntu/ natty-updates/main libpq5 i386 8.4.10-0ubuntu0.11.04.1 [81.6 kB]
Get:12 http://au.archive.ubuntu.com/ubuntu/ natty/main libssl-dev i386 0.9.8o-5ubuntu1 [2,019 kB]
Get:13 http://au.archive.ubuntu.com/ubuntu/ natty-updates/main libkrb5-dev i386 1.8.3+dfsg-5ubuntu2.2 [15.0 kB]
Get:14 http://au.archive.ubuntu.com/ubuntu/ natty-updates/main libpq-dev i386 8.4.10-0ubuntu0.11.04.1 [193 kB]
Fetched 3,142 kB in 6s (506 kB/s)                                              
(Reading database ... 167506 files and directories currently installed.)
Preparing to replace libk5crypto3 1.8.3+dfsg-5ubuntu2.1 (using .../libk5crypto3_1.8.3+dfsg-5ubuntu2.2_i386.deb) ...
Unpacking replacement libk5crypto3 ...
Preparing to replace libgssapi-krb5-2 1.8.3+dfsg-5ubuntu2.1 (using .../libgssapi-krb5-2_1.8.3+dfsg-5ubuntu2.2_i386.deb) ...
Unpacking replacement libgssapi-krb5-2 ...
Preparing to replace libkrb5-3 1.8.3+dfsg-5ubuntu2.1 (using .../libkrb5-3_1.8.3+dfsg-5ubuntu2.2_i386.deb) ...
Unpacking replacement libkrb5-3 ...
Preparing to replace libkrb5support0 1.8.3+dfsg-5ubuntu2.1 (using .../libkrb5support0_1.8.3+dfsg-5ubuntu2.2_i386.deb) ...
Unpacking replacement libkrb5support0 ...
Selecting previously deselected package libgssrpc4.
Unpacking libgssrpc4 (from .../libgssrpc4_1.8.3+dfsg-5ubuntu2.2_i386.deb) ...
Selecting previously deselected package libkadm5clnt-mit7.
Unpacking libkadm5clnt-mit7 (from .../libkadm5clnt-mit7_1.8.3+dfsg-5ubuntu2.2_i386.deb) ...
Selecting previously deselected package libkdb5-4.
Unpacking libkdb5-4 (from .../libkdb5-4_1.8.3+dfsg-5ubuntu2.2_i386.deb) ...
Selecting previously deselected package libkadm5srv-mit7.
Unpacking libkadm5srv-mit7 (from .../libkadm5srv-mit7_1.8.3+dfsg-5ubuntu2.2_i386.deb) ...
Selecting previously deselected package comerr-dev.
Unpacking comerr-dev (from .../comerr-dev_2.1-1.41.14-1ubuntu3_i386.deb) ...
Selecting previously deselected package krb5-multidev.
Unpacking krb5-multidev (from .../krb5-multidev_1.8.3+dfsg-5ubuntu2.2_i386.deb) ...
Selecting previously deselected package libpq5.
Unpacking libpq5 (from .../libpq5_8.4.10-0ubuntu0.11.04.1_i386.deb) ...
Selecting previously deselected package libssl-dev.
Unpacking libssl-dev (from .../libssl-dev_0.9.8o-5ubuntu1_i386.deb) ...
Selecting previously deselected package libkrb5-dev.
Unpacking libkrb5-dev (from .../libkrb5-dev_1.8.3+dfsg-5ubuntu2.2_i386.deb) ...
Selecting previously deselected package libpq-dev.
Unpacking libpq-dev (from .../libpq-dev_8.4.10-0ubuntu0.11.04.1_i386.deb) ...
Processing triggers for man-db ...
Processing triggers for doc-base ...
Processing 1 added doc-base file(s)...
Registering documents with scrollkeeper...
Processing triggers for install-info ...
Setting up libkrb5support0 (1.8.3+dfsg-5ubuntu2.2) ...
Setting up libk5crypto3 (1.8.3+dfsg-5ubuntu2.2) ...
Setting up libkrb5-3 (1.8.3+dfsg-5ubuntu2.2) ...
Setting up libgssapi-krb5-2 (1.8.3+dfsg-5ubuntu2.2) ...
Setting up libgssrpc4 (1.8.3+dfsg-5ubuntu2.2) ...
Setting up libkadm5clnt-mit7 (1.8.3+dfsg-5ubuntu2.2) ...
Setting up libkdb5-4 (1.8.3+dfsg-5ubuntu2.2) ...
Setting up libkadm5srv-mit7 (1.8.3+dfsg-5ubuntu2.2) ...
Setting up comerr-dev (2.1-1.41.14-1ubuntu3) ...
Setting up krb5-multidev (1.8.3+dfsg-5ubuntu2.2) ...
Setting up libpq5 (8.4.10-0ubuntu0.11.04.1) ...
Setting up libssl-dev (0.9.8o-5ubuntu1) ...
Setting up libkrb5-dev (1.8.3+dfsg-5ubuntu2.2) ...
Setting up libpq-dev (8.4.10-0ubuntu0.11.04.1) ...
Processing triggers for libc-bin ...
ldconfig deferred processing now taking place
root@chris-VirtualBox:~/site# 

And then just run bundle install again:

root@chris-VirtualBox:~/site# bundle install

There’s some documentation on the gems wiki page: https://bitbucket.org/ged/ruby-pg/wiki/Home

Re-Index Sunspot/Solr – Ruby on Rails

Just a very quick post on how to re-index Sunspot/Solr – simply open the console and enter the following command:

chris@chris-VirtualBox:~/site$ rake sunspot:solr:reindex

WARNING: 'task :t, arg, :needs => [deps]' is deprecated.  Please use 'task :t, [args] => [deps]' instead.
    at /usr/lib/ruby/gems/1.8/gems/sunspot_rails-1.2.1/lib/sunspot/rails/tasks.rb:41
chris@chris-VirtualBox:~/site$ 

That’s all there is to it, nice and easy!