I'm sure you're eager to get started with Laravel. Since it's a web framework, it's important that we create an environment with a web-server. In a production environment, this would take a long time to configure, and would rely on a unix-based platform, so let's build a homestead instead.
This is no time for home-making!
Hah! You might be right, but that's not quite what we mean in this instance. A ‘Homestead’ is a virtualised web application environment powered by a piece of software called ‘Vagrant.' It can get us up and running on Laravel in a jiffy!
Homestead is my preferred option of running Laravel. It ships with all the software required to launch the framework, however, it is one extra piece of software to learn. You'll need to remember to SSH into the virtual machine and run your commands from there. For this reason, the next chapter will introduce 'Valet'. Valet is a binary that will allow you to run web applications using a version of PHP installed on your computer. Right now, it's only available on Mac. If you're not using a Mac, or you'd like to take my advice, then please continue reading. Otherwise, feel free to skip to the next chapter to use Valet instead.
Install Software Dependencies
Before we can get our homestead running, we're going to need to install a few dependencies. Here's the full list.
- PHP http://www.php.net/
- Git https://git-scm.com/
- Composer https://getcomposer.org/
- Virtualbox https://www.virtualbox.org/
- Vagrant https://www.vagrantup.com/
I'm not going to provide specific installation instructions because they change from week to week. Instead, head over to the URLs above and you'll find sections of each site that will cover download and installation instructions for your operating system.
Let's take a look at what each piece of software offers.
We're going to need ‘PHP’. It's the language used by the Laravel framework. There's no way of getting out of this one! Download the latest version that's available.
We'll use ‘Git’ for version control. It's also a great way of downloading a base copy of Laravel. It's worth having since it's a day-to-day part of any experienced software developer's workflow.
We'll use ‘Composer’ to manage all the libraries that we use with PHP. Go ahead and grab it. It's one of my favorite pieces of software!
Next, we have ‘Virtualbox’. It's software that will allow operating system virtualisation. Essentially, this will allow you to run 'pretend' computers on your main computer. Since web development setups run best on unix environments, we'll be using a virtual unix environment so that we can develop on any platform!
Finally, we have ‘Vagrant’. Vagrant is used to provision virtual environments, and to configure them the way that we want them. It's a command line program, so we'll be using it in the terminal. Vagrant will be making use of Virtualbox to create project environments.
Once we've got all five pieces of software installed, we're ready to move to the next section.
Create a Laravel Project
Before we build our homestead, we'll need to have a Laravel project ready for it.
First, we'll need to decide on a disk location for your projects. I'm going to be creating a ‘Projects’ directory in my home folder. This is where all my Laravel projects will live.
Let's start by navigating to our projects folder, shall we?
That wasn't so hard, was it? Next, we can use the ‘Composer’ software that we installed earlier to create a new Laravel project. Let's type the following command and see what happens. You can replace the word ‘example’ with your project name if you'd prefer something different.
composer create-project laravel/laravel example
You'll see a lot of output. This is where Composer is downloading all of the libraries that support Laravel. We call these ‘package dependencies’. Once complete, you've installed Laravel. Continue to the next section to add a homestead.
First, let's navigate to the directory that our Laravel project is contained within.
Next, we're going to install the homestead composer package, so that we can create a homestead virtual environment. Don't worry; it's simple. Just type the following command.
composer require laravel/homestead --dev
You'll see a few more composer packages installed in the output of the command. Don't worry. That's what we want!
Next, we'll run the ‘make’ command, to configure our project for homestead. Here's the full command.
php vendor/bin/homestead make
Well, at least that one was super quick!
Next, it's time for the long one. Let's use the following command to boot up a virtual machine. If it's the first time that you're running this command, it's going to take a while to download the virtual machine image. It's around 600mb or so. Go and make yourself a coffee!
Once the virtual machine has come online, there's one final thing you're going to need to do. We'll add the homestead app domain to our hosts file so that we can conveniently access our web application in the browser.
Use your favorite editor to edit the file at
/etc/hosts. You're going to need admin/root privileges to access this file. You'll want to add the following line.
To check whether everything is working, let's visit
http://homestead.app in our browser. You should see the text ‘Laravel 5’. That's our Laravel application!
Your brand new web development environment exists on a virtual machine, and that machine is draining the resources of your host machine. For example, it will share your available RAM and processor time.
From time to time, it might be useful to be able to ‘halt’ and restore our environment to save system resources. It's also noting that when we restart our host computer, our virtual environment will not boot automatically.
For this reason, let's go over a few basic commands to maintain our Vagrant environments. We'll be using these commands in the directory containing the
The first command is used to start your virtual machine. You've already used it. Here's an example.
The second command is the exact opposite. It's used to halt your virtual machine. It will no longer be using system resources. You're going to want to use the following.
If you decide to change the configuration of your Vagrant box by updating the
Vagrantfile, you're going to want to use ‘provision’ to apply the changes to the virtual machine.
In some circumstances, you may want to run commands on the guest virtual machine. You can easily access this machine by using the Vagrant SSH command. This will allow you sudo access to the guest. For example.
Finally, if we're done with our project, and don't want the virtual machine image hogging our disk space, we can ditch it! Just use the ‘destroy’ command!
In the next chapter, we'll take a look at an alternative way of running a Laravel application.
My books are available online for free to encourage learning. However, if you'd like for me to keep writing, then please consider buying a digital copy over at Leanpub.com.
It's available in PDF, ePub, and Kindle format, and contains a bunch of extras that you won't find on the site. I have a full-time job, and I write my books in my spare time. Please consider buying a copy so that I can continue to write new and exciting books!