Normally, there's an ad running in this spot, but you're using an ad blocker. To help support our blog, which provides free tutorials for everybody, we just ask that you whitelist or follow us on facebook, twitter, or subscribe using the form to the leftabove. Thank you!

    Using Vagrant and VirtualBox to Set Up a Virtual Machine

    Select Your OS:

    This guide is going to teach you how to provision a remote server on your local computer, using virtual machines. VMs are perfect for quickly spinning up and down resources for practicing SSH, website deployments, firewall configuration, and anything else you would do on a server.

    And because VMs run on your local machine, it's virtually free. You'd only pay for a Windows license if you wanted to simulate deployments to a Windows server, but don't let that stop you. I've worked with more companies and clients in the real world that use servers running free, open source Linux OSes.

    Speaking of which, today, we'll be learning how to spin up a virtual machine running Ubuntu 14.04. This VM will take care of the majority of our use cases. Let's jump right into it.

    System requirements

    • A 32-bit or 64-bit computer with at least 8GB of RAM. 16GB or more is recommended.
    • At least 50GB of free disk space.

    VirtualBox App Introduction

    VirtualBox is an application that allows you to spin up any number of virtual machines. In case you aren't familiar, a virtual machine is an emulation of a computer running on top of your operating system. Virtual machines allow you to run Windows on top of Mac OS X, or Ubuntu on top of Windows, and so on.

    Installing the VirtualBox App

    1. Fetch your OS version information by going through the prerequisites.
    2. Download the appropriate VirtualBox platform package on their website and run the installer.

    Vagrant

    Initializing a new Vagrantfile

    1. Navigate to the download page at https://www.vagrantup.com/downloads.html and download the appropriate installer.
    2. Run the installer
    3. Run the following commands from the command line, which will create a folder.
    4.       
              cd ~
              mkdir ubuntu14
              cd ubuntu14
            
          
    5. Navigate to https://atlas.hashicorp.com/boxes/search to search for a specific base box. Because we'll be launching an Ubuntu 14.04 VM, you can either enter that term in the search field or navigate to 14.04 page directly at https://atlas.hashicorp.com/ubuntu/boxes/trusty64.
    6. Copy and paste the command displayed to you into your terminal, and hit enter. The command will look something like the following:
              vagrant init ubuntu/trusty64
            
    7. The last command will spin up the VM and generate a VagrantFile in the same directory. Most of your Vagrantfile will be commented out except for the following:
    8.       
            Vagrant.configure("2") do |config|
              config.vm.box = "ubuntu/trusty64"
            end
            
          
    9. Let's add another line to this configuration which will allow the virtual machine to have it's own IP address. Update the Vagrantfile so that it only contains the following:
    10.       
            Vagrant.configure("2") do |config|
              config.vm.box = "ubuntu/trusty64"
              config.vm.network "private_network", type: "dhcp"
            end
            
          
    11. Now that we have a basic configuration in place, let's spin up the VM by running the following command. If you ever shut down or power off your virtual machine, this command can also be used to turn it on again.
    12.       vagrant up --provider virtualbox
          

      Possible Error: There was an error while executing 'VBoxManage'.

      • Host OS: Ubuntu 16.04
      • Guest OS: Ubuntu 14.04
      • Vagrant versions: 1.8.1, 1.9.5
      • VirtualBox versions: 5.0.26, 5.1.22 r115126 (Qt5.5.1)
                  There was an error while executing `VBoxManage`, a CLI used by Vagrant for controlling VirtualBox. The command and stderr is shown below.
                  Command: ["startvm", "36750088-5456-4b40-aeaa-f2033816b3da", "--type", "headless"]            
                  Stderr: VBoxManage: error: The virtual machine 'ubuntu14_default_1495401507524_62986' has terminated unexpectedly during startup with exit code 1 (0x1)
                  VBoxManage: error: Details: code NS_ERROR_FAILURE (0x80004005), component MachineWrap, interface IMachine
                

      Solution: Reinstall VirtualBox and set GUI to true.

      1. Right click and remove the VM through the VirtualBox UI.
      2.               
                      sudo apt-get purge "^virtualbox-.*"
                      sudo apt-get update
                      echo "deb http://download.virtualbox.org/virtualbox/debian xenial contrib" | sudo tee /etc/apt/sources.list.d/oracle-vbox.list
                      wget -q https://www.virtualbox.org/download/oracle_vbox_2016.asc -O- | sudo apt-key add -
                      wget -q https://www.virtualbox.org/download/oracle_vbox.asc -O- | sudo apt-key add -
                      sudo apt-get update
                      sudo apt-get install virtualbox-5.1
                      
                    
      3. Now start VirtualBox.
      4. This is an issue on Debian OSes where the command is run in a terminal without a graphical interface ($DISPLAY is empty). Fix this by updating the Vagrantfile with the following.
      5.               
                      Vagrant.configure("2") do |config|
                        config.vm.box = "ubuntu/trusty64"
                        config.vm.provider 'virtualbox' do |v|
                          v.gui = true
                        end
                      end
                      
                    
      6. Then run vagrant up
        References
      • https://github.com/mitchellh/vagrant/issues/7617
      • https://www.virtualbox.org/ticket/7367

      Possible Error: Hardware acceleration is not available.

      If you receive a "Hardware acceleration is not available" when running vagrant up, you'll need to enter your BIOS setting to allow processor assisted virtualization. The following instructions are for Intel-based machines.

      Solution: Edit BIOS settings.

      1. Right click and remove the VM through the VirtualBox UI.
      2. Restart your computer and enter the BIOS menu.
      3. Under the Security tab, enable both "Intel (R) Virtualization Technology" and "Intel (R) VT-d Feature".
      4. Save settings and exit.
      5. Then, open up a terminal and run:
      6.               
                      cd ~/ubuntu14
                      vagrant up
                      
                    

      Possible Error: VirtualBox kernel modules do not match the version of VirtualBox.

      This is likely caused by having multiple versions of VirtualBox installed on your computer.

      Solution: Uninstall all versions of VirtualBox and reinstall.

      1. Right click and remove the VM through the VirtualBox UI.
      2. Run the following in your terminal:
      3.               
                      sudo apt-get autoremove 'virtualbox*'
                      
                    
      4. Then go to https://www.virtualbox.org/wiki/Linux_Downloads and install the approriate version of VirtualBox
      5. Then run vagrant up
        References
      • https://github.com/mitchellh/vagrant/issues/7617

    Selecting an operating system and launching

    1. You have multiple options for setting the base box. Today, we'll spin up an Ubuntu Server 14.04 LTS instance, so run vagrant init ubuntu/trusty64.
    2. Note: About custom Vagrantfiles.

      Alternatively, you can set config.vm.box = “ubuntu/trusty64” of an existing Vagrantfile. Well go over all of the custom Vagrantfile configurations in another article. For today, we’ll just focus on getting an Ubuntu 14.04 box up and running.

    3. Now run vagrant up and switch over to the VirtualBox UI.
    4. We have some configurations to take care of before we’re ready to ssh into the machine. Select your instance, click “Settings” and select the “Network” tab.
    5. Now click on port forwarding and you should see a table with the following values:
    6. Name Protocol Host IP Host Port Guest IP Guest Port
      ssh TCP 127.0.0.1 2222 22

      This basically means that the guest machine can be accessed on localhost (127.0.0.1) through port 2222.

    How to Login with SSH

    SSHing into the VM with a Username and Password

    Alternatively, if you're in the same directory as the Vagrantfile, you can just run:

        vagrant ssh
      

    You’ll be prompted for a password. Enter vagrant. You should now be logged into the virtual machine.

    SSHing into the VM Over Port 2222

    To SSH into the machine from any folder, open up the command line and run:

        ssh vagrant@127.0.0.1 -p2222
      

    You’ll be prompted for a password. Enter vagrant. You should now be logged into the virtual machine.

    SSHing into the VM Using Its IP Address

    1. First, SSH into the machine the easy way or over port 2222.
    2.       
            vagrant ssh
            
          
    3. Then, run ifconfig
    4.       
            ifconfig
            
          
    5. Copy the eth1 inet addr. We will refer to this inet address as the virtual machine's IP address from now on.
    6. Run exit from the command line to exit the machine.
    7.       
            exit
            
          
    8. Re-enter the machine using its IP address using the following command, but replace "the.vm.ip.address" with the machines IP address.
    9.       ssh vagrant@the.vm.ip.address
          

    Alternatively, you can use the following command from the host machine to also fetch the IP address: vagrant ssh -c "ip address show eth1 | grep 'inet ' | sed -e 's/^.*inet //' -e 's/\/.*$//'"

    By the way, VMs on the same bridged network have the ability to ssh into each other.

    VirtualBox Snapshots

    Snapshots are completely optional. To learn how to create snapshots, proceed to this guide.

    Additional Tips

    Quickly Sending Commands to the Virtual machine

    When you just want to quickly send a command to a VM without having to SSH in to the machine, navigate to the same folder as the Vagrantfile and run vagrant ssh -c like so:

          
          vagrant ssh -c 'sudo service httpd restart'
          Stopping httpd: [  OK  ]
          Starting httpd: [  OK  ]
          
      

    Reloading Changes to the Vagrantfile

    After you've made your changes, run vagrant reload.

    Deprovisioning the Virtual Machine

          
          # by hostname
          vagrant destroy [HOSTNAME]
          # all
          vagrant destroy -f
          
      

    Did you like this tutorial? Help us pay for server costs by following us on Facebook, Twitter, and subscribing below, where you'll get post notifications, training webinar invites, and free bundles.