Building a Vagrant Box: Setting up your Environment

Facebook
Twitter
LinkedIn

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. 

Creating the vagrant box

Download VirtualBoxand 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. 

So, the code would be:

 mkdir vagrant_demo      --create directory

 cd vagrant_demo         --enter into the directory

 vagrant init ubuntu/trusty64    --initiate vagrant operating system

A VagrantFile, 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. 

 -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 a 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

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.  In our example, we need to do some installations as below:

You might also like:   Services, the Missing Element in Rails Applications: A Discussion with Riaz Virani

install git

install git

install curls

install curls

load rvm

load rvm

 install ruby

install ruby

set default ruby version

set default ruby version

 verify ruby version

 verify ruby version

install and check your rails version

install and check your rails version1

install and check your rails version2

install bundler

install bundler

bundle your gems

bundle your gems

 install nodejs

 install nodejs

 install your database.

 install your database.

Make sure to have a properly aligned database.yml fileand 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.

Scale performance. Not price. Try Engine Yard today and enjoy our great support and huge scaling potential for 14 days.
Deploy your app for free with Engine Yard.

Creating the application

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

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

  • 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. 
  • Move into your synced directory, which is “/vagrant_filesin our example. 
  • Move into the new app’s directory.
  • Run ‘bundle install’.
  • Run ‘rails sand 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.

You might also like:   Rails encrypted credentials on 6.2

clone-existing-app

Now, update the database with proper names.

  • Open the terminal where the vagrant is running. 
  • Move into your synced directory, which is “/vagrant_files” in our example. 
  • Move into the new app’s directory.
  • Run ‘bundle install’.
  • Run ‘rails sand 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. 

 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.

To deploy and start your Ruby apps on Engine Yard, start your free trialnow.

 

Credits: 

Images source: DEV Community

Want more posts like this?

What you should do now:

Facebook
Twitter
LinkedIn

Easy Application Deployment to AWS

Focus on development, not on managing infrastructure

Deploying, running and managing your Ruby on Rails app is taking away precious resources? Engine Yard takes the operational overhead out of the equation, so you can keep innovating.

  • Fully-managed Ruby DevOps
  • Easy to use, Git Push deployment
  • Auto scaling, boost performance
  • Private, fully-configured Kubernetes cluster
  • Linear pricing that scales, no surprises
  • Decades of Ruby and AWS experience

14 day trial. No credit card required.

Sign Up for Engine Yard

14 day trial. No credit card required.

Book a Demo