DEV Community

Cover image for Install Virtual Machine With Vagrant & Remotely Access it
ChigozieCO
ChigozieCO

Posted on

Install Virtual Machine With Vagrant & Remotely Access it

For this walk through I will be installing a new VM using Vagrant and then remotely accessing the VM using SSH with Vagrant.

What is Vagrant

Installed as a layer of software between a virtualization tool (like VirtualBox, Docker, or Hyper-V) and a virtual machine (VM), Vagrant is an open-source program that makes it simple to create, configure, and manage boxes of virtual machines through an intuitive command interface.

Why is Vagrant Useful

The process of setting up and maintaining virtual machines can be laborious and time-consuming. Even more difficult is replicating the virtual machine on an alternate server as well as if you have to replicate more than one virtual machine (VM).

Vagrant is an effective tool that can make setting up and maintaining your development environment easier. Owing to the fact that it writes your configuration as code, it makes it possible for teams (and every other VM you create with your VagrantFile) have the same virtual machine setup across board.

For a basic cheat sheet of every day vagrant commands that you will use in your day to day operations with vagrant, check out this post.

Install a Hypervisor and Vagrant

🌟 Before you begin, ensure you have a hypervisor such as Oracle Virtual box or VMWare. For this tutorial I will be making use of Oracle Virtual box, you can download it from here.

🌟 To install a VM with Vagrant, first install vagrant in your computer, get it from vagrantup.com

🌟 Vagrant is automatically added to your env path so to check if the installation was successful, launch your terminal and enter the following command to output the installed version number:

vagrant --version
Enter fullscreen mode Exit fullscreen mode

🌟 Once it's installed find the VM (box) you want to install on vagrant cloud and note the name.

🌟 For this installation I will be using ubuntu 20.04-LTS and we can see from vagrant cloud that it is called focal/64.

Vagrant cloud

Initialize Vagrant

🌟 The next thing to do is to initialize vagrant in other to create the Vagrantfile that would be used for our configuration.

🌟 For each box I create with vagrant I create a specific directory for that box to keep things organised, that way the Vagrantfile (which is the configuration) of that box is stored in that directory and so before we initialize vagrant we will create the necessary directories.


mkdir -p vagrant/boxes/ubuntu20.04-LTS
cd vagrant/boxes/ubuntu20.04-LTS
Enter fullscreen mode Exit fullscreen mode

Tip
the p flag will make parent directories as needed and will not result in any error if already existing.

vagrant directory

🌟 Now we can go ahead and initialize vagrant by running the command below

vagrant init <box name>

vagrant init ubuntu/focal64
Enter fullscreen mode Exit fullscreen mode

Initialize vagrant

🌟 Your Vagrantfile is now ready to be used to spin up your Virtual machine but before we do this I want to make some changes to our VM configuration in the Vagrantfile. Be sure to read the Vagrantfile so you understand the configurations of your VM.

Edit the Virtual Machine Configuration.

I want my VM to use a private network where the IP will be assigned via DHCP and I also want to configure a shared folder between the Host OS and the Guest OS.

These are the changes we will be making to the Vagrantfile.

🌟 Using any editor of your choice, open the Vagrant file. I will be making use of vim.

vi Vagrantfile
Enter fullscreen mode Exit fullscreen mode

🌟 Scroll to the part of the file that says Create a private network, which allows host-only access to the machine as shown in the image below.

vagrantfile

This is what the part looks like and the line highlighted is the line we will change.

🌟 First, uncomment it and edit it as shown below:

  config.vm.network "private_network", type: "dhcp"
Enter fullscreen mode Exit fullscreen mode

This is what it should now look like:

Edited Vagrantfile

🌟 The next change we will make is to add a shared folder between the host OS and the guest OS. Scroll down in the file and find where it says Share an additional folder to the guest VM. The first argument is

vagrantfile snippet

Uncomment the highlighted line and choose a host folder of your choice, I want it in the same directory I am working on and so I will just make a slight change as can be seen below:

  config.vm.synced_folder "./data", "/vagrant_data"
Enter fullscreen mode Exit fullscreen mode

Edited vagrantfile snippet

Note: Ensure that the specified host directory already exist if not you will get an error message saying There are errors in the configuration of this machine. Please fix the following errors and try again:

vm:
* The host path of the shared folder is missing: ./data

Save the Vagrantfile and we are ready to spin our VM up.

Create the VM

🌟 After saving the Vangrantfile configuration changes we made, we are ready to run the command we need to create our VM and we do this with the command below:

vagrant up
Enter fullscreen mode Exit fullscreen mode

Note that the above command assumes that you are using Oracle virtual box as your hypervisor. If your were using VMware or any other virtualization tool the command to use is:

vagrant up <hypervisor>

vagrant up VMware
Enter fullscreen mode Exit fullscreen mode

The above command will first download the ubuntu/focal64 VM image (or whichever image you selected) from the vagrant cloud and then it will start the VM.

Also, it will generate an SSH key pair and adds the public key to the VM during this process so that we can SSH into the machine once it is up and running.

SSH Key

If you get this error displayed in the screenshot below, follow the next set of steps to correct the error.

DHCP Error

Fix DHCP Error

🌟 Head over to your virtual box (hypervisor), launch it.
🌟 Select the VM that Vagrant just created and on the top left hand click on file.
🌟 Click tools and select network manager.

Virtualbox interface

🌟 Select the host-only network, either right click on it and click on remove or select it and then at the top right click on remove and remove when asked for confirmation.

Virtualbox host only network

🌟 When this is done go back to the terminal you are setting up your vagrant VM with and enter the below command.

vagrant reload
Enter fullscreen mode Exit fullscreen mode

The configuration of your VM should complete successfully now.

VM installed

SSH into the Machine

🌟 After the setup is complete you can now ssh into the machine:

vagrant ssh
Enter fullscreen mode Exit fullscreen mode

vagrant ssh

Out of date Guest Additions

If you see the message saying your guest additions are out of date, as seen in the image below, and you're like me that can't let things just be. Here's how to update your guest additions.

Out of date Guest Additions

🌟 If you had previously SSH into the VM, exit the remote access:

exit
Enter fullscreen mode Exit fullscreen mode

🌟 Run the vagrant vb-guest plugin

vagrant plugin install vagrant-vbguest
Enter fullscreen mode Exit fullscreen mode

🌟 Next halt your VM by running the command:

vagrant halt
Enter fullscreen mode Exit fullscreen mode

🌟 Then run the vagrant up command. You will see a lot of installations happening, allow it run its full course.

🌟 Repeat the two commands again if need be:

vagrant halt
vagrant up
Enter fullscreen mode Exit fullscreen mode

🌟 You should now see a message saying GuestAdditions <version> running -- OK

Up to date Guest Additions

🌟 You are good to go now, SSH into your machine and start enjoying tinkering around the Linux OS.

Top comments (0)