Building a Vagrant Box: Setting up your Environment
Tags: ruby, rails, webdev, vagrant
Published: 2021-09-27 05:50:59

---
title: Building a Vagrant Box: Setting up your Environment
published: true
description: So, you want to set up a virtual development environment. But before you begin, here is a detailed guide on how to utilize the vagrant box and virtual machine.
tags: Ruby, Rails, webdev, vagrant
canonical_url: https://blog.engineyard.com/build-vagrant-box-set-up-environment?utm_source=dev.to&utm_campaign=Canonical-url&utm_id=QiWorks.in
//cover_image: https://blog.engineyard.com/hubfs/Blog_Blog%202%20copy%20%281%29.jpg
---
By [Ritu Chaturvedi](https://blog.engineyard.com/author/ritu-chaturvedi)

If setting up a virtual development environment is your goal, here is a guide on how to utilize the vagrant box and virtual machine for this purpose. Let’s discuss the process in detail with this blog.

Here, we will build a vagrant on top of a virtualization engine, as VirtualBox.&nbsp;

## Creating the vagrant box

[Download VirtualBox](https://www.virtualbox.org/wiki/Downloads) and create a directory. You will also need a base operating system. Let’s use ‘ubuntu/trusty64’ for now. Now, we need to initiate the use of the vagrant directory as ‘vagrant init’ and the operating system name.&nbsp;

So, the code would be:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _mkdir vagrant\_demo &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_ --create directory

_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_ _cd vagrant\_demo &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_ --enter into the directory

_vagrant init ubuntu/trusty64&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_ --initiate vagrant operating system

A [VagrantFile](https://friendsofvagrant.github.io/v1/docs/vagrantfile.html), which explains the configuration of the vagrant environment, is created with that last command. Five specs in this file can be modified to suit your requirements. If no modifications are done, the default setup will be used.&nbsp;

&nbsp;_-config.vm.box_ -- defining vagrant Operating System.

_-config.vm.provider_ -- defining the base (Virtual Machine). With this command, you can also manipulate the number of CPUs used.

_-config.vm.network_ -- defining IP address and ports of application. Rails applications usually default to port 3000. In our case, the host is our computer, and the guest is a virtual machine.

_-config.vm.synced.folder_ -- defining how the guest accesses files in the host. Thus, the project files can be modified on your computer, and then they will be automatically synced to the virtual machine.

_-config.vm.provision_ -- defining the virtual environment setup.

Thus, the final version of our vagrant file is as follows:

![vagrant file](https://blog.engineyard.com/hs-fs/hubfs/image12.png?width=849&name=image12.png)

Once the vagrant file is created, start and _ssh_ into it to be in a completely active yet isolated OS. Now that the virtual machine and vagrant file are up and running, define and install everything you need for developing your application. &nbsp;In our example, we need to do some installations as below:

_install git_

![install git](https://blog.engineyard.com/hs-fs/hubfs/image10.png?width=847&name=image10.png)

_install curls_

_![install curls](https://blog.engineyard.com/hs-fs/hubfs/image6.png?width=848&name=image6.png)_

_load rvm_

_![load rvm](https://blog.engineyard.com/hs-fs/hubfs/image2-1.png?width=847&name=image2-1.png)_

&nbsp;_install ruby_

_![install ruby](https://blog.engineyard.com/hs-fs/hubfs/image13.png?width=848&name=image13.png)_

_set default ruby version_

![set default ruby version](https://blog.engineyard.com/hs-fs/hubfs/image14.png?width=845&name=image14.png)

&nbsp;verify ruby version

![verify ruby version](https://blog.engineyard.com/hs-fs/hubfs/image3-1.png?width=844&name=image3-1.png)

_install and check your rails version_

_![install and check your rails version1](https://blog.engineyard.com/hs-fs/hubfs/image8.png?width=846&name=image8.png)_

![install and check your rails version2](https://blog.engineyard.com/hs-fs/hubfs/image9-1.png?width=846&name=image9-1.png)

_install bundler_

_![install bundler](https://blog.engineyard.com/hs-fs/hubfs/image4-1.png?width=846&name=image4-1.png)_

_bundle your gems_

_![bundle your gems](https://blog.engineyard.com/hs-fs/hubfs/image1-1.png?width=847&name=image1-1.png)_

&nbsp;_install nodejs_

_![install nodejs](https://blog.engineyard.com/hs-fs/hubfs/image7.png?width=849&name=image7.png)_

&nbsp;_install your database_.

![install your database.](https://blog.engineyard.com/hs-fs/hubfs/image5-1.png?width=1950&name=image5-1.png)

Make sure to have a properly aligned [database.yml file](https://circleci.com/blog/what-is-yaml-a-beginner-s-guide/) and set the database names in this file. The alignment of this _yml_ file is very important because it can be read wrongly otherwise. Another important factor to consider is the database names. The database names in this _yml_ file should be the database names of your rails application.

Finally, the vagrant environment configuration is complete, and now create your application from scratch.

## Creating the application

Creating an application from scratch is done in your local machine and not in a vagrant.&nbsp;

&nbsp;In your local machine, open a new tab in the same directory as your vagrant file.&nbsp;

- Now, to create a new application, run:

_rails new your\_app\_name database = postgresql_

- The application is now created. Configure the database with names and permissions, as discussed above.
- Open the terminal where the vagrant is running.&nbsp;
- Move into your synced directory, which is “_/vagrant\_files”_ in our example.&nbsp;
- Move into the new app’s directory.
- Run ‘_bundle install_’.
- Run ‘_rails s’_ and your app should be up and running at the port or URL as per your definition.

## Using the existing application

Open a new terminal in the directory of your vagrant file. Bring the code of your existing app from GitHub.

![clone-existing-app](https://blog.engineyard.com/hs-fs/hubfs/image11.png?width=849&name=image11.png)

Now, update the database with proper names.

- Open the terminal where the vagrant is running.&nbsp;
- Move into your synced directory, which is “/vagrant\_files” in our example.&nbsp;
- Move into the new app’s directory.
- Run ‘_bundle install_’.

Run ‘_rails s’_ and your app should be up and running at the port or URL as per your definition.

## Conclusion

At first, the process of creating and setting up a virtual machine and a vagrant box will be tiring. But once tuned in, there is no going back.

There are options to use existing vagrant boxes and also use packers instead of manually doing all the setup. But either way, it is always better to know the logic and working of any predefined schemes for better understanding and easy troubleshooting. So, try out your versions in an above-discussed manner before creating your app. Once you get into the detailed flow, you can skip the basic steps by utilizing predefined packers and move up the ladder for further challenges.&nbsp;

&nbsp;There are chances to replace virtual machines with Docker containers as well. Want to know more about virtual machines and Dockers? Check out our blog post - [Docker Vs Virtual Machines Explained](https://blog.engineyard.com/docker-vs-virtual-machines-explained).

To deploy and start your Ruby apps on Engine Yard, start your [free trial](https://signup.ey.io/?_ga=2.953649.1268158655.1630289997-1200590696.1626847843) now.



**Credits:**

_Images source:_ [_DEV Community_](https://dev.to/denisepen/setting-up-vagrant-for-a-rails-application-kgc)