Monday, September 17, 2012

Install pgAdmin for PostgreSQL 9.2 on Ubuntu 12.04


The Ubuntu 12.04 package manager will install pgadmin3 version 1.14 which does not support PostgreSQL 9.2.

Warning:
The server you are connecting to is not a version that is supported by this release of pgAdmin III.
pgAdmin III may not function as expected.
Supported server versions are 8.2 to 9.1.

Remove the current pgAdmin version


sudo apt-get remove pgadmin3


Install pgAdmin version 1.16 using a ppa


sudo apt-add-repository ppa:voronov84/andreyv
sudo apt-get update
sudo apt-get install pgadmin3


Update: If you are having trouble with the method above (see comments below) try the .deb install.

pgAdmin 1.16 (amd64)
pgAdmin 1.16 (i386)


Saturday, September 15, 2012

Replace PostgreSQL 9.1 with 9.2 in Ubuntu 12.04

Recently released PostgreSQL 9.2 comes with a heap of new features and improvements over 9.1.

These are the steps I took to remove 9.1 and install PostgreSQL 9.2 in Ubuntu 12.04.

Warning: This procedure is suitable for test and development environments only as it will wipe out all 9.1 databases


1. List currently installed postgres packages


sudo dpkg -l | grep postgres


2. Remove postgresql-9.1

*these packages were on my system

sudo apt-get --purge remove postgresql postgresql-9.1 postgresql-client-9.1 postgresql-client-common postgresql-common postgresql-doc postgresql-doc-9.1


3. Install postgresql-9.2 with official ppa


sudo add-apt-repository ppa:pitti/postgresql
sudo apt-get update
sudo apt-get install postgresql-9.2

If you cannot add the repository
sudo: add-apt-repository: command not found

Install this package

sudo apt-get install python-software-properties


4. Check postgresql is set to the default port 5432


sudo netstat -lp | grep postgresql


5. Reset to default port if necessary


sudo gedit /etc/postgresql/9.2/main/postgresql.conf

port = 5432 *change the port value in the conf file

sudo /etc/init.d/postgresql reload


6. Setup your project database / user and database.yml


PostgreSQL Setup for Rails Development (skip to step 2)


Sunday, August 12, 2012

Could not find a JavaScript runtime

Devs new to Linux will hit a 'Could not find a JavaScript runtime' error when trying to start a rails server.

The reason for this is Linux, by default, does not have an interface to a JavaScript engine.

Use one of these methods to install the interface required:

  1. Add The Ruby Racer gem to your Gemfile assets group:

    group :assets do
      gem 'therubyracer'
    end

    * Its a bit of a memory hog (Heroku discourage it) and you need to add it to every project you spin up on that machine.


  2. Install the Node.js package to your machine:

    sudo apt-get install nodejs

    * Distro specific installation: Installing Node.js by Package Manager


Sunday, July 29, 2012

Learning Rails Testing with MiniTest:Spec instead of RSpec?

If you haven't already, or if you are just starting out with Rails, now would be a good time to take a look at MiniTest:

  • Included with Ruby 1.9
  • Likely to replace Test:Unit in Rails 4
  • Complete Suite (Unit, Spec, Mock, Benchmark)
  • Fast, readable, efficient

Any other Rails beginners skip through the RSpec tests in a two-footed lunge at Michael Hartl's Rails tutorial?

With the best of intentions I didn't stand a chance:

  • Prior experience - my non-Ruby testing consisted of basic manual logical steps
  • Frustration - tests take time and I came to the tutorial to learn Rails
  • RSpec's alien syntax - strange snake_cased semi English

As a Rails beginner this is what I needed to know about testing before taking the tutorial:

  • To write efficient and elegant code we need to check its quality and stability using a testing framework
  • Tests are the backbone of a Rails project giving confidence to change and improve
  • Would you accept code into your project that isn't tested?

A year on and wanting to improve my testing code and workflow I revisited the holy grail of Rails tutorials with the plan to use MiniTest:spec in place of RSpec.

No real issues so far and couple of nice workarounds here is my GitHub repo *work in progress

Helpful Resources

Please share any MiniTest resources you find and i'll update the list.



Monday, June 4, 2012

PostgreSQL setup for Rails development in Ubuntu 12.04

Rails comes with SQLite and it works out of the box with no configuration. That's great! but... its not suitable for a production environment so we need another database if your app is to see the light of day.

You could use SQLite in your development environment and switch it up for production, however, it makes far more sense to use the same database in development and production.

My personal preference is to use PostrgreSQL, its open source and gaining traction in the Rails world due to stability and rather nifty features. Heroku also uses PostgreSQL so its a good option if you plan on using their services.

See the excellent PostgreSQL Documentation for more information.

1. Install postgresql and admin tools through the package manager


sudo apt-get install postgresql libpq-dev phppgadmin pgadmin3

Change the postgresql configuration to solve this error
FATAL: Peer authentication failed for user

sudo nano /etc/postgresql/9.1/main/pg_hba.conf

Replace local peer with local md5 in the file. Then reload postgresql to apply the change.

sudo /etc/init.d/postgresql reload


2. Setup your databases from the command line


a) Login to postgresql prompt as the postgres user

sudo su postgres -c psql

b) Create a postgresql user for your project

create user username with password 'password';

TIP: If you are running tests, then to ease permission issues setup your postgres user with the same name and password as your Ubuntu user and make him a postgres superuser ( \du lists users with permissions).

alter user username superuser;


c) Create the development and test databases

create database projectname_development;
create database projectname_test;

d) Give permissions to the user on the databases

grant all privileges on database projectname_development to username;
grant all privileges on database projectname_test to username;

e) Finish your postgresql session

\q

Other useful commands in a postgresql session


Update password

alter user username with password ‘new password’;

Destroy database

drop database projectname;

Connect to specific database

\c databasename

Common MySQL commands with postgresql shortcut

MySQL Postresql
SHOW DATABASES \l
SHOW TABLES \d
SHOW COLUMNS \d table



3. Place a database configuration file in your rails project


Save the code below as app/config/database.yml and rake db:migrate your project

development:
  adapter: postgresql
  encoding: unicode
  database: projectname_development
  pool: 5
  username: username
  password: password

test:
  adapter: postgresql
  encoding: unicode
  database: projectname_test
  pool: 5
  username: username
  password: password




4. Misc Errors encountered


If you are importing data (or using a seed file) then you may run across this error.
PG::Error: ERROR: duplicate key value violates unique constraint "table_name_pkey"

Your import didn’t use the sequence so we need to reset it to be in sync again.

Note when replacing table_name use a plural.

sudo su postgres -c psql
\c database_name
select setval('table_name_id_seq', (select max(id) + 1 from table_name));


Tuesday, May 15, 2012

How to install Rvm, Ruby, and Rails in Ubuntu 12.04

A concise guide to get Rails running locally.

1. Install package dependencies

sudo apt-get install zlib1g zlib1g-dev build-essential
sqlite3 libsqlite3-dev openssl libssl-dev curl git
libmagick++-dev libmagick++4

2. Install rvm from Github source

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

3. Reload terminal shell

source /home/ben/.rvm/scripts/rvm

4. Add rvm to bash

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

5. Add any additional requirements specified by rvm

rvm requirements
copy / paste any Additional Dependencies: specified in the terminal output into sudo apt-get

6. Install ruby version to rvm

rvm install 1.9.3-p194

*Update rvm and Ruby version (useful to know)
rvm get stable
rvm list known

Troubleshooting
RVM is not a function, selecting rubies with 'rvm use ...' will not work.
Open your Terminal  and select Edit > Preferences > Run command as login shell (make sure it is selected)

7. Setup default ruby version in rvm

rvm use 1.9.3-p194 --default

8. Install rails

gem install rails

9. Install RSpec

gem install rspec

10. Install Bundler

gem install bundler

If you see ERROR: Gem bundler is not installed, run `gem install bundler` first. when running bundle change https://rubygems.org to http://rubygems.org in your Gemfile.

Saturday, May 12, 2012

Sublime Text 2 Setup for Rails

NB: Please see this post for Sublime Text 2 installation

After installing Sublime Text 2 these are the settings I use and packages I install to assist with Ruby on Rails development. Use all or some of these to suit your own needs.

Remove the Minimap 

View > Hide Minimap

Install Package Manager

View > Show console

Copy code below / paste / enter


import urllib2,os; pf='Package Control.sublime-package'; ipp=sublime.installed_packages_path(); os.makedirs(ipp) if not os.path.exists(ipp) else None; urllib2.install_opener(urllib2.build_opener(urllib2.ProxyHandler())); open(os.path.join(ipp,pf),'wb').write(urllib2.urlopen('http://sublime.wbond.net/'+pf.replace(' ','%20')).read()); print 'Please restart Sublime Text to finish installation'

 

Install Ruby / Rails related Packages 

Preferences > Package Control > Package Control:Install Package
  • ERB Insert and Toggle Commands 
  • Rails Related Files 
  • RubyTest 
  • Rspec 
  • Guard 
  • Haml 
  • Sass 
  • Coffeescript 

 

User Settings

Preferences > Settings - User

Copy code below / paste / save

{
"color_scheme": "Packages/Color Scheme - Default/railscasts.tmTheme",
"font_face": "Droid Sans Mono",
"font_size": 12,
"scroll_past_end": false,
"detect_indentation": false,
"tab_size": 2,
"translate_tabs_to_spaces": true,
"trim_trailing_white_space_on_save": true,
"word_wrap": true
}

 

Create a plugin to Open file in browser

Tools > New plugin...

Copy code below / paste / save as OpenBrowser.py

# OpenBrowser.py
import sublime, sublime_plugin
import webbrowser

class OpenBrowserCommand(sublime_plugin.TextCommand):
   def run(self,edit):
      url = self.view.file_name()
      webbrowser.open_new(url)

 

Add Keybindings to Open file in new browser and to insert ERB

Tools > Command Palette > "User Key bindings"

Copy code below / paste / save

[
{ "keys": ["ctrl+shift+b"], "command": "open_browser" },
{ "keys": ["ctrl+shift+."], "command": "erb" }
]

 

Monday, May 7, 2012

Sublime Text 2 - Linux Textmate Alternative

There are plenty of editors and IDE's out there for Linux Rubyists and my search was based on an editor that was modern, fast, and a viable alternative to the popular Mac only editor Textmate.

Sublime Text 2 came out on top, as my editor of choice, for its features, speed, and ability to use Textmate themes. Its not perfect or free (price) yet... while its in beta status you are free to use it (with minor nagware). To get started see below or download directly (more detailed setup instructions in my next blog post).

Ubuntu install via the webupd8 maintained repo

sudo add-apt-repository ppa:webupd8team/sublime-text-2
sudo apt-get update
sudo apt-get install sublime-text-2-dev
or
sudo apt-get install sublime-text-2-beta

Install Railscasts theme


Copy
https://raw.github.com/ryanb/textmate-themes/master/railscasts.tmTheme

Place it here
.config/sublime-text-2/Packages/Color Scheme - Default

Select it
Preferences > Color Scheme > railscasts

Sunday, May 6, 2012

Rails + OSX Developer Monopoly

Why are so many Rails developers using the Apple Mac?

  • 37 Signals use them! They even made a cheezy video about it!! 
  • Build quality is good and its a fashionable tech accessory 
  • Single platform across a team of developers makes for an easier life and Mac is the obvious choice
  • Textmate, the modern standard Rails text editor in many ways, is Mac only software
I’m not trying to dissuade anyone from using a Mac for Rails development – like many Linux users I like having a choice.

Ruby on Rails is not, last time I checked anyway, a Mac specific programming language / web framework.

Having just moved to the latest Ubuntu (12.04 LTS) as a platform for my RoR projects i’m finding it a comparably slick alternative to the OS X monopoly.