Home > Backend Development > PHP Tutorial > How to Create and Share a Vagrant Base Box

How to Create and Share a Vagrant Base Box

尊渡假赌尊渡假赌尊渡假赌
Release: 2025-02-20 09:51:10
Original
344 people have browsed it

Every new tools appear every day to help developers become more efficient and allow them to focus on the actual business value of their projects.

Vagrant is such a tool that is becoming one of the most powerful assistants for developers, which standardizes the way development environments are created and managed.

This article will teach you how to create your own Vagrant basic image based on Ubuntu 14.04 LTS, configure it with your favorite app, and share it with others via Vagrant Cloud.

Key Points

  • To create a Vagrant base image, you need to install VirtualBox and Vagrant on your machine, and then use VirtualBox to create a new virtual machine. Install the necessary software and configurations and use the vagrant package command to package them into a mirror file.
  • Vagrant Basic Mirror is a pre-packaged environment that can be used as a starting point for a project. It ensures consistency between different development environments and saves time by eliminating the need to set everything manually from scratch.
  • The Vagrant basic image can be shared by uploading it to a mirror hosting service such as Vagrant Cloud. Once uploaded, the mirror URL can be shared with others, which they can add to their Vagrant environment using the vagrant box add command.
  • Vagrant basic mirroring usually includes the operating system, necessary software (such as a web server or database server), and any configuration required by the development environment. However, the content can be adjusted according to the needs of a specific project.

Create a virtual machine

Note: The following example requires that Vagrant be installed at least 1.5 version on the local computer.

To create a virtual machine, you need a virtualization provider. Although Vagrant does not discriminate against which provider to use, the following example will use VirtualBox.

Before creating a virtual machine, you need to install VirtualBox first.

Next, go to the Ubuntu download page and get the Ubuntu 14.04 LTS ISO image.

How to Create and Share a Vagrant Base Box

About LTS Version

A new Ubuntu version is released every six months, and support for each version is limited.

LTS stands for long-term support, and it is given a specific subset of Ubuntu version. The LTS spans up to five years. If you install the LTS version today, you will get an update in five years.

For web development, it is best to always use Ubuntu with LTS tags, the good news is that Ubuntu 14.04 LTS was released on April 17, 2014, so support continues until 2019.


Install Ubuntu

After the ISO file is downloaded, start VirtualBox and click New to create a new virtual machine, and select Linux as type, Ubuntu (64-bit) as version , and name your virtual machine.

How to Create and Share a Vagrant Base Box

Click Continue and set the memory size to 1024 MB or higher (if you think memory-intensive tasks will be run in the virtual machine).

In the next panel, select Create virtual hard disk now, and then select VMDK (Virtual Machine Disk).

In the Physical Hard Drive Storage panel, select Dynamic Allocation and set at least 8 GB. Click Continue and your virtual machine will be almost finished.

You need to insert the downloaded Ubuntu 14.04 LTS ISO file as a virtual CD to install the operating system when starting the virtual machine. It's like you plugged a bootable CD into a CD-ROM.

Select the virtual machine from the list, click Set, and then click the Storage tab. On Controller: IDE, click the small CD icon (highlighted in red in the image below), and then click to select disk and select the Ubuntu ISO file.

How to Create and Share a Vagrant Base Box

Now we are ready to start the virtual machine and install Ubuntu as the guest operating system. There are several steps in the installation process, such as selecting language, country/region, etc. I will introduce more important steps below.

  • When prompted for a host name, type vagrant
  • For username and password, also type vagrant
  • Encrypt your home directory? Select No
  • On the Time Zone panel, select UTC or your own preferred time zone
  • Parallel Method: Boot-Use the entire disk and set up LVM
  • When prompted to install which software, select OpenSSH server, and the remaining software (such as LAMP or MySQL) will be installed later
  • Select to install the GRUB boot loader on the main boot record

This should summarize the installation process and you will be prompted to log in next. Log in with vagrant user and password.

How to Create and Share a Vagrant Base Box

Next, switch to root user by typing the following command:

<code>sudo su -</code>
Copy after login
Copy after login
Copy after login

Please note that this is not a very secure setting, and protecting Ubuntu is not within the scope of this article. Safety is not important for developing machines.

Using root user, update to the latest software package by typing the following command

<code>apt-get update
apt-get upgrade</code>
Copy after login
Copy after login
Copy after login

Configure virtual machines for Vagrant

Vagrant requires setting some specific options to work well with the virtual machine.

Execute command without password

All operations performed by Vagrant will be used with the

vagrant user, and all sudo commands need to be executed without a password every time they are run.

To set this, we need to run the

visudo command to configure the user with no password. Just type:

<code>visudo</code>
Copy after login
Copy after login
Copy after login
Add the following line to the end of the file and save:

<code>vagrant ALL=(ALL) NOPASSWD:ALL</code>
Copy after login
Copy after login
Copy after login
Root Password

The general rule when creating a Vagrant image is to set the root password to a well-known password. Vagrant uses vagrant as password. To set this, type the following command as root and type your password when prompted:

<code>sudo su -</code>
Copy after login
Copy after login
Copy after login

SSH configuration

To enable Vagrant to SSH to a virtual machine, it uses public key authentication. We need to set this for our vagrant users.

Still log in as root user, go to vagrant user's home directory:

<code>apt-get update
apt-get upgrade</code>
Copy after login
Copy after login
Copy after login

Create the following folder:

<code>visudo</code>
Copy after login
Copy after login
Copy after login

Vagrant Connect to the virtual machine using an insecure key pair, you can download it here: github.com/mitchellh/vagrant/blob/master/keys/vagrant.pub

Simply run the following command to get it directly:

<code>vagrant ALL=(ALL) NOPASSWD:ALL</code>
Copy after login
Copy after login
Copy after login

OpenSSH has very strict permissions for this folder and file, so let's change it to the correct permissions:

<code>passwd</code>
Copy after login

If everything goes well, this is the folder structure of /home/vagrant and /home/vagrant/.ssh , including permissions.

How to Create and Share a Vagrant Base Box

Optional steps: In order to speed up the SSH connection speed to the virtual machine, we need to modify the SSH server configuration:

<code>cd /home/vagrant</code>
Copy after login

Add this line to the end of the file and save:

<code>mkdir .ssh</code>
Copy after login

Then restart the SSH server:

<code>wget https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub -O .ssh/authorized_keys</code>
Copy after login

Finally, we can log out of the root user by typing the following command:

<code>chmod 700 .ssh
chmod 600 .ssh/authorized_keys
chown -R vagrant:vagrant .ssh</code>
Copy after login

Installing VirtualBox Client Add-ons

In order for Vagrant to correctly share folders between the client and host operating system, the VirtualBox Client Add-on is required.

The prerequisite for installing client add-ons on a Linux system are software packages such as Linux headers and development tools. Let's install them first by typing the following:

<code>cd /etc/ssh
pico sshd_config</code>
Copy after login
Next, we need to make sure that the client add-on image is inserted into the virtual CD-ROM of the virtual machine. To do this, please follow the window of the virtual machine and select

Device -> Insert the client add-on CD image .

How to Create and Share a Vagrant Base Box

You can verify that the image is inserted into VirtualBox by selecting the running virtual machine from the list and clicking

Settings -> Storage . You should see VBoxGuestAdditions.iso under Controller: IDE.

How to Create and Share a Vagrant Base Box

Next, we need to mount the CD and run the actual installation shell script by running the following command:

<code>UseDNS no</code>
Copy after login
After this operation is complete, it is best to restart the virtual machine to ensure that the new settings take effect:

<code>sudo su -</code>
Copy after login
Copy after login
Copy after login

Create the basic image package

Now that we have created the basic virtual machine, we need to create the actual Vagrant base image. Simply type the following command in the terminal of your actual host machine (not the command line of your virtual machine):

<code>apt-get update
apt-get upgrade</code>
Copy after login
Copy after login
Copy after login

is the name of the virtual machine you specify in the VirtualBox settings. If you don't know what the name is, you can type the following command to find it:

<code>visudo</code>
Copy after login
Copy after login
Copy after login

In our case, it's called Ubuntu 14.04 x64 LTS Vagrant Base Box, and it also contains spaces, so make sure you enclose it in quotes like this:

<code>vagrant ALL=(ALL) NOPASSWD:ALL</code>
Copy after login
Copy after login
Copy after login

This process will generate a package.box file, which is an archive of the virtual machine and Vagrant metadata files.

Shared basic mirror

Vagrant Recently created a service called Vagrant Cloud where anyone can create and share Vagrant images for others to use.

To share our basic image on Vagrant Cloud, we first need to register. After logging in, you will see the dashboard below. Click the to create a mirror link in the upper right corner.

How to Create and Share a Vagrant Base Box

We need to assign a name and a short description to our image. You can enter anything here, I added ubuntu-trusty64 to our image. Trusty is the development code for Ubuntu 14.04 LTS.

How to Create and Share a Vagrant Base Box

Next, you need to create a version for the image, let's add 1.0.0 for the initial version number and click to create a version .

How to Create and Share a Vagrant Base Box

After creating the image and version, we need to add a provider. Click Create a new provider and type virtualbox, then click the URL button and type the URL where the package.box file is located.

Note: Only paid advanced users can upload the basic image directly to Vagrant Cloud. For more information, check out the pricing page.

After adding the provider, click the Publish now button to make the image available on Vagrant Cloud.

The Vagrant image used as an example in this article is available at https://www.php.cn/link/cb0d6d3459c57b7a5c968956b1ecf1e3. This is a perfect base image you can use for your development environment, but remember it is not safe and only used locally.

Conclusion

You learned how to create a VirtualBox virtual machine, install Ubuntu as a guest operating system, and how to package this VM for use with Vagrant.

Finally, we show you how to distribute your Vagrant basic image on Vagrant Cloud and share it publicly on the internet.

In a subsequent article in this article, you will learn how to use this Vagrant basic image to install your preferred software and set up your development environment using a combination of shell scripts and other configuration tools.

FAQ for Creating and Sharing Vagrant Basic Mirroring (FAQ)

What is a Vagrant basic mirror and why do I need it?

Vagrant Basic Mirroring is essentially a pre-packaged environment that you can use as a starting point for your project. It contains the operating system, pre-installed software, and the configuration required for the development environment. The main advantage of using Vagrant base mirroring is that it ensures consistency between different development environments, thus reducing the possibility of having a “workable on my machine” problem. It also saves time because you don't have to manually set everything from scratch.

How to create a Vagrant basic image?

Creating a Vagrant basic image involves multiple steps. First, you need to install VirtualBox and Vagrant on your machine. Then, you use VirtualBox to create a new virtual machine, install the necessary software and configurations, and use the vagrant package command to package it into a mirror file. This image file can then be used to initialize the new Vagrant environment.

How to share my Vagrant basic image with others?

You can share your Vagrant basic image by uploading it to a mirror hosting service like Vagrant Cloud. Once uploaded, you can share the mirror URL with others, and they can add it to their Vagrant environment using the vagrant box add command.

What should I include in my Vagrant base image?

What is included in your Vagrant base image depends on your project requirements. However, it usually includes an operating system, necessary software (such as a web server or database server), and any configuration required by the development environment.

How to update my Vagrant basic image?

To update your Vagrant base image, you need to make the necessary changes to the virtual machine, repackage it into a mirror file, and upload it to the image hosting service. You can then use the vagrant box update command to update the image in the Vagrant environment.

Can I use Vagrant basic mirroring for production environments?

While you can technically use Vagrant base mirroring for production environments, this is not recommended. Vagrant is primarily used to create reproducible development environments, and using them for production can lead to performance and security issues.

How to troubleshoot my Vagrant basic mirroring problem?

Troubleshooting issues with Vagrant base images may include checking Vagrant and VirtualBox logs, verifying image file integrity, and ensuring that your Vagrant and VirtualBox versions are compatible.

Can I use Vagrant basic mirrors with other virtualization software?

Yes, Vagrant supports several other virtualization providers besides VirtualBox, including VMware, Hyper-V, and Docker. However, you need to make sure the image files are compatible with the provider you are using.

How to optimize my Vagrant basic image?

Optimizing your Vagrant basic image may include reducing image file size, minimizing the number of pre-installed software, and using scripts to automate the image creation process.

Can I use Vagrant basic mirrors for cross-platform development?

Yes, one of the main advantages of using Vagrant basic mirroring is that it allows for cross-platform development. Since the image file contains a complete development environment, it can be used on any platform that supports Vagrant and selected virtualization providers.

The above is the detailed content of How to Create and Share a Vagrant Base Box. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template