10 11 / 2011

Setup GitLab (Github clone) using Vagrant and Chef

Update: Nov 28, 2011: The box that is downloaded via vagrant might not work properly. Here is the box that I’d custom built to have chef pre-installed. You can download it here http://api2.ge.tt/0/8gA4QSA/0/blob/download

This post is Part 1 of the series Rails app with distributed deployment playground

Here we’ll be installing GitlabHQ.

I won’t be showing on how to install Vagrant with VirtualBox here. If you’re not familiar with Vagrant and how to set it up, then first watch the Railscasts 292 and install the Vagrant with VirtualBox.

Then you can start with the following commands in the terminal. First of all, we have to have our own chef-repo to contain the cookbooks.

You can clone my repo or you can use your own or others. Here I’ll be using mine chef-repo

git clone git://github.com/millisami/chef-repo.git

Now, lets start with the gitlabhq setup.

mkdir vagrants
cd vagrants
git clone git://github.com/gitlabhq/gitlabhq.git
cd gitlabhq
vagrant init

Modify the Vagrantfile as below

Vagrant::Config.run do |config|
  config.vm.box = "lucid32"
  config.vm.network ""

  config.vm.customize do |vm|
    vm.name = "GitlabHQ"
    vm.memory_size = 384

  config.vm.provision :chef_solo do |chef|
    chef.log_level = :debug
    chef.cookbooks_path = ["~/chef-repo/cookbooks"]
    chef.add_recipe "base"
    chef.add_recipe "rvm::install"
    chef.add_recipe "rvm::ruby_192"

    chef.json = { 
      :base => {
        :system_packages => ["tree", "htop", "vim-nox"]

Then from the terminal, run the following commands.

vagrant up
vagrant ssh

Once you login, install these dependencies.

sudo apt-get install -y git-core sqlite3 libsqlite3-dev gitosis libcurl4-openssl-dev python-setuptools

sudo visudo

Add the following line at the VERY BOTTOM of the file, if not at the end, it can be nullified by later entries


Ctrl-X to leave, Y to save your changes. Logout logout and ssh vagrant ssh again to reflect the effect.

Lets install the gem dependencies.

cd /vagrant
rvmsudo gem install bundler --pre

# Seeding the database
RAILS_ENV=production bundle exec rake db:setup
RAILS_ENV=production bundle exec rake db:seed_fu

Write the admin account and its password which we’ve to use later while accessing gitlab.

Administrator account created:


Then, we have to setup the following:

sudo easy_install pygments
sudo echo 'export RAILS_ENV=production' >> ~/.bash_profile
sudo adduser --system --shell /bin/sh --gecos 'git version control' --group --disabled-password --home /home/git git
ssh-keygen -t rsa
sudo -H -u git gitosis-init < ~/.ssh/id_rsa.pub
sudo chmod 755 /home/git/repositories/gitosis-admin.git/hooks/post-update

echo "gem: --no-rdoc --no-ri" > ~/.gemrc
rvmsudo gem install passenger
rvmsudo passenger-install-nginx-module

Creating an Upstart file for nginx

sudo vim /etc/init/nginx.conf

description "Nginx HTTP Server"

start on filesystem
stop on runlevel [!2345]


exec /opt/nginx/sbin/nginx -g "daemon off;"

Restart the VM

sudo shutdown -r now

Verify nginx urnning

sudo initctl list | grep nginx

You should see:

nginx start/running, process 678

…Or use a browser and visit

Open nginx.conf to configure nginx

sudo vim /opt/nginx/conf/nginx.conf

Change the user to ‘vagrant’

Then inside the http {....}, add/replace a server block looking similar to this:

http {

    server {
    listen 80;
    server_name localhost;
    root /vagrant/public;
    passenger_enabled on;

Restart nginx to pick up the changes

sudo service nginx stop; sudo service nginx start

or just

sudo start|stop nginx

Setting up the first repository

  1. Access on the HOST machine’s browser
  2. Login in using admin@local.host and 5iveL!fe
  3. Add your HOST key at
  4. Create the first project, say (Myproject)
  5. Add a remote. git remote add vagrant-chef git@
  6. Push with tracking branch. git push -u vagrant-chef master

Dealing with the Git and SSH keys gotchas

  1. Added user vagrant to group git since /home/git/repositories directory is owned by git

  2. Did one clone in ~/tmp directory git clone git@localhost:gitosis-admin.git gitosis-copy to add the RSA key to the list of known hosts. So, that when later via browser, the webserver will try to create a new repo. Otherwise you might end up with the infamous 500 error page.

Thats it for this part. Hang-on for the part 2 of the series Rails app with distributed deployment playground

10 11 / 2011

Rails app with distributed deployment playground

While setting up Etxpress on the production box with system wide installed RVM and Resque for background processing, monitored via Upstart using Foreman export command, I had to run the following command to export the updated Procfile.production file with capistrano and custom cap deploy:foreman task.

rvmsudo foreman export upstart /etc/init -a etxpress -u deploy \ 
        -f ./Procfile.production -c worker=1 scheduler=1 redis=1 

Since the command rvmsudo is for sudoers, it asks for sudo password which capistrano cannot handle. Similarly, there are some other commands that need the sudo access as well. So, tweaking the settings directly in production and playing with cap deploy:foreman and other sudo related tasks, I’d to manually login and do service restarts.

This is not a best practice to test or play with deployment settings directly on the production box. So, to simulate the same environment and process, here the Vagrant and Chef come in.

The idea is to host a Gitlab as a Github repo in one VM, launch another VM to simulate the production box and setup the multistage capistrano recipe to test and experiment.

Local Deployment Diagram

It will be a long post. So, I’ll be blogging in the following series:

  1. Setup Gitlab as a Github repo hosting
  2. Setup a production box in another VM
  3. Setup capistrano multistage to test the deployment

To automate the process, we’ll be using vagrant to launch VM(s) and chef-solo for provisioning. I might do another post using chef-server to make it more automated.

Without further ado, here is the first part of the series: Setup Gitlab as a Github repo hosting using vagrant and chef-solo.