INFO
More info on post-processors can be found here: https://www.packer.io/plugins/post-processors/vagrant/vagrant#virtualbox.
- As you can see from the preceding screenshot, we’re going to create a new folder called scripts and place the shell script in there:
└── ol8-vagrant
├── http
│ └── ol8-ks.ks
├── scripts
│ └── vagrant-base-box.sh
└── vagrant-ol8.pkr.hcl
- Now, in the vagrant-base-box.sh script, we’ll add this:

Figure 8.39 – Contents of the vagrant-base-box.sh script
- Next, we’re going to run packer init to download the external plugin:
$ packer init .
- Now we’re ready to build the Vagrant box. This part is simple, just run the following:
$ packer build .
After entering the command, you will see something like the following:

Figure 8.40 – Output of the “packer build” command
This will take a while, because it’s actually doing quite a bit here. Remember what we talked about from a high level: Packer is going to download the ISO, create a VM, and install the OS, and afterward it will run everything in the script you just defined, but once all of this is done, you will be left with a nice neat .box file (which is exactly what we need to for use with Vagrant).
If you switch over to VirtualBox, you’ll eventually see a new VM that was created by Packer. You can just leave it be and let Packer do its thing, but knowing this detail gives you the ability to know what’s going on behind the scenes.

Figure 8.41 – Screenshot of VM being built and configured with Packer
Once the build is complete, you should see output similar to the following:

Figure 8.42 – Screenshot of completed build of Vagrant box
NOTE
The Oracle Linux ISO file is several gigabytes, so this really could take a while. Now may be the perfect time to take a break and have a coffee. 😉
- Now it’s time to prepare our box for use with Vagrant. We’ll just run vagrant init to build our Vagrantfile:
$ vagrant init ol8-x64-virtualbox.box
The output is as follows:

Figure 8.43 – Initializing Vagrant box
HELPFUL TIP
You could run vagrant init and vagrant init . , but it’s best to specify the filename of the box. This way, Vagrant will automatically specify the proper value of the box inside the Vagrantfile. That’s why for this recipe, we are running vagrant init ol8-x64-virtualbox.box.
- Finally, it’s time to test out our new Vagrant box by running vagrant up:
$ vagrant up
The output of this command is shown in the following screenshot:

Figure 8.44 – Output of the “vagrant up” command
And there you have it. After a few short moments, your virtual machine will be up and running, and you can interact with it using the convenient commands made possible by Vagrant.
The source code for this recipe can be found at https://github.com/PacktPublishing/Oracle-Linux-Cookbook/tree/main/ch8/packer-vagrant.
IMPORTANT NOTE
If you’re still developing your Vagrant box and need to test after each build, you’ll want to make sure you’re testing the latest box that you’ve built. I actually ran into an issue thinking that my changes weren’t persisting until I realized Vagrant was using a cached box from a previous build. You can remove old boxes with vagrant box remove ol8-x64-virtualbox.box.