Call + (44) 0843 289 4539

Add Laravel to Path on Mac OSX

 

Learning and understanding Laravel, the PHP framework for web artisans can greatly improve your PHP development experience and actually make it fun, but just concentrating on the things that matter most. Laravel takes care of all the boring lower value cruft work a developer has to go through just to get a typical site up and running

It really is an awesome framework, however, for a new developer to get it installed can be somewhat intimidating and there are so many tools and components that come along with it, that things can get a little confusing.

Although the documentation on Laravel is quite extensive and can really help to get you working with the framework, Ironically we have found that the documentation to get it all installed and working can be a little confusing and in some areas lacking.

In this post we hope to address some of these issues help you get up and running with Laravel on Mac OSX .

How to Install PHP 7.2 on Mac OSX

The best way to set up a development environment or even to manage the packages installed on Mac OSX is to use Homebrew. A word of caution though HomeBrew have deprecated the old PHP channel and have merged everything within the code.

Homebrew – The missing package manager for macOS

How 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.

brew update && brew upgrade
brew doctor
brew install php

PHP 7.2 is now the default in HomeBrew

If you want to have the ability to step through and debug your PHP applications you will need to install xdebug which you will have to do using PECL. PECL is a repository of PHP extensions which provides all known extensions and hosting facilities for downloading and development of PHP extensions.

pecl install xdebug

Full Instructions to configure PHP 7 & Xdebug on Mac OSX



How to install Composer on Mac OSX

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

brew install composer

if you are not using HomeBrew you can install composer using Curl

curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer

Once the install has completed you can test it out by simply executing composer

How to Install Laravel on Mac OSX

We can now install Laravel using composer we will add it to the global composer

composer global require "laravel/installer"

We can also generate new Laravel web applications using

composer create-project --prefer-dist laravel/laravel notepad



How to Add Laravel to Path on Mac OSX

The Laravel Documentation refers to an alternate method of creating a new Laravel project by using,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.

In my opinion, this does not clearly explain what this means or provides sufficient detail on how to do this.

What is PATH

*nix Operating Systems and Microsoft Windows use Environment variables hold values related to the current environment.  PATH is an environment variable which is used for specifying a set of directories where executable programs are located.

In general, each executing process or user session has its own PATH setting.

How to Edit your laravel PATH on Mac OSX

There are 2 approaches you can take to edit your PATH on MAC OSX.

Approach 1

Using your terminal window, open  $HOME/.bash_profile,  using any Text editor of your choice.   Typically using either nano or Vim.

In this example, I will use nano

 nano ~/.bash_profile

You can just copy and paste the following commands towards the end of the file –

export PATH="$PATH:$HOME/.composer/vendor/bin"

After completing, CTRL + X to exit and save the file.

You will then also need to refresh your current terminal window with updated bash_profile.

 source ~/.bash_profile

Approach 2

If you want to edit bash_profile all in one line you can do so by :

echo 'export PATH="$PATH:$HOME/.composer/vendor/bin"' >> ~/.bash_profile

You will then also need to refresh your current terminal window with updated bash_profile.

source ~/.bash_profile

Once you have completed either of the steps above you can now create new projects using commandslaravel new [project name].

Example of using Laravel new

We can create a new project named notepad 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 a 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 two additional components

  • Virtual Boxbrew 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:

 

vagrant box add laravel/homestead

It will take some time for the installation to complete, usually anywhere between 5 – 15 minutes depending on your connection and MacBook processing power.

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 will 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.



Configure Homestead Per Project

Homestead is configured using a YAML file, which contains configuration information regarding sites & files on your computer, databases to create etc. When you start a new project, you have to add an entry to this file and re-provision.

When using Homestead on a per-project basis, you keep this file with your project and it contains only that project’s settings. Enabling you to quickly clone a project if it’s under source control, and quickly boot a Homestead instance for that project.

This is especially helpful when setting an existing project up on a new computer, as you don’t need to manually set up Homestead and configure it for your project.

To create a Homestead.yaml for your project.

First, we need to add development Laravel/Homestead dependency to our project.

To do this cd into your project directory then execute:

 composer require laravel/homestead --dev

Once complete we can now use this package to initialize files Homestead will require and create the Homestead.yaml

php vendor/bin/homestead make

If you check your directory now you will notice a file, Homestead.yaml created.

ip: 192.168.10.10
memory: 2048
cpus: 1
provider: virtualbox
authorize: ~/.ssh/id_rsa.pub
keys:
    - ~/.ssh/id_rsa
folders:
    -
        map: /home/gary/code/sourcelink
        to: /home/vagrant/code
sites:
    -
        map: sourcelink.test
        to: /home/vagrant/code/public
databases:
    - homestead
name: sourcelink
hostname: sourcelink

We are now almost ready to run our project, all we need to do now is add an entry to our Host file for the website.
sudo gedit /etc/hosts
Then we add an entry for our new website and save the file
192.168.10.10 sourcelink.test
We can now just execute vagrant up and browse to the test site http://sourcelink.test

Summary

You should now have the complete Laravel and homestead environment setup on Mac OSX. Go ahead and enjoy your new development environment and go create the nextxt big thing.

Follow Me

Gary Woodfine

Helps businesses by improving their technical proficiencies and eliminating waste from the software development pipelines.

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.
Follow Me
%d bloggers like this: