Category Archives: Programming

Getting started with Go language on Ubuntu/Linux

Go (Golang) is an open source programming language developed at Google. It’s a compiled and statically typed language like C/C++/Java. It’s lightweight and fast, with small memory footprint and its support for concurrency, networking and multi-processing makes it an interesting choice for specific projects.

I use Go for building small command line apps and rewriting some parts of Sinatra/Rails(Ruby based web frameworks) applications in Go. It helps me save lots of CPU power and RAM after I rewrote some CPU intensive modules in Go. Ruby(Sinatra/Rails) is my default choice for new web projects, but I’m thinking of trying Golang(net/http) for my next project, especially if the performance/speed is critical.

Setting up Go in Ubuntu [14.04]

1. Download the package from the official page.

2. Extract it (to /usr/local or anywhere else)

sudo tar -C /usr/local -xzf go1.5.3.linux-amd64.tar.gz

3. Set the path correctly
Open bash config file using nano (or your preferred text editor).

nano ~/.bashrc

And append the following line.

export GOPATH=$HOME/go
export PATH=$PATH:/usr/local/go/bin
export PATH=$PATH:$GOPATH/bin
export GOBIN=$GOPATH/bin

4. Reload Bash (to apply the new config)

source ~/.bashrc

Type go in the terminal to check if the command is available. Next, you can start writing programs in Golang.

Writing your first program in Go

Lets write a simple “Hello World” Program (hello.go) that would simply print something using fmt.

package main
import "fmt"

func main() {
  fmt.Println("Hello World!")


go build hello.go



Recommended Links

Adding SSL to a Rails Application

Adding SSL to a new or existing Rails application isn’t really that difficult. If the website exists for a while and you want to move to https, then you must properly redirect (301) visitors to the new url. If it’s a new website, then it’s probably a good idea to use SSL from the start. (it’s must if you collect any kind of sensitive data from the users)

Assumptions : It’s a Rails 4 app, running with nginx, passenger and Ubuntu 14.04 Server. (preferably on VPS, or somehow you should be able to update nginx configs and so). For SSL certificate, I would recommend Comodo PositiveSSL Certificate from NameCheap @ $9 a year, unless you’ve a good reason to spend more on that.

SSL Setup

First, generate a key and then CSR for buying a SSL certificate. Enter the required info as required. Watch out for Common Name / FQDN field, it must match with the domain (in this case :

openssl genrsa -out 2048
openssl req -new -key -out

Then copy the content of above csr file to your clipboard (use xclip, a command line utility) and paste that into SSL order form.

xclip -sel clip < path_to_your_csr_directory/

Next, you’ll receive a confirmation email. After confirming that, they will email you the certificate. (usually within few hours)

Once you receive the ssl certificate (usually in *.zip format), extract the zip file (containing certificates) and concatenate them in right order to get a single certificate file.

cat www_example_com.crt COMODORSADomainValidationSecureServerCA.crt COMODORSAAddTrustCA.crt AddTrustExternalCARoot.crt > ssl-bundle.crt

Now, you need to upload these two files – ssl-bundle.crt and (the private key, generated earlier) to the server. (use scp. e.g scp target_file user@server_ip:file_name)

Preparing Rails for SSL

Enable SSL in production mode, by updating the config/environments/production.rb file.

config.force_ssl = true

And you also need to make sure all the external resources (e.g fonts, images, css, js etc) are loaded securely over https only.

Nginx setup

Login to VPS/Server and create/update your nginx config for ssl.

sudo nano /etc/nginx/sites-available/

A sample nginx config for Rails Application.

Now, enable that nginx config and reload the server.

sudo ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled/
sudo service nginx reload

Deploying static websites to VPS using git

git is a distributed open source version control system. If you’re not already familiar with git then read some free tutorials listed here and come back after you have some basic understanding to follow through the tutorial. Whether it’s a static website or a fun project, it’s usually a good idea to use a version control system such as git. Additionally, git can also help you in deployment and make your development workflow much easier and simpler.

git deployment

Step #1. Server Setup

If you’ve not already selected a VPS then get one there on Digital Ocean or Linode. Make sure you select Ubuntu (preferably : 14.04 LTS) for the server OS. Otherwise, you may need to adjust few commands depending on the Linux distribution you’re using.

Step #2. Configure git on the server

SSH into the remote server and configure git for deployment.

ssh example_user@IP_ADDRESS
mkdir example.git
cd example.git
git init --bare

Next, create a git hook – a shell script that will be executed on git push (to update code on server).

nano ~/example.git/hooks/post-receive

Here is a sample post-receive script you can use : (use Ctrl+X to save and exit)

And make it executable.

chmod +x ~/example.git/hooks/post-receive

Step #3. Install and configure git on dev machine

First, install git, move into your project directory and commit it under git.

sudo apt-get -y install git-core
git init
git add .
git commit -am "MVP is ready for the wild"

Add remote repository for deployment server.

git remote add production ssh://example_user@IP_ADDRESS/~/example.git

And now, you’re ready to deploy. Just type (to push recent changes to live server) :

git push production master

Step #4. Configure nginx

ssh into the server and create a nginx config for the domain (

sudo nano /etc/nginx/sites-available/example.conf

Here is a sample config you could use :

sudo ln -s /etc/nginx/sites-available/example.conf /etc/nginx/sites-enabled/
sudo service nginx reload

Now, update the DNS settings at the registrar and create two records – one for naked domain (e.g and other for www-version of the website (e.g – both pointing to the server IP. And, wait for the DNS to be propagated (few minutes).

Skeleton – A lightweight CSS framework for your next side project

Skeleton is a lightweight CSS framework that provides a nice boilerplate to kickstart the development process. I like the minimal design approach. Plus, I can easily add more CSS on the top if required. The grid and some basic styles are more than enough to get started.

Simple Responsive grid

Simple, easy and human friendly grid-naming conventions. Just add a div element with class ‘row’ (with child columns) inside a container and you’re good to go (12 column grid – kind of similar to bootstrap and foundation).

Basic styles for Standard elements

Skeleton comes with basic styles for standard HTML elements such as Headings (h1/h2/h3 etc), basic form elements (input, buttons etc), tables and some utility classes (for left/right alignment, clearing floats etc). Here is how a simple ‘sign in’ form with Skeleton.

Lightweight, fast and easy to override

The Skeleton along with Normalize CSS is less than 10KB when minified. So, it’s a great choice for smaller web projects, side projects, mobile websites, landing pages for your mobile apps etc.

You can download the latest version from the official website : (Current version : 2.0.4). Sass and LESS versions are also available to help you add Skeleton in your preferred development setup.

Also check out my last weekend project – Mobile Number Tracker (India) – I created using Skeleton.

Start Unicorn on reboot in a Rails application

If you need to run some cron jobs on server, check out the ruby gem whenever. It lets you write cron jobs in ruby way. You can do lots of useful thing using whenever, create database dumps/backups, run scripts/commands on reboot etc.

Using whenever to start unicorn on reboot

1. Add whenever to your Gemfile and run bundle command.

gem 'whenever', :require => false

2.Writing a simple cron job
Move to the project directory and run

wheneverize .

Now, open the file config -> schedule.rb and remove the default contents. Add these lines instead. (you need to replace unicorn_app with whatever you’ve defined in your nginx config)

If you use capistrano for deployment, you should add this to config/deploy.rb.

require "whenever/capistrano"

Next time, you push the code to the server, it will create the required cron jobs on the server. Make sure, it’s working as expected and let me know (via comments) if you’ve any issue.