Custom Ubuntu Vagrant Base Image

I live in Linux, but not every one in my team GivingFire.com does. Windows has its uses, but when it comes to Rails development, its not a good idea. Windows 10 WSL (Windows Subsystem for Linux) is a great step, but its still not there. But one can always use a virtual machine and vagrant makes integration super easy. This is how you would make your own Vagrant image.

You need to first use an existing image. I used the one provided by Canonical them self. It doesn’t follow the proper specs of vagrant, but I don’t care.

#~ cd /path/to/your/project
#~ vagrant init ubuntu/xenial64

Make necessary changes to the vagrant file…

#~ vagrant up
#~ vagrant ssh

Now as I said, the box by Canonical is having some issues. You can either create a new vagrant user with password or change the current password. If you don’t do this you will get stuck with “Authentication failure” and unable to login after creating an VM using the box.

#~ sudo passed ubuntu

Now do your installations and configuration within the virtual machine. Make sure you test your application before we continue to the next step.

Now it is time to minimize it and make it in to a box that you can distribute. Lets clean up apt, zero out and clear the bash history.

#~ sudo apt-get clean
#~ sudo dd if=/dev/zero of=/EMPTY bs=1M
#~ sudo rm -f /EMPTY
#~ cat /dev/null > ~/.bash_history && history -c && exit

Now we can make the box and add it to the list of boxes.

#~ vagrant package --output project.box
#~ vagrant box add project project.box

Now lets try it out.

#~ vagrant destroy
#~ vagrant init project

If you didn’t create a vagrant user with password vagrant which is the default you will have to explicitly mention it in the vagrant file.

Vagrant.configure("2") do |config|
  config.vm.box = "gf"

  config.vm.network "forwarded_port", guest: 3000, host: 80

  config.ssh.username = "ubuntu"
  config.ssh.password = "password"

  config.vm.provider "virtualbox" do |vb|
    vb.memory = "1024"
  end
end

Then you can do vagrant up and then ssh to the box using vagrant ssh. It will prompt the password.

Once you know its working, distribute it how ever you see it fit.