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 : 🛫
before we getting into rest of the part, i would like to show you the final folder structure,
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
🏹 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
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
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.
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,
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,
- downloading the golang archive (with version in mind)
- extracting it
- moving the folder into
- update the
.bash_profilefile to enable go support in terminal
thats it, done writing provisioner
now, lets go to the last line of
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
to run your box,
- go to the root directory
(Note : in your case location might be different depending on at which location , you have created the vagrant file)
2, running vagrant box
above script will start creating new box for you with provision information.
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
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
to stop the box use
and for deleting the entire box, use
NB : Please refer my repo for source code.