Using a disposable Ubuntu Xenial (16.04) in seconds
We want to access and use an Ubuntu Xenial system (16.04 LTS) as quickly as possible.
To do that, Vagrant uses a file named Vagrantfile
to describe the Vagrant infrastructure. This file is in fact pure Ruby that Vagrant reads to manage your environment. Everything related to Vagrant is done inside a block such as the following:
Vagrant.configure("2") do |config| # all your Vagrant configuration here end
Getting ready
To step through this recipe, you will need the following:
- A working Vagrant installation
- A working VirtualBox installation
- An Internet connection
How to do it…
- Create a folder for the project:
$ mkdir vagrant_ubuntu_xenial_1 && cd $_
- Using your favorite editor, create this very minimal Vagrantfile to launch an ubuntu/
xenial64
box:Vagrant.configure("2") do |config| config.vm.box = "ubuntu/xenial64" end
- Now you can execute Vagrant, by explicitly using the Virtualbox hypervisor:
$ vagrant up --provider=virtualbox
- Within seconds, you'll have a running Ubuntu 16.04 Vagrant box on your host and you can do whatever you want with it. For example, start by logging into it via Secure Shell (SSH) by issuing the following
vagrant
command and use the system normally:$ vagrant ssh Welcome to Ubuntu 16.04.1 LTS (GNU/Linux 4.4.0-34-generic x86_64) […] ubuntu@ubuntu-xenial:~$ hostname ubuntu-xenial ubuntu@ubuntu-xenial:~$ free -m ubuntu@ubuntu-xenial:~$ cat /proc/cpuinfo
- When you're done with your Vagrant VM, you can simply destroy it:
$ vagrant destroy ==> default: Forcing shutdown of VM... ==> default: Destroying VM and associated drives...
Alternatively, we can just stop the Vagrant VM with the goal of restarting it later in its current state using vagrant halt:
$ vagrant halt
How it works…
When you started Vagrant, it read the Vagrantfile, asking for a specific box to run (Ubuntu Xenial). If you previously added it, it will launch it right away through the default hypervisor (in this case, VirtualBox), or if it's a new box, download it for you automatically. It created the required virtual network interfaces, then the Ubuntu VM got a private IP address. Vagrant took care of configuring SSH by exposing an available port and inserting a default key, so you can log into it via SSH without problems.