Add Laravel to Path on Mac OSX
I have been playing around learning the Laravel, the PHP framework for web artisans. It really is an awesome framework, however I did find getting up and running with the framework to be not as simple as I’d hoped. I ran into a couple of installation niggles that I thought I would document in order to save others time and frustration.
Install PHP
The best way to set up a development environment or even to manage the packages installed on Mac OSX is to use Homebrew
Homebrew – The missing package manager for macOS
Check out the instructions to install Homebrew on MacOsx
If you have Homebrew installed, installing PHP is very simple, in the example below we will be installing PHP 7.2, which at the time of writing is the most current version.
1 |
brew install php72 |
If you would like to install any other version of PHP i.e PHP 5.6 you simply need to use brew install php56
Install Composer
In order to install and easily use the Laravel framework we will need to install composer
Composer is a tool for dependency management in PHP. It allows you to declare the dependent libraries your project needs and it will install them in your project for you.
If you would like to install composer without making use of Homebrew you can so by
1 2 |
curl -sS https://getcomposer.org/installer | php mv composer.phar /usr/local/bin/composer |
I recommend installing composer via Homebrew
1 |
brew install composer |
Once the install has completed you can test it out by simply executing composer
Install Laravel
We can now install Laravel using composer we will add it to the global composer
1 |
composer global require "laravel/installer" |
We can also generate new Laravel web applications using
1 |
composer create-project --prefer-dist laravel/laravel notepad |
Add Laravel to path
There is an alternate method to creating a new Laravel project by using a the laravel new
but it requires an additional configuration step to make it work.
Make sure to place composer’s system-wide vendor bin directory in your
$PATH
so the laravel executable can be located by your system. This directory exists in different locations based on your operating system;
It does not clearly explain what this means or provides sufficient detail on how to do this. In order to achieve this we need to put this folder on the PATH environment variable type, just copy and paste the following commands into your terminal window
1 |
export PATH="$PATH:$HOME/.composer/vendor/bin" |
If you want it to be automatically set For bash you can append this line to $HOME/.bashrc
using your favourite editor or type the following on the shell
1 |
echo 'export PATH="$PATH:$HOME/.composer/vendor/bin"' >> ~/.bash_profile |
Once complete ensure your refresh the source by executing
1 |
source ~/.bash_profile |
Once you have completed the steps above we can now create a new project.
We can create a new notepad project by using laravel new notepad
.
Change into the new notepad directory cd notepad
and start the project php artisan serve
Install Laravel Homestead
Laravel Homestead is pre-packaged Vagrant box that provides a development environment without you having to install everything on your local machine. Enabling you to create a complete environment for your laravel application.
In order to install this you will need to install
- Virtual Box –
brew install caskroom/cask/virtualbox
- Vagrant –
brew install caskroom/cask/vagrant
After you have completed the installation of the above you can install Laravel Homestead by running the following
1 |
vagrant box add laravel/homestead |
It will take sometime for the installation to complete.
Once complete you will have the latest copy of a complete Ubuntu Lamp stack server set up, which you can directly SSH into. The project files on your local machine wil be synced with the VM.
Once the installation of Homestead completes, you will need to create a homestead.yaml
file that will hold all the configuration for your VM.
YAML is a human friendly data serialization standard for all programming languages.
Installing Homestead
Install Homestead by cloning the GIthub repository.
Clone the repository into a Homestead
folder within your $HOME
directory, as the Homestead box will serve as the host to all of your Laravel projects.
1 |
git clone https://github.com/laravel/homestead.git ~/Homestead |
Check out a stable tagged version of Homestead. You can find the latest stable version on the Homestead GitHub Release Page
1 2 |
cd ~/Homestead git checkout v7.3.0 |
We can now run the Initialization script, which will create the homestead.yaml
configuration file, which will be located in ~/Homestead
Configuring Homestead
Open your homestead.yaml
in which ever text editor you prefer.
Setting the Provider
The provider key in your Homestead.yaml
file indicates which Vagrant provider should be used:
virtualbox
vmware_fusion
vmware_workstation
parallels
hyperv
- How to install Laravel 5.6 on Ubuntu 16.04 - April 4, 2018
- What is a CRM ? - March 15, 2018
- Introduction to Python - March 6, 2018
.
set this to the provider you’re using. In my case it’s virtualbox
:
1 |
provider: virtualbox |
Configure shared folders
The folders property of the Homestead.yaml
file lists all of the folders you wish to share with your Homestead environment.
When Files within these folders are changed, Edited etc. they will be kept in sync between your local machine and the Homestead environment.
A sort of convention among Laravel developers is to create a folder name code
in your $HOME
folder where you’ll store your projects.
1 2 3 4 5 |
cd $HOME mkdir code cd code |
Create a project using laravel new sourcelink
using what ever name you want to call your project.
Once the project has been created you can now edit your homestead.yaml
file
1 2 3 |
folders: - map: ~/code to: /home/vagrant/code |
You can create as many projects as you like, but you have to remember to edit your homestead.yaml
to add your new project mapping.
You should map every project to its own vagrant
folder.
1 2 3 4 5 6 |
folders: - map: ~/code/sourcelink to: /home/vagrant/code/sourcelink - map: ~/code/notepad to: /home/vagrant/code/notepad |
Gary Woodfine
A unique background as business owner, marketing, software development and business development ensures that he can offer the optimum business consultancy services across a wide spectrum of business challenges.