📜 ⬆️ ⬇️

Creating a new virtual machine in one minute or “vagrant up!”

Vagrant
Those who have previously encountered the creation of virtual machines, imagine the process and can evaluate it over time. You need to create the virtual machine itself, specify the necessary parameters for it (size and type of disk, amount of RAM), then connect the installation disk with the operating system (whether it is a real optical drive or an ISO image), wait for the download, start the installation, wait for it to finish , disable the installation disk and reboot. All this may take about 20 minutes - in some cases less, in some more, but still it is a lot of time. And if the virtual machine is deleted for some reason, then all this will have to be done anew.

Vagrant is a way to significantly simplify and speed up the creation of new virtual machines. Instead of the installation disk, a special box-file is used, which is automatically deployed to a new virtual environment. After that, ports can be redirected from the network interface of the virtual machine, an IP address can be allocated, and the necessary software can be installed and configured. All this is indicated by the Vagrant settings, which are described in detail in the documentation .

Vagrant runs on Linux, Mac OS X, Solaris, and Windows.
')

To install Vagrant, you need the RubyGems package manager.

You can install RubyGems like this.

wget -c http://production.cf.rubygems.org/rubygems/rubygems-1.5.0.tgz
tar xvzf rubygems-1.5.0.tgz
cd rubygems-1.5.0
sudo ruby ​​setup.rb
sudo ln -s /usr/bin/gem1.8 / usr / bin / gem

Update RubyGems. It should be done both after a fresh installation, and in case it has already been installed.

sudo gem update --system

Now install Vagrant. There should be warned about the features of different versions . The fact is that the 0.6 branch only supports VirtualBox 3.2, and the new 0.7 branch only supports VirtualBox 4.0, without backward compatibility.

So, if you have VirtualBox 3.2:
gem install --version '= 0.6.9' vagrant

And if the new VirtualBox, then just install the latest version:
gem install vagrant

Now you need to download the box-file. Download Ubuntu 10.04 Lucid Lynx - 32 bits , 64 bits .

And add this box-file.

vagrant box add lucid64 lucid64.box

In principle, this command can accept a link to it instead of the path to the file, but I think that it’s safer to keep the box-file in oneself.

Now we will create a new directory for the Vagrant virtual environment and create a Vagrantfile configuration file there.

mkdir my_virtual_env
cd my_virtual_env
vagrant init

Edit the Vagrantfile, replacing
config.vm.box = "base"
on
config.vm.box = "lucid64"

By the way, if you plan to use only one box-file, then when you add it, you can simply specify the name base as the name (vagrant box add base lucid64.box). In this case, such editing of Vagrantfile is not required at all.

Now we will start the virtual machine.

vagrant up

The first time it takes a little more time, because the virtual environment is deployed, and then it takes less time, because you just need to load the virtual machine.

Now we can connect to the virtual machine.

vagrant ssh

If you need to execute commands as the superuser, sudo su is enough (no password is required).

The status of virtual machines can be viewed using the vagrant status command.

The virtual machine can be placed in standby mode. In this case, the resumption of work occurs very quickly. To do this, use the vagrant suspend command (resuming work - vagrant up).

You can turn off the virtual machine using vagrant halt. And if the virtual machine is no longer required, then you can delete it (along with the disk) with the vagrant destroy command.

This is enough to create new virtual machines and use them. Of course, there are still many possibilities, including creating your own box-files, as well as automatically installing and configuring software - and you can read about it in the project documentation.

It's great that many things can be done faster and easier than before. And it's great that you can do them in exactly the way that you like and enjoy the most.

Source: https://habr.com/ru/post/113354/


All Articles