Setting Up A Virtual Machine

Liselle Logan
6 min readOct 13, 2021

Featuring Vagrant and VirtualBox

Photo by Kevin Horvat on Unsplash

I was recently given the task to set up a project on a virtual machine. Now some of you may be wondering, what is a virtual machine and why would we want to use it? These are the same questions I asked myself! In this post, I will be going through how I was able to successfully set up my Rails application, Food for Fido, on a virtual machine via Vagrant and VirtualBox (don’t worry, I’ll explain these too).

What is a Virtual Machine?

A Virtual Machine (VM) is actually very similar to your local system in the sense that it has memory to store your files and if necessary, has the ability to connect to the internet. The main difference between a VM and your local system is that your local system has more tangible hardware that makes up your computer, as a VM is thought of like a virtual computer with its “hardware” made up of code.

Why a VM?

Now that we know that VMs perform like a virtual computer with operating systems and applications, VMs have the ability to remain completely separate from one another and the local host machine. VMs have a monitoring software called hypervisor that creates and runs VMs. These hypervisors allow you to use your local host computer to support multiple VMs by sharing its resources virtually. Since VMs are independent of one another, they have portability. Due to this flexibility, you are able to even move a VM to a different machine by moving it to another hypervisor. There are various benefits of VMs such as cost savings, decreasing downtime, scalability, security, etc. However, the biggest selling point for me was speed. Since you are running things virtually, it is a lot more efficient to quickly test and run your application since it is not relying on the type of hardware you have. Efficiency is something that developers and their clients always strive for. I’m going to show you how to set up a simple Ruby on Rails application so that we can focus on getting used to this environment!

Vagrant and VirtualBox

Vagrant is a tool to create and manage virtual environments. It provides the ability to efficiently reproduce and configure portable work environments. VirtualBox is a virtualization platform that allows you to use your local operating system in a virtual environment. Together, they allow you to simulate your application’s production environment.

Installation

Before installation, it’s best practice to create a directory on your local computer to serve as the root of your vagrant environment. This folder will also be used to hold the Rails application so that we can share a directory between the Vagrantfile and your Rails project. When you create a shared directory, it allows you to automatically sync changes from your local environment to VirtualBox. For installation instructions see the Vagrant and VirtualBox documentation. Make sure that you have the correct version of each that correlates with the other.

Vagrantfile Configuration for Ruby on Rails

Once installed on your computer, you will run the following commands to fire up your vagrant environment:

To initialize:

$ vagrant init ubuntu/trusty64

I used ubuntu/trusty64 as it is the most popular VagrantBox. You can take a look at a list of Vagrant boxes here.

If initialize was successful, you will see this in your terminal:

When you initialize Vagrant, you create a Vagrantfile that comes with its configuration code and can be modified to fit your particular application. Before we run vagrant up we need to edit our Vagrantfile to set up an environment that can efficiently run the Rails application.

Your Vagrantfile should look like this:

Vagrant.configure(“2”) do |config|
config.vm.box = “ubuntu/trusty64”
config.vm.network “forwarded_port”, guest: 3000, host: 3000
config.vm.synced_folder “.”, “/vagrant_files”
config.vm.provider “virtualbox” do |vb|
vb.memory = “4096”
end
end

Since Rails applications default to port:3000, we want to make sure that our network setting is the same as well. The config.vm.synced_folder “.”, “/vagrant_files” is used to sync the local system with the VirtualBox, and vb.memory = “4096” is used to change the memory or space that the Rails application can use.

To start the virtual machine:

$ vagrant up

SSH into the machine to log in:

$ vagrant ssh

Once you’ve ssh’d into the vagrant environment, you should see “Welcome to Ubuntu-”:

Your VM is now all set up and ready to host your Rails application! This step in the process is the easier part of the setup as long as you have played around with the environment using cd and ls to navigate your way through the VM.

Adding Your Rails Application To Vagrant

You need to install the necessary packages and gems that the Rails application uses:

curl to fetch files

$ \curl -sSL https://get.rvm.io | bash

When I was running this command in my terminal, I came across a certification issue since the SSL certificate expired recently. So in order to address this, I researched and found that when I disabled the certificate on my server by running sudo dpkg-reconfigure ca-certificates and deselected ​​mozilla/DST_Root_CA_X3.crt I was able to successfully install curl.

As per the post-installation instructions of curl, you need to run the following command to load Ruby Version Manager (RVM) into the current environment:

$ source /home/vagrant/.rvm/scripts/rvm

To efficiently install the various packages and compilers necessary to build Ruby and its libraries, run the command:

$ rvm requirements

In RVM, you can see the list of Rubies by running this command:

$ rvm list known

To install Ruby:

$ rvm install 2.6

To set this version of Ruby as default:

$ rvm use 2.6 — default

It is always best practice to test to make sure that your installations were successful. So to test if Ruby was successfully installed, run the command:

$ which ruby
/home/vagrant/.rvm/rubies/ruby-2.6.8/bin/ruby
$ ruby -v
ruby 2.6.8p205 (2021–07–07 revision 67951) [x86_64-linux]

Install bundler

$ gem install bundler

NodeJS for JavaScript runtime that is needed for the Rails’ Asset Pipeline:

$ sudo apt-get install nodejs

Install Rails

$ gem install rails

After successfully installing all of the packages and gems necessary to host a Rails application, you can now clone your project into the vagrant root directory or where you have your shared file. Run bundle install and any migrations. Then start up your Rails server to test how well your application runs! These steps are quite tedious and took me a bit of time but they are necessary for your Rails application.

Keep Learning!

This was my first time ever using a VM and although it came with its challenges (as learning new things does), I’m grateful that I never gave up. Being a developer means that you basically live for failure. Now I don’t mean like your entire application breaking and burning down but you rely on errors to point you in the right direction. Before learning about VMs I have only known local environments. Today, I can proudly say that I would be comfortable using this in a professional setting. There were a lot of bugs (and I mean A LOT) but perseverance and hard work can really get you through obstacles. If I have learned anything from this experience, it taught me that believing in yourself is the greatest strength you have to get through the moments where you feel like you are hitting a wall (I have had too many of those). Try to implement one of your previous projects with a virtual machine and let me know your experience! If you get stuck, I’m always here to answer any questions you have. I believe in you and happy coding!

--

--