Today I will install Vagrant which provides an easy way to configure reproducible and portable virtual environments.
It is just a first step towards this new direction, so stay tuned for more details soon.
Initial setup
Install VirtualBox
an x86 virtualization solution.
$ sudo apt-get install virtualbox
Download Vagrant
package for Debian
from downloads page.
Install downloaded package.
$ sudo dpkg -i vagrant_1.8.1_x86_64.deb
Verify installation process.
$ vagrant -v Vagrant 1.8.1
Download base box
Download Vanilla Debian base box
for VirtualBox
.
$ vagrant box add https://atlas.hashicorp.com/debian/boxes/jessie64/ --provider virtualbox
==> box: Loading metadata for box 'https://atlas.hashicorp.com/debian/boxes/jessie64/' ==> box: Adding box 'debian/jessie64' (v8.5.0) for provider: virtualbox box: Downloading: https://vagrantcloud.com/debian/boxes/jessie64/versions/8.5.0/providers/virtualbox.box ==> box: Successfully added box 'debian/jessie64' (v8.5.0) for 'virtualbox'!
List downloaded boxes.
$ vagrant box list
debian/jessie64 (virtualbox, 8.5.0)
Create and configure Vagrant environment
Create vagrant_learning
directory to store environment configuration.
$ mkdir vagrant_learning
Change working directory to vagrant_learning
.
$ cd vagrant_learning
Initialize default virtual environment.
vagrant_learning$ vagrant init debian/jessie64
A `Vagrantfile` has been placed in this directory. You are now ready to `vagrant up` your first virtual environment! Please read the comments in the Vagrantfile as well as documentation on `vagrantup.com` for more information on using Vagrant.
Inspect default configuration stored inside created Vagrantfile
.
# -*- mode: ruby -*- # vi: set ft=ruby : # All Vagrant configuration is done below. The "2" in Vagrant.configure # configures the configuration version (we support older styles for # backwards compatibility). Please don't change it unless you know what # you're doing. Vagrant.configure(2) do |config| # The most common configuration options are documented and commented below. # For a complete reference, please see the online documentation at # https://docs.vagrantup.com. # Every Vagrant development environment requires a box. You can search for # boxes at https://atlas.hashicorp.com/search. config.vm.box = "debian/jessie64" # Disable automatic box update checking. If you disable this, then # boxes will only be checked for updates when the user runs # `vagrant box outdated`. This is not recommended. # config.vm.box_check_update = false # Create a forwarded port mapping which allows access to a specific port # within the machine from a port on the host machine. In the example below, # accessing "localhost:8080" will access port 80 on the guest machine. # config.vm.network "forwarded_port", guest: 80, host: 8080 # Create a private network, which allows host-only access to the machine # using a specific IP. # config.vm.network "private_network", ip: "192.168.33.10" # Create a public network, which generally matched to bridged network. # Bridged networks make the machine appear as another physical device on # your network. # config.vm.network "public_network" # Share an additional folder to the guest VM. The first argument is # the path on the host to the actual folder. The second argument is # the path on the guest to mount the folder. And the optional third # argument is a set of non-required options. # config.vm.synced_folder "../data", "/vagrant_data" # Provider-specific configuration so you can fine-tune various # backing providers for Vagrant. These expose provider-specific options. # Example for VirtualBox: # # config.vm.provider "virtualbox" do |vb| # # Display the VirtualBox GUI when booting the machine # vb.gui = true # # # Customize the amount of memory on the VM: # vb.memory = "1024" # end # # View the documentation for the provider you are using for more # information on available options. # Define a Vagrant Push strategy for pushing to Atlas. Other push strategies # such as FTP and Heroku are also available. See the documentation at # https://docs.vagrantup.com/v2/push/atlas.html for more information. # config.push.define "atlas" do |push| # push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME" # end # Enable provisioning with a shell script. Additional provisioners such as # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the # documentation for more information about their specific syntax and use. # config.vm.provision "shell", inline: <<-SHELL # sudo apt-get update # sudo apt-get install -y apache2 # SHELL end
Create virtual environment
Create and start virtual environment.
vagrant_learning$ vagrant up
==> default: Importing base box 'debian/jessie64'... ==> default: Matching MAC address for NAT networking... ==> default: Checking if box 'debian/jessie64' is up to date... ==> default: Setting the name of the VM: vagrant_learning_default_1465340058365_37671 ==> default: Clearing any previously set network interfaces... ==> default: Preparing network interfaces based on configuration... default: Adapter 1: nat ==> default: Forwarding ports... default: 22 (guest) => 2222 (host) (adapter 1) ==> default: Booting VM... ==> default: Waiting for machine to boot. This may take a few minutes... default: SSH address: 127.0.0.1:2222 default: SSH username: vagrant default: SSH auth method: private key default: default: Vagrant insecure key detected. Vagrant will automatically replace default: this with a newly generated keypair for better security. default: default: Inserting generated public key within guest... default: Removing insecure key from the guest if it's present... default: Key inserted! Disconnecting and reconnecting using new SSH key... ==> default: Machine booted and ready! ==> default: Checking for guest additions in VM... default: No guest additions were detected on the base box for this VM! Guest default: additions are required for forwarded ports, shared folders, host only default: networking, and more. If SSH fails on this machine, please install default: the guest additions and repackage the box to continue. default: default: This is not an error message; everything may continue to work properly, default: in which case you may ignore this message. ==> default: Installing rsync to the VM... ==> default: Rsyncing folder: /home/milosz/Projekty/vagrant_learning/ => /vagrant
Control virtual machine state
Display current virtual machine state.
vagrant_learning$ vagrant status
Current machine states: default running (virtualbox) The VM is running. To stop this VM, you can run `vagrant halt` to shut it down forcefully, or you can run `vagrant suspend` to simply suspend the virtual machine. In either case, to restart it again, simply run `vagrant up`.
Start the virtual machine.
vagrant_learning$ vagrant up
==> default: Checking if box 'debian/jessie64' is up to date... ==> default: Clearing any previously set forwarded ports... ==> default: Clearing any previously set network interfaces... ==> default: Preparing network interfaces based on configuration... default: Adapter 1: nat ==> default: Forwarding ports... default: 22 (guest) => 2222 (host) (adapter 1) ==> default: Booting VM... ==> default: Waiting for machine to boot. This may take a few minutes... default: SSH address: 127.0.0.1:2222 default: SSH username: vagrant default: SSH auth method: private key ==> default: Machine booted and ready! ==> default: Checking for guest additions in VM... default: No guest additions were detected on the base box for this VM! Guest default: additions are required for forwarded ports, shared folders, host only default: networking, and more. If SSH fails on this machine, please install default: the guest additions and repackage the box to continue. default: default: This is not an error message; everything may continue to work properly, default: in which case you may ignore this message. ==> default: Rsyncing folder: /home/milosz/Projekty/vagrant_learning/ => /vagrant ==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision` ==> default: flag to force provisioning. Provisioners marked to run always will still run.
Stop the virtual machine.
vagrant_learning$ vagrant halt
==> default: Attempting graceful shutdown of VM...
Restart the virtual machine.
vagrant_learning$ vagrant reload
==> default: Attempting graceful shutdown of VM... ==> default: Checking if box 'debian/jessie64' is up to date... ==> default: Clearing any previously set forwarded ports... ==> default: Clearing any previously set network interfaces... ==> default: Preparing network interfaces based on configuration... default: Adapter 1: nat ==> default: Forwarding ports... default: 22 (guest) => 2222 (host) (adapter 1) ==> default: Booting VM... ==> default: Waiting for machine to boot. This may take a few minutes... default: SSH address: 127.0.0.1:2222 default: SSH username: vagrant default: SSH auth method: private key ==> default: Machine booted and ready! ==> default: Checking for guest additions in VM... default: No guest additions were detected on the base box for this VM! Guest default: additions are required for forwarded ports, shared folders, host only default: networking, and more. If SSH fails on this machine, please install default: the guest additions and repackage the box to continue. default: default: This is not an error message; everything may continue to work properly, default: in which case you may ignore this message. ==> default: Rsyncing folder: /home/milosz/Projekty/vagrant_learning/ => /vagrant ==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision` ==> default: flag to force provisioning. Provisioners marked to run always will still run.
Suspend the virtual machine.
vagrant_learning$ vagrant suspend
==> default: Saving VM state and suspending execution...
Resume the virtual machine.
vagrant_learning$ vagrant resume
==> default: Resuming suspended VM... ==> default: Booting VM... ==> default: Waiting for machine to boot. This may take a few minutes... default: SSH address: 127.0.0.1:2222 default: SSH username: vagrant default: SSH auth method: private key ==> default: Machine booted and ready!
Access virtual machine
Generate configuration for SSH
client.
vagrant_learning$ vagrant ssh-config
Host default HostName 127.0.0.1 User vagrant Port 2222 UserKnownHostsFile /dev/null StrictHostKeyChecking no PasswordAuthentication no IdentityFile "/home/milosz/Projekty/vagrant_learning/.vagrant/machines/default/virtualbox/private_key" IdentitiesOnly yes LogLevel FATAL
Connect to the virtual machine using SSH
.
vagrant_learning$ vagrant ssh
The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. Last login: Tue Jun 7 23:05:34 2016 from 10.0.2.2 vagrant@debian-jessie:~$
Delete virtual machine
Shutdown and delete virtual machine.
vagrant_learning$ vagrant destroy
default: Are you sure you want to destroy the 'default' VM? [y/N] Y ==> default: Forcing shutdown of VM... ==> default: Destroying VM and associated drives...
Additional notes
These steps are more than sufficient to start using Vagrant. Be sure to read the official documentation for Vagrant.