Home Vagrant How to Create a Vagrant Box from an Existing Box

How to Create a Vagrant Box from an Existing Box

We have crossed halfway through this series and by this time you might have a good understanding of what Vagrant does and how to use provisioners in vagrant.

Till now you are using prebuilt vagrant boxes downloaded from the vagrant cloud site. Some boxes are preconfigured to serve different purposes like the scotch box which comes with a LAMP stack, trusty64cdh which comes with a single node CDH Hadoop distribution. You can also create your box like the one mentioned and share it with the community or fellow geeks working with you on the same project.

Here there are two ways you can create a vagrant box. You can choose any hypervisor and manually install guest os and harden the os then package it to box format. Alternatively, you can download boxes already available in the vagrant cloud, customize them according to your requirement, and repack it to box format. This is quite easy compared to building the box from scratch.

Creating a New Vagrant Box

1. Choose the box which will be used. In my case, I am downloading the ubuntu/focal64 box. Create a new directory and run the vagrant init command.

$ mkdir ubuntu
$ cd rebuild
$ vagrant init -m "ubuntu/focal64"

2. Bring up the virtual machine by running the vagrant init command.

$ vagrant init

3. Connect to the guest virtual machine using:

$ vagrant ssh

Enable password-based authentication if needed, by default it is disabled.

$ sudo sed -i "/^[^#]*PasswordAuthentication[[:space:]]no/c\PasswordAuthentication yes" /etc/ssh/sshd_config
$ sudo service sshd restart

4. I want to install PostgreSQL on this virtual machine and rebox it.

$ sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
$ wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
$ sudo apt-get update
$ sudo apt-get -y install postgresql

Switch as a Postgres user and type psql to connect to the shell. Now if everything goes well installation is successful.

$ sudo su - postgres
$ psql

5. Before repacking the box we have to zero out the underlying drive to achieve better compression.

$ sudo dd if=/dev/zero of=/EMPTY bs=1M
$ sudo rm -f /EMPTY

Create a New Vagrant Box using Existing Vagrant Box

6. Create a new box from the existing virtual machine by running the following command.

$ vagrant package --output ubuntu_repacked.box
Create New Vagrant Box
Create New Vagrant Box

7. Take a look at the above image where you can see a new box is created “ubuntu_repacked.box”. Now, this box needs to be added to the vagrant to import.

$ vagrant box add repacked ubuntu_repacked.box
Add Box to Vagrant
Add Box to Vagrant

8. Now I can use this box and spin up a new guest virtual machine. Create a new project directory and do a vagrant init.

$ mkdir repacked
$ cd repacked
$ vagrant init -m "repacked"
$ vagrant up
Importing Repacked Vagrant Box
Importing Repacked Vagrant Box

From the above image, you can see it is importing the repacked box we created. Connect to the virtual machine and run the psql command to work with PostgreSQL.

$ vagrant ssh
$ sudo su - postgres
$ psql

We have come to the end of this article. I have shown you how to create a box from the existing vagrant box. In this example, I have shown you how to install PostgreSQL in the vagrant box. The real-time use case can be anything and this process will be very handy.

Ravi Saive
I am an Experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies. Founder of TecMint.com, LinuxShellTips.com, and Fossmint.com. Over 150+ million people visited my websites.

Each tutorial at UbuntuMint is created by a team of experienced writers so that it meets our high-quality standards.

Was this article helpful? Please add a comment to show your appreciation and support.

Got something to say? Join the discussion.

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.