I recently went through the trouble of setting up an Ubuntu 16 vm using virtualbox and sharing a folder with my workstation (the host) for a Laravel development project. The idea was to set up things so that I can edit the source code and perform git operations on my workstation/host machine while running apache on the vm/guest because laravel 5.4 needs newer features my workstation lacks. Kind of a Pain In The Ass (PITA) for various reasons. I document the steps here both for my own future reference but also to show part of the PITA sandwich.
1) Open VirtualBox and fire up my Ubuntu 16.04 instance.
2) Reset root password, which I had forgotten, on the vm/guest. Helpful video here.
3) Consider cloning the basic VM (which is just a clean Ubuntu 16.04 in my case) because you may not want the following settings applied to your one-single-virtualbox-master-copy instance. I do this by shutting down the VM, right-clicking it in the VirtualBox gui, and selecting Clone.... Give it an informative name, e.g., my-project-vm or something. I chose Linked Clone because I wanted smaller file size and am not concerned about moving this clone away from its linked VirtualBox files.
4) Put the new clone in bridged network mode (settings->network, select 'bridged adapter').
5) Fire the new clone up.
6) Set the clone to use a static ip address. Should probably be different than the vm from which you cloned it (192.168.1.4 in my case). Why do this? If you don't, it may acquire a different IP address every time you fire it up, which can be a real nuisance to keep tracking down. Changing ubuntu-server to static ip address is beyond the scope of these instructions for ubuntu-desktop, just open the Network control panel, select Wired from the connections listed and then click Options.... Then click the IPv4 Settings tab and in the resulting Editing Wired Connection 1 dialog, set Method to Manual. Click Add to add a static IP. with:
Address: 192.168.1.5 (change this if you like)
Netmask: 255.255.255.0 (this might be different on your system, check output of ifconfig from host machine to see what it has for Mask)
Gateway: 192.168.1.1 (this is the IP of my router running DHCP)
In the bottom half of the dialog where is says DNS Servers, enter 192.168.1.1 (ip addr of router running DHCP).
Click Save and close the dialog. Reboot the vm/guest machine. When it reboots, it should try to bind to the new address (192.168.1.5 in this case) on your network.
At this point, you should make sure that your host machine can ssh into the vm -- or at the very least access the vm/guest via browser. Try entering http://192.168.1.5 in browser and make sure you are accessing the vm.
7) Establish a shared folder between my workstation/host computer and the vm/guest computer. I've cloned a repo of a laravel project into a working directory on my workstation (/path/to/my-project) and I want apache on the vm/guest machine t7 serve those laravel source files. For laravel project to even run, apache needs read access to all the files and write access to the storage subdirectory (and all its subdirectories) and also the bootstrap/cache directory (and possibly its subdirectories?). There is useful information in the VirtualBox documentation here. NOTE: setting up a shared folder requires installation of the Guest Additions to the vm/guest machine. This is also discussed earlier on that same page I've linked.
7a) Share a folder on your host machine. You cannot share from the guest/vm machine so you share one on the host and it is accessible to the guest/vm. There are a few ways to get at this setting in VirtualBox. I suggest going to the VM's window and using the menu at the top: Devices->Shared Folders->Shared Folders Settings. This brings up a Settings dialog. Click the Folder-with-plus icon to add a shared folder. This brings up another Add Share dialog. Where it says Folder Path, you can either type in the path on the host machine to be shared or you can select Other... and browse your host machine for the folder you want to share. I choose /path/to/my-project. In the Folder Name input, you'll need to enter a "share name" for it. In my case, I just chose my-project-share. Do NOT check the read-only checkbox. You can check automount if you like, but this means that the guest/vm will have a weird path to the shared folder, something like /media/sf_my-project-share. I only check Make Permanent -- this should make the shared folder available even if we shutdown/reboot the vm, however, we will still have to manually mount the shared folder from our guest/vm to work with it.
7b) Mount the shared folder on the guest/vm. First, create a directory on your guest/vm in the location where you want the shared folder to appear. I chose this:
sneakyimp@guest-vm$ sudo mkdir /var/www/my-project
According to section 4.3.1, you can mount the shared folder from the guest/vm with this command (which I've modified to reflect my choices here):
sneakyimp@guest-vm$sudo mount -t vboxsf my-project-share /var/www/my-project
You should now be able, on your guest/vm, to list the contents of the new folder and see the shared contents from your host machine:
sneakyimp@guest-vm$ ls -al /var/www/my-project
If you encounter problems or would like to change your mount options, you can use:
sudo umount /var/www/my-project
to unmount the shared folder and try again.
TO BE CONTINUED....(too long for one post)