Setting composer and PHP on your local machine for faster Workflows
In this section, we will cover some tips on using PHP and composer outside of the Homestead box to help with your workflow.
Getting ready
As with the preceding sections, you will need to have a terminal and decent internet. I will cover this using a Mac, but Windows and Linux have their systems to install the software. By default, you can install Xcode on a Mac and get pretty far with PHP, but it tends to be an older version of PHP. Here, we will use Homebrew to install PHP 5.6. We will also use Homebrew later on in this book as well.
How to do it...
- Visit the http://brew.sh/ site, and run the command they show there to install Homebrew on your Mac.
- Follow the instructions at https://github.com/Homebrew/homebrew-php to get the PHP5.6 setup.
- After you are done with step 2, add to your
~/.bash_profile
so that we can use this version of PHP:export PATH="$(brew --prefix php56)/bin:$PATH"
- Then, update your current session:
>source ~/.bash_profile
- Then, we will make sure our PHP is set up properly:
>which php
You will see the
/usr/local/opt/php56/bin/php
output and type:>php –v
This will show that you are running 5.6.19 or a higher version.
- Set up
mycrypt
as follows:>brew install php56-mcrypt
- Then, we will install composer as seen at https://getcomposer.org/download/:
>php -r "readfile('https://getcomposer.org/installer');" > composer-setup.php >php -r "if (hash('SHA384', file_get_contents('composer-setup.php')) === 625196afcca95a5abf44391188c695c6c1456e16154c75a211d238cc3bc5cb47') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" >php composer-setup.php >php -r "unlink('composer-setup.php');" >sudo mv composer.phar /usr/local/composer
Tip
What is ~/
? That is shorthand for your Home directory. When I use this, for example, >source ~/.bash_profile
, your operating system will know it is in your home folder, for example, /Users/alfrednutile/.bash_profile
.
How it works...
That was a lot of steps! Let's cover what we did and why. We began by installing Homebrew to make installing packages easier. We will periodically need to install packages such as Wget
, Webdriver
, and more as we progress through this book. Using the brew
command supplied by Homebrew makes installing these packages a snap.
Then, we used Brew to make sure we have a current version of PHP on our Mac. But considering we already have Homestead, why do this? There is some work you do outside of Homestead, for example, getting and installing Laravel using composer, running envoy, and more. And some of these you can run in Homestead, but you will see some speed difference outside of it. So, you still need it on your machine, but in this case, we are not so worried about it being the wrong version for one of our many applications.
The mcrypt
part of the installation took care of the extension that we need to run common commands such as php artisan key:generate
and other commands in Laravel.
We finalized the PHP setup with Bash shortcuts, so when we open the terminal, we are ready to use PHP and not the version that comes with Xcode on Mac.
We then use PHP to download and install composer, the biggest advancement in PHP since I started 15 years ago in my opinion, and you will see more of composer shortly.
Finally, we are ready to download Laravel!
There's more...
You could, of course, use Brew to install MySQL and more. But for now, we are going to leave all of this inside the Homestead box that we set up earlier.
See also
- Composer Install: https://getcomposer.org/download/
- Composer Docs : https://getcomposer.org/doc/
- Brew: http://brew.sh/