Creating virtual dev environment with xhyve

xhyve is the awesome lightweight virtual machine for Mac OS X.

In this post, I will show you how to have the same target as Vagrant with
xhyve. At the end of this post, we will have the virtual environment which
synced with the host OS’s folder to work on our project.

Our virtual environment will run Ubuntu Server
, which has built-in Python

 Install Ubuntu on xhyve

 Prepare the kernel and initrd

Because xhyve does not support BIOS or EFI booter. We need to pass the
kernel and ramdisk file to xhyve manually.

Since OS X does not recognize the file system of Ubuntu ISO. We need a little
hack to mount it.

Create a cloned ISO file named /tmp/tmp.iso of Ubuntu ISO disk.

$ dd if=/dev/zero bs=2k count=1 of=/tmp/tmp.iso
$ dd if=ubuntu.iso bs=2k skip=1 >> /tmp/tmp.iso

Now we can mount the ISO file to copy the necessary files:

$ hdiutil attach /tmp/tmp.iso

The ISO file now mounted at** “Ubuntu-Server 14”** volume.

$ cp /Volumes/Ubuntu-Server\ 14/install/vmlinuz .
$ cp /Volumes/Ubuntu-Server\ 14/install/initrd.gz .

 Create virtual hard disk image

Create a virtual hard disk image so you can install your Linux distro on it. In
this post, we create a** 3GB** disk named hdd.img

$ dd if=/dev/zero of=hdd.img bs=1g count=3

 Create start up script for Ubuntu installation

Now, we create a new bash script to init and start up a virtual machine with
xhyve, that use our hdd.img as a virtual hard disk and ubuntu.iso as a virtual CD. Then install Ubuntu to the virtual hard disk.


CMDLINE=”earlyprintk=serial console=ttyS0 acpi=off”

MEM=”-m 1G”
NET=”-s 2:0,virtio-net”
IMG_CD=”-s 3,ahci-cd,ubuntu.iso”
IMG_HDD=”-s 4,virtio-blk,hdd.img”
PCI_DEV=”-s 0:0,hostbridge -s 31,lpc”
LPC_DEV=”-l com1,stdio”


Don’t forget to chmod the script file, so you can execute it:

$ chmod +x

Start your virtual machine and install Ubuntu in text mode installer:

$ sudo ./

You have to run with sudo because virtio-net required root

At the very end of the installation, don’t forget to choose “Install the GRUB
boot loader to the master boot record”

When you reach “Installation complete” **screen, select “Go back” and
“Execute a shell” to config copy installed **kernel and initrd
to Mac. Enter this to VM:

$ cd /target
$ sbin/ifconfig
$ tar c boot | nc -l -p 1234

Then back to Mac, enter this:

$ nc [IP] 1234 | tar x

As** [IP]** is the IP address you saw in VM ifconfig command.

Now, exit shell mode in VM

$ exit

And select “Finish the installation” to finishing up your Ubuntu Guest.

The Linux kernel from your virtual disk has been copied to your Mac’s boot

 Modify the startup script to boot Ubuntu from virtual hard disk

Now, modify the script to make it boot from installed Ubuntu in
virtual hard disk instead of CD image.


CMDLINE=”earlyprintk=serial console=ttyS0 acpi=off root=/dev/vda1 ro”

MEM=”-m 1G”
NET=”-s 2:0,virtio-net”
IMG_HDD=”-s 4,virtio-blk,hdd.img”
PCI_DEV=”-s 0:0,hostbridge -s 31,lpc”
LPC_DEV=”-l com1,stdio”


Now, start your virtual machine with:

$ sudo ./up

To shutdown the VM and go back to Mac terminal, enter:

$ sudo halt

Now your virtual machine is ready! Time to go to next step: Setting up develop environment.

Our target is to build something like Vagrant, which allow you create a virtual environment that linked to the folder on Host. So we can do anything with that virtual environment without damaging Host machine.

 Setup dev environment on your Ubuntu Guest

It depends on which development environment you want to setup, maybe you want to install LAMP, or MEAN, or Ruby on Rails.

In this post, we have Python built-in with Ubuntu, and will run the simple HTTP server with:

python -m SimpleHTTPServer

 Synching shared folder from Host to Guest

To share a folder from host OS to guest OS, you have to setup NFS server on the host OS and mount it in guest OS.

 Setup NFS Server on Host

On Mac OS X, you have to create a file named **/etc/exports **to config which folder you want to share via NFS Server.

$ sudo touch /etc/exports

Then put the shared folder path in that file with the following syntax:

<Folder path> -mapall=501

Don’t forget to use sudo when editing your exports config:

$ sudo vi /etc/exports

Folder’s path is the full path to your shared folder. Mapall parameter tell
NFS Server that which user has permission to access that folder. In this case, 501 is your current (and default) user ID.

To see your user ID, enter:

$ id

Example** /etc/exports** file:

/Users/huy/Shared -mapall=501

Now, run the NFS Server with the following command:

$ sudo nfsd enable

If you already have NFS Server started up, you will want to restart it to make
the new config available:

$ sudo nfsd restart

Now, you have your folder shared and will be visible with the Guest OS.

To check if it is available or not, run this command:

$ showmount -e

This will list all exported (shared) folder you have configured on Host.

 Mount the folder on Guest

Before mounting NFS server on Guest OS, we have to install an NFS client package first.

Run this command on Guest:

$ sudo apt-get install rpcbind nfs-commond

Now you will be able to mount any folder from NFS Server (Host) by enter the
following command:

$ sudo mount -o nolock -t nfs <host-ip>:/<folder-path> <target-parth>

For example, this command will mount** /Users/huy/Shared folder from host,
which has IP **
to ~/Shared folder in Guest OS.

$ sudo mount -o nolock -t nfs ~/Shared

Now, you have your virtual machine linked with a real folder on your Mac host.

In the next section, we will see how to use this develop environment in your
real life projects.

 Using virtual dev environment

In this section, we will use our dev environment to run a simple HTTP server
with Python to serve a simple web page.

 Prepare the dev folder on host

On host machine, create a project folder, let’s say:


Export this to /etc/exports file:

$ sudo vi /etc/exports


/Users/huy/Code/helloPy/ -mapalls=501

And restart the NFS Server:

$ sudo nfsd restart

 Mount and run HTTP server on Guest

On Guest VM, mount the project folder to somewhere, with the following command:

$ sudo mount -o nolock -t nfs ~/Code/helloPy

Then, move to that folder and run the HTTP server:

$ cd ~/Code/helloPy
$ python -m SimpleHTTPServer

The server will run at port 8000 on Guest OS.

To get Guest’s IP, run this command:

$ ifconfig

For example, the Guest VM has IP:****

We can access the web server from Host machine by entering this address:

 Coding from Host and sync to Guest’s HTTP server

You can use your favorite editor (such as Sublime, Atom, WebStorm,…) and modify the code, then refresh the web page you can see the result.

That’s how we use it!

From now, you can create as much server as you want, running as many stacks as you can without messing up your Host machine.


Now read this

Setup free SSL with Let’s Encrypt and Nginx

If you search Google for “free SSL”, there are many companies provide you a free trial SSL for only 90 days or less. After that, you will have to pay. Now, thanks to Let’s Encrypt. It’s no longer needed. You can claim your own SSL... Continue →

Subscribe to The Full Snack Developer

Don’t worry; we hate spam with a passion.
You can unsubscribe with one click.