This blog post is borne out of a difficulty in finding an authoritative and easy to understand guide to setting up a complete development environment with VirtualBox. I primarily develop with the standard LAMP stack and so this guide with focus on setting up SSH access, NFS file sharing, Apache, PHP and MySQL on the Debian Linux distribution. You will require an internet connection to complete this guide.
- Step 1: Download VirtualBox and an operating system
- Step 2: Install VirtualBox, with an operating system
- Step 3: Install sudo…
- Step 4: Configure Networking with VirtualBox
- Step 5: Share your files with your VirtualBox
There are many benefits to working with a virtualised development environment over using your OS packages and installers (whether that be homebrew/macports on OS X or directly using package managers on whatever flavour of Linux you happen to be running) these include:
- Consistency – perhaps most importantly you can mimic the environment that you are deploying to. No matter what OS your computer is running, you have complete flexibility on what you install to your VirtualBox.
- Portability – through the GUI you can export virtual machines, which means you can back them up, and share them across machines – great for standardising your development environment in a team.
- Convenience – by virtualising your development environment you isolate it from your day-to-day use of your computer. If you’re like me, your laptop is not only your work machine, but your TV watching, Facebook stalking, music playing, Skype calling all in one. I don’t really want to have a bunch of unnecessary software running when I’m not working. That said, you can just as easily have the VirtualBox spin up when you login/boot-up your computer if you’re on a dedicated work box.
Step 1: Download VirtualBox and an operating system
Get yourself to https://www.virtualbox.org/wiki/Downloads and install the appropriate package for your operating system.
An operating system
Picking a distribution can be just as confusing as setting up the environment itself, I like Debian and we use it at work so if you are going to follow this guide, it will be easiest if you use Debian or a Debian based distribution (such as Ubuntu) – the rest of this guide will assume you picked Debian.
- Debian – select the amd64 option (unless you are not on a 64bit compatible system, in which case pick i386) from any of the options, unless you know/want to pick otherwise (small CDs, Tiny CDs or Smaller CDs) http://www.Debian.org/distrib/netinst
- Ubuntu server – just click download! http://www.ubuntu.com/download/server
Step 2: Install VirtualBox, with an operating system
Take your downloaded version of VirtualBox, run it and follow the instructions. Once that is complete you should be able to run the VirtualBox program (found in your applications folder in OS X, or you should be able to type ‘virtualbox’ in the terminal on Linux).
Click the ‘new’ button in the top right and you will be welcomed by the New Virtual Machine Wizard. Click next and give your virtual machine a (memorable) name and tell it the OS (in our case Linux and version (Debian). I have imaginatively called my Virtualbox Debian.
Now you can give your VirtualBox some memory, I usually give mine 1gb (1024mb) of memory, but I only have 4gb of RAM. If you have more you can give your box more – however for a basic development environment you probably don’t need to.
The next screens will ask you about creating a virtual hard disk, tick Start-up Disk and select Create new hard disk. Click next and leave the default file type as VDI (unless you know otherwise). Click next, and select fixed size as it can offer some performance benefits, if your hard disk space is at a premium you may want to select dynamically allocated. The next screen is self-explanatory and I usually leave the default size and location for my virtual box.
Now you should be at the summary page – click create and let VirtualBox set up your machine – we haven’t installed an OS though, so we aren’t done yet.
Mount the Debian installation disk
You should now be at your VirtualBox manager screen. If you called your VirtualBox Debian, it should look something like the screenshot below. Click settings and go to the storage tab, select the CD icon with ‘Empty’ and then click on the (other) CD icon on the right hand pane. Click Choose a virtual CD/DVD disk file… and navigate to the Linux distribution file you downloaded earlier. See the image below for reference (complete with my mad drawing skills).
Now the file you downloaded is in your VirtualBox’s virtual CD drive…neat. Click ‘OK’ and we are now ready to run our VirtualBox for the first time.
Run your VirtualBox
Highlight your VirtualBox from the list (if it isn’t already highlighted) and click run. If everything so far has gone to plan you should be greeted with the Debian installation screen. If not, make sure you downloaded the correct version (32/64 bit). Select Install and let’s get started; these next steps are pretty self-explanatory so I will keep it brief:
- Select your language, location and keyboard input.
- Wait for “loading additional components”
- Configure the network > enter a host name – I usually use the same name as I have given my machine in VirtualBox. Don’t worry too much about this, we won’t be using it
- Configure the network > enter a domain name e.g. debian.local
- Choose a mirror of the Debian archive choose somewhere close!
- Enter your proxy details if you have any, just continue otherwise
- Set a root password make sure you remember what you set it to!
- Create a user – you will be using this to access the machine regularly
- Partition disks unless you know otherwise: select Guided – use entire disk, the vbox harddisk (you should only have one option and All files in one partition. After that select Finish partitioning and then accept the changes
At this point the installer will install the basic OS. You may be waiting a little while for this to complete – so go make a cup of tea, or do whatever else you like to do in roughly the amount of time it takes to make and drink a cup of tea.
Once you have had your cup of tea, Debian should be waiting for you, asking if you want to participate in an anonymous survey, I don’t care what you select, but pay attention on the next screen. At software selection, you want to install the bare minimum. Therefore we will deselect everything, and only leave Standard system utilities selected.
Just follow the prompts
You’ve got this far, you deserve a pat on the back. Unfortunately that was the easy bit. Select continue, you don’t need to worry about removing any media (VirtualBox seems to ignores it?).
You will be presented with the GRUB bootloader screen, it will automatically start your installation of Debian, where you can enter your username and password we set up earlier.
Step 3: Install sudo…
Sudo is a command that is at the root (haha) of system administration. It is the tool that lets you modify files that otherwise would be protected (for very good reason). But we also need it to configure some system critical files. If you are still where I left you previously, you should be logged into your VirtualBox with the user we created earlier. We need to switch to the root user to install the sudo program, and we do this by typing the command (omit the dollar)
At which point you should be asked for a password, this is the root password we set up earlier.
Notice we’ve changed from
#, indicating that we are now the root user.
Next type (don’t type the
# apt-get update && apt-get install sudo
You will become very familiar with the
apt- based commands, apt is the package management tool that will let you install all kinds of software/packages on your VirtualBox.
Once those commands have finished running, type the following on the command prompt (again without the # symbol, but keep the quotes, and switch my_user_name with the username we have been using throughout):
# echo “my_user_name ALL=(ALL) ALL” >> /etc/sudoers
This will add your user to the list of people allowed to use the sudo command, now you can switch back to your own user name by typing
# su my_user_name
If you don’t already know, you may be wondering how we are going to connect to and work with our projects on our virtual machine – this is where SSH, and NFS step in…
Step 4: Configure Networking with VirtualBox
I have found the most flexible option for networking with a virtualbox is to set up two networking adapters, one uses a host only network to allow you to access the machine via SSH, and the other uses NAT to allow the VirtualBox access to the outside world (to let you download packages etc.). Before doing any of the below you need to make sure your VirtualBox is powered down.
First you need to open the general settings for the VirtualBox program, hit the network tab and add a host only network, when you click add VirtualBox will let you configure an IP address for your host computer (i.e. your workstation). This will be used to communicate with the VirtualBox. For this example we will give ourselves the IP address 10.10.4.1, leave the other settings at their defaults, but go to the DHCP Server tab and make sure that enable server is switched off.
Next you need to set up the networking for your actual VirtualBox, and you can do this by highlighting your machine, and clicking on settings, like we did before. Navigate to the Network tab, and you should see that Adapter 1 is already configured and enabled.
Hit the Adapter 2 tab and tick Enable Network Adapter and select Host-only Adapter under Attached to, Name should be pre-filled for you.
Now all that is done, we can start up our VirtualBox. Once you have logged in you want to edit your (VirtualBox) Debian configuration file for it’s network. You do this by typing the following
$ sudo nano /etc/network/interfaces
This will open the interfaces file with the nano text editor (I prefer vim, but we haven’t installed it yet, and that’s a steeper learning curve!!).
Add the following to the end of the file, this will allow us to connect to the VirtualBox via SSH and share files with it. The address section can be altered to be whatever number you desire, so long as it is prefixed 10.10.4.x.
Once everything is set, run the following to restart your networking configuration (unfortunately the restart command is deprecated in Debian)
$ sudo /etc/init.d/networking stop; sudo /etc/init.d/networking start
At this point networking is setup, and your VirtualBox can communicate with the internet and your computer, but we don’t want to have to use the clunky VirtualBox GUI to execute commands, we do this through a tool called SSH which we can install via
$ sudo apt-get install ssh
Once that has completed, open a terminal on your local machine and you will be able to use SSH to log into your VirtualBox (you should know the username and IP address as we set them up earlier!)
$ ssh email@example.com
Step 5: Share your files with your VirtualBox
In order to access your files on your VirtualBox you need to use some kind of sharing mechanism. The VirtualBox program provides an easy to use Shared Folders, however these have proven to be very slow as documented by the folks over at Clock:
It is for that reason we are going to use NFS, it takes a little extra configuration, but coming from someone who has tried to use the VirtualBox shared folders, you will not regret the choice. The following is largely a repetition of the steps found in that second blog post, so thank you Paul Serby!
First we need to define which folders to share with our VirtualBox, we do this by adding/editing a file called exports on our local machine.
$ sudo nano /etc/exports
And adding the following lines
Then we need to reload the nfs config on the local machine
$ sudo nfsd update
$ sudo exportfs -a
Then we can install NFS on our VirtualBox and mount the folders we have just exported, ssh into your VirtualBox and type
$ sudo apt-get install nfs-common
Then we want to add a line to our
/etc/fstab file on the guest machine, so it knows where to mount our project files to, I usually mount them in
/var/www for convenience with apache.
Mount all the available drives/sources:
$ sudo mount -a
$ ls /var/www
You should now have a working basic VirtualBox that you can export (see the File > Export Appliance dialog in the VirtualBox GUI) and backup and use as a base for all your development projects.
Alternatively you could just use Vagrant and Puppet/Chef which are tools that can help take a lot of pain out of the above process, however going through this process once or twice is really worthwhile as you will become a lot more familiar with server configuration.
I currently use a little bash script to manage my VirtualBoxes on the command line which removes the need for using the GUI, or having the VBoxManage command running in a terminal window.
You can use it to start a VirtualBox
./vboxme.sh -s <name>
Or stop a VirtualBox
./vboxme.sh -x <name>
And check out the help message for a few extra options.
I’ve now written Part 2 to this tutorial explaining how to configure the LAMP stack and Part 3 – setting up the Symfony2 development environment on VirtualBox is in the pipeline so watch this space.