Install Virtual Machine With Vagrant & Remotely Access it
Tags: vagrant, cloud, devops, linux
Published: 2024-03-11 10:23:54

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](https://dev.to/chigozieco/vagrant-cli-commands-cheat-sheet-f7c).

## 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](https://www.virtualbox.org/).

🌟 To install a VM with Vagrant, first install vagrant in your computer, get it from [vagrantup.com](https://developer.hashicorp.com/vagrant/install?product_intent=vagrant)

🌟 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:

```sh
vagrant --version
```

🌟 Once it's installed find the VM (box) you want to install on [vagrant cloud](https://app.vagrantup.com/boxes/search) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2uauulrk22znpphpc73o.png)

## 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.

```sh

mkdir -p vagrant/boxes/ubuntu20.04-LTS
cd vagrant/boxes/ubuntu20.04-LTS
```

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

![vagrant directory](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rklzfxmdi2cg0bvuz4bu.png)

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

```sh
vagrant init <box name>

vagrant init ubuntu/focal64
```

![Initialize vagrant](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ttd8aqebvup8yjf9u8p1.png)

🌟 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.

```sh
vi Vagrantfile
```

🌟 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xzej6ksr93q2ytopm8bk.png)

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:

```sh
  config.vm.network "private_network", type: "dhcp"
```

This is what it should now look like:

![Edited Vagrantfile](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t7z9qn2jjcz62ss0qe5k.png)

🌟 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ykjwkooi6kje39eyb8e.png)

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:

```sh
  config.vm.synced_folder "./data", "/vagrant_data"
```

![Edited vagrantfile snippet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k78b996imbpwpmby4atd.png)

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

```sh
vagrant up
```

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

```sh
vagrant up <hypervisor>

vagrant up VMware
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pwls0eo7sxzta23t9itx.png)

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

![DHCP Error](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r7ajqas65f9kmhuithd8.png)

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kfmwbqk5aom24fpzw0ob.png)

🌟 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1gwv92b08qiid82zmxze.png)

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

```sh
vagrant reload
```

The configuration of your VM should complete successfully now.

![VM installed](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j6rogux27xu1vj57uhlk.png)

## SSH into the Machine

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

```sh
vagrant ssh
```

![vagrant ssh](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uzk0ax93bnpr2fdurwsr.png)

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0gcjzbxa3tslta1c9tkd.png)

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

```sh
exit
```

🌟 Run the vagrant vb-guest plugin

```sh 
vagrant plugin install vagrant-vbguest
```

🌟 Next halt your VM by running the command:

```sh
vagrant halt
```

🌟 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:

```sh
vagrant halt
vagrant up
```

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

![Up to date Guest Additions](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6x6f0dxxtjqp7b8gt49f.png)

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