Laravel is an open-source PHP framework by Taylor Otwell for creating full-featured web applications. It features a modular packaging system with a dedicated dependency manager, different ways for accessing relational databases, and utilities that aid in application deployment and maintenance, that is oriented toward syntactic sugar. For those of you who are proficient in PHP, Laravel will make your job a lot easier by saving you time compared to developing a website from scratch. As an added bonus, websites built in Laravel are more secure and are resistant to several types of web attacks.
In this tutorial, we will learn two ways to work with Laravel: first, on its own, then within the Laravel Homestead Virtual Machine environment.
Laravel uses Composer for managing dependencies so you'll have to install it before you install Laravel. During the installation process, Composer will ask you to point it to your PHP executable.
Once Composer is installed, you can check the installation by typing
composer at the command prompt. That should bring up the logo, version details, and a list of commands:
Now we're ready to create our Laravel project. Create a new directory anywhere in your system for your new Laravel project. Then, cd into the parent of the new directory and type the following command there to install Laravel:
composer create-project laravel/laravel -- prefer-dist
C:\Users\blackjacques>mkdir laravel C:\Users\blackjacques>composer create-project laravel/laravel -- prefer-dist
Composer will download a whole wack of modules and create the following directory structure:
And that's it! Now we can start the Laravel service by executing the following command:
php artisan serve
Point your browser to http://localhost:8000/ to see the default Laravel page:
Installing Laravel Homestead Components
Laravel has a generally stricter set of server requirements than your typical PHP frameworks and projects. This allows Laravel to do more and make use of cool modern features. That's why Laravel offers Homestead. It's a pre-packaged Vagrant box that has all the necessary PHP configurations to use Laravel as well as some additional features. It's also setup to so you can manage multiple sites and projects from a single virtual machine. Vagrant is a portable virtual software development environment. It's written in the Ruby language and simplifies software configuration management of virtualizations in order to increase development productivity.
The process for installing Vagrant depends on your particular OS. I will cover Windows 10 here, but you can find instructions for other OSes on the Laravel Homestead site.
- First, you need to download and install virtualbox, vagrant, and git bash, in that order. You may need to restart your PC after the installations are complete.
- Open git bash in administrator mode and run the following command:
vagrant box add laravel/homestead
Vagrant will then ask you which virtual box provider you want to use:
C:\Users\robg>vagrant box add laravel/homestead ==> box: Loading metadata for box 'laravel/homestead' box: URL: https://vagrantcloud.com/laravel/homestead This box can work with multiple providers! The providers that it can work with are listed below. Please review the list and choose the provider you will be working with. 1) parallels 2) virtualbox 3) vmware_desktop Enter your choice: 2
Now, the laravel/homestead box will be downloaded and added to your Vagrant installation. It will take a few minutes to download the box, depending on your Internet connection speed. Git will keep you updated on its progress:
==> box: Adding box 'laravel/homestead' (v6.1.0) for provider: virtualbox box: Downloading: https://vagrantcloud.com/laravel/boxes/homestead/versions/6.1.0/providers/virtualbox.box box: Progress: 40% (Rate: 552k/s, Estimated time remaining: 0:28:27)
Eventually, like maybe thirty to forty minutes later, the installation will complete:
==> box: Adding box 'laravel/homestead' (v6.1.0) for provider: virtualbox box: Downloading: https://vagrantcloud.com/laravel/boxes/homestead/versions/6.1.0/providers/virtualbox.box box: Progress: 100% (Rate: 479k/s, Estimated time remaining: -- : -- : -- ) ==> box: Successfully added box 'laravel/homestead' (v6.1.0) for 'virtualbox'!
The next step is to clone the Official Laravel Repository onto your computer. You're going to want to clone it to a location other than where all your projects reside. As you will find out, with Homestead you're actually mapping all your projects to their folders on your computer. Homestead actually acts as a manager for your all your projects.
Create a new directory named "homestead" and then navigate to it within the console.
Now we'll clone the Homestead repository into the current (homestead) folder:
C:\Users\blackjacques\homestead>git clone https://github.com/laravel/homestead.git . Cloning into '.'... remote: Counting objects: 2972, done. remote: Compressing objects: 100% (12/12), done. remote: Total 2972 (delta 9), reused 22 (delta 9), pack-reused 2948 Receiving objects: 100% (2972/2972), 610.46 KiB | 387.00 KiB/s, done. Resolving deltas: 100% (1762/1762), done.
Next, we have to create the Homestead.yaml (Ain't Markup Language) configuration file. This requires the use of a Bash shell. As a Windows user, I would recommend win-bash. It's a stand-alone bash for Windows that is a port of the famous GNU bash.
C:\Users\blackjacques>cd homestead C:\Users\blackjacques\homestead>bash init.sh init.sh: [[: command not found Homestead initialized!
Although the init.sh shell script produced a "command not found" error, it still gave the "Homestead initialized!" success message. To be on the safe side, you can verify that the Homestead.yaml file was created in the C:\Users\USER_NAME\Homestead folder:
In this tutorial we learned two ways of working with Laravel: on its own and within the Laravel Homestead Virtual Machine environment. Next time, we'll configure our laravel/homestead environment and create a new project within it.
Rob's alter-ego, "Blackjacques", is an accomplished guitar player, who has released several CDs and cover songs. His band, Ivory Knight, was rated as one of Canada's top hard rock and metal groups by Brave Words magazine (issue #92).