Using Vagrant for GoLang Development with Ubuntu

Yesterday I was trying to setup vagrant for golang using ubuntu box.💻

if you google, can see a lot of git repositories are available which already have vagrant setup and some of them even have puppet integration.

I always love to be a problem solver, so I decided to make dev setup from scratch. to be honest its pretty simple to setup vagrant with ubuntu and golang.

In this article, I will use ubuntu 14.04 and golang 1.9.1 for environment setup.

Pre - Requirements : 🛫

1, Virtual Box
Download from - https://www.virtualbox.org/wiki/Downloads
2, Vagrant
Download from - https://www.vagrantup.com/downloads.html

before we getting into rest of the part, i would like to show you the final folder structure,

Screen_Shot_2018-05-15_at_11_45_34_AM

that's it, let's start setting up the environment. 🎉

🏹 let's assume we are going to keep our source code in desktop, inside a folder called workspace.

mkdir ~/Desktop/workspace
cd ~/Desktop/workspace

Screenshot :
Screen-Shot-2018-05-15-at-11.38.49-AM

🏹 Writing our vagrant file

  • Create a file named Vagrantfile , and place in root folder
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
    config.vm.box = "ubuntu/trusty64"

    # set the hostname
    config.vm.hostname = "mylocal.dev"

    #set the shared folder
    config.vm.synced_folder "src", "/home/vagrant/workspace/src"

    #provision the box
    config.vm.provision :shell, :path => "setup/install.sh"

    #port forwarding
    config.vm.network "forwarded_port", guest: 8085, host: 8085,
        auto_correct: true

end 

Explanation :

config.vm.box = "ubuntu/trusty64"
this line defines the box details , ubuntu/trusty64 is ubuntu 14.04.

for more available boxes , visit below link. you can go though 100's of boxes
https://app.vagrantup.com/boxes/search

config.vm.synced_folder "src", "/home/vagrant/workspace/src"
when we consider normal ftp concept , when ever we do some changes in the source code we have to manually copy that file into server. and its a repeated proses.

vagrant synced folder allows you to ,map host folder to guest machine. hence when ever you make a change in host machine, it will update in guest machine too.

in our case , am mapping src folder from host root to /home/vagrant/workspace/src guest location.

Screen_Shot_2018-05-15_at_3_24_31_PM

config.vm.provision :shell, :path => "setup/install.sh"

this line of code is very important, as this one does all installation and configuration part inside ubuntu.

What Is Provisioning ?

  • Provisioners in Vagrant allow you to automatically install software, alter configurations, and more on the machine as part of the vagrant up process.

in our case , provisioner is a shell script and in some cases it can be puppet or any other provisioning tools.

lets go inside our provisioner and see what it does,
open setup/install.sh in your favorite editor.

#!/bin/bash
# This is the entry point for configuring the system.
#####################################################

#install basic tools
sudo DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confnew" install git

#get golang 1.9.1
curl -O https://storage.googleapis.com/golang/go1.9.1.linux-amd64.tar.gz

#unzip the archive 
tar -xvf go1.9.1.linux-amd64.tar.gz

#move the go lib to local folder
mv go /usr/local

#delete the source file
rm  go1.9.1.linux-amd64.tar.gz

#only full path will work
touch /home/vagrant/.bash_profile

echo "export PATH=$PATH:/usr/local/go/bin" >> /home/vagrant/.bash_profile

echo `export GOPATH=/home/vagrant/workspace:$PATH` >> /home/vagrant/.bash_profile

export GOPATH=/home/vagrant/workspace

mkdir -p "$GOPATH/bin" 

everything here is pretty simple, except #install basic tools block. somehow if you want to install tools via apt-get, you have to use this in script. (honestly i don't know why 🤔)

rest of the part is pretty simple,

  1. downloading the golang archive (with version in mind)
  2. extracting it
  3. moving the folder into /usr/local/go
  4. update the .bash_profile file to enable go support in terminal

thats it, done writing provisioner

now, lets go to the last line of Vagrantfile
config.vm.network "forwarded_port", guest: 8085, host: 8085,
if you are developing socket based programs , or lets say a web application. then you might need to access program via browser using a port number.

in my example, port number of guest 8085 (which will be used by my golang application) is mapped to port number of host application. hence when ever you try to access the port in host application though web browser, it will redirect to guest os.

Note : some times , if the host port is used by other applications then vagrant will automatically assign one available port. to see the correct mapping information use command vagrant port

to run your box,

  1. go to the root directory

cd /workspace/

(Note : in your case location might be different depending on at which location , you have created the vagrant file)

2, running vagrant box

vagrant up

above script will start creating new box for you with provision information.

Screen-Shot-2018-05-15-at-4.03.11-PM

Screen-Shot-2018-05-15-at-4.04.14-PM

Screen-Shot-2018-05-15-at-4.04.23-PM

Screen-Shot-2018-05-15-at-4.05.55-PM

depending on your Internet speed it might take some time, if there is no error while provisioning then you will get a screen like above.

🤞 now its time to login to out box and check the source folder is there , golang is installed etc..

3, Login into vagrant

vagrant ssh

Screen-Shot-2018-05-15-at-4.08.33-PM

now lets go to the workspace folder and see our code is there, if there lets execute it

cd /home/vagrant/workspace/src/github.com/rahulvramesh/hello-go

go run main.go 

ubuntu-go-1_9_1_-ssh-vagrant_ssh-_106-48

wola!!!!! 🎉🎉🎉🎉🎉🎉🎉

to stop the box use

vagrant stop

and for deleting the entire box, use

vagrant destroy

NB : Please refer my repo for source code.
https://github.com/rahulvramesh/go-vagrant

Show Comments

Get the latest posts delivered right to your inbox.