Setting up a Virtual Machine
11th February 2015 • Guide
We'll cover three important things:
First you'll need VirtualBox, a visuaistion provider.
Download from here, Look for VirtualBox X.X.XX for OS X Hosts. Once it is downloaded, follow the GUI to install it.
Vagrant helps you build and manage development environments really quickly, everything is automatic and allows you to create identical environemnts over and over again on any computer.
Download from here. Once downloaded follow the GUI to install.
Now we have the easy important tools installed we can start setting up an environment.
There loads of diferent ways to go from here, you could install a plain image of Ubuntu and set it up to install the specific packages you want, but we just want a pretty generic PHP environment.
Vagrant refers to VMs as Boxes, we want to create a box for our PHP dev.
That's where homestead comes in, built by the same guy who made Laravel its a premade vagrant box perfect for all things PHP/Laravel.
Jump into terminal.
Since installing Vagrant, we now have access to the
vagrant command line tool. We can add Homestead box by running the following command.
vagrant box add laravel/homestead
This is going to download the homestead box (ubuntu and the packages), so its big and might take a little while. You only have to do this once, even if you have 20 VMs. Vagrant will store this box so you can quickly provision a new box.
Now it is time to set up a VM and start using it. You will have to do the following for every development environment you create.
But if you're just going to be building simple applications you can use the same environment for them all.
Find a safe place on your machine and navigate to it in Terminal (I use
cd ~/Developer/Virtual git clone https://github.com/laravel/homestead.git Homestead
This will pull down the homestead tools. Open Homestead and run the initializing script.
cd Homestead bash init.sh
You might have this bit
If you have ever used SSH you might have done this, otherwise, do it. Generate an SSH key. This is so you can connect to your VM.
ssh-keygen -t rsa -C "[email protected]"
You can hit enter a few times to use the default settings.
Back to Homestead
Now we need to configure the Virtual Machine. Set up our sites, domains and folders.
The file we want to edit is
~/.homestead in Finder and edit the file in Sublime or..
folders: - map: ~/Code to: /home/vagrant/Code
The first line maps a local folder
~/Code to a folder on the VM
/home/vagrant/Code Adding multiple folders could look like this. Be careful with your indenting.
folders: - map: ~/Developer/MyApplication to: /home/vagrant/MyApplication - map: ~/Developer/WebsiteOneTwoThree to: /home/vagrant/WebsiteOneTwoThree - map: ~/Developer/ClientApp to: /home/vagrant/ClientApp
Now these folders will sync. If you edit something in
~Developer/ClientApp it will update in the VM
~/home/vagrant/ClientApp and vice versa.
Now we need away to access these folders.
sites: - map: homestead.app to: /home/vagrant/Code/Laravel/public
The first line maps a domain
homestead.app to a directory on the VM
/home/vagrant/Code/Laravel/public. The same folders we set up before could be sites like this.
sites: - map: myapp.local to: /home/vagrant/MyApplication - map: website123.dev to: /home/vagrant/WebsiteOneTwoThree - map: client.app to: /home/vagrant/ClientApp/public
Be careful setting the VM directory if your application doesn't run in the document root.
To make these domains work, we need to tell your computer to look for a server on the VM.
Open up your
sudo nano /etc/hosts
And add each domain, pointed to the VM.
192.168.10.10 myapp.local, website123.dev, client.app
Now it's time to launch the box.
Navigate back to where we clone the git repo. And tell vagrant to start the VM.
cd ~/Developer/Virtual/Homestead vagrant up
The first time you run this, it'll take a little time as it sets up all the folders and domains. You'll have to re run this whenever you restart your computer.