Laravel 5 Beauty - Setting up an OS X or Linux Machine

Getting your OS X or Linux machine ready for Laravel 5.1 development

Posted on February 22, 2015 in L5 Beauty
Note this is the second step of the tutorial (for OS X/Linux users). Please install everything below before continuing.

This chapter goes through the steps required to set up and install the supporting software for Laravel Homestead on an OS X or Linux machine. It is assumed VirtualBox and Vagrant were already installed from the Required Software and Components chapter.

Chapter Contents

Slight Variations with Linux

There are slight variations between the different Linux distributions. In particular, the package manager. CentOS and Fedora use yum as the package manager, Ubuntu uses apt. There is no official “package manager” with OS X other than the App Store, but homebrew is the unofficial OS X package manager. Regardless of the differences, the essence is pretty much the same across all *nix systems, including OS X.

Step 1 - Installing PHP

Often PHP will be pre-installed on your system. You can check the version from a terminal window.

Checking the PHP version

~> php --version
PHP 5.5.9-1ubuntu4.5 (cli) (built: Oct 29 2014 11:59:10)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
    with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies

Laravel 5.1 requires PHP version 5.5.9 or above. If you don’t have PHP installed, or it’s not at least version 5.5.9, then you’ll need to use your package manager to install PHP.

OS X Yosemite

Yosemite (the version of OS X at the time of this writing) ships with PHP 5.5.14. So no worries there.

Example installing PHP in Ubuntu

~> sudo apt-get install php5
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
  php5
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.

[snip]

Step 2 - Install Node.js

You’ll need Node.js installed to later use gulp.

Often, Node.js is already installed. You can check the version of npm to see if Node.js is installed on your system.

Checking the npm version

$> npm --version
1.5.0-alpha-4

If it’s not installed, there are a couple options for installing it. You can use your package manager to install it. With OS X you can install it with Homebrew. Or you can just go to nodejs.org/download and download the correct version for your operating system.

Figure 4.1 - Node.js Download Page

Node.js Download Page

Once installed, be sure to check the version to make sure node and npm are available in your path.

Checking node and npm versions

~> node --version
v0.10.29

~> npm --version
1.5.0-alpha-4

Step 3 - Install Gulp

Gulp in an integral part of rapid Laravel development. Use the Node package manager (NPM) to install gulp globally.

Installing gulp globally

~> npm install -g gulp
/usr/local/bin/gulp -> /usr/local/lib/node_modules/gulp/bin/gulp.js
gulp@3.8.10 /usr/local/lib/node_modules/gulp
[snip]

Checking the gulp version

~> gulp --version
[10:13:44] CLI version 3.8.10

Optionally Install Bower

You can optionally install bower globally if you wish to run bower from your Linux (or OS X) console. Personally, I use bower both from my OS X console and within the Homestead Virtual Machine, whichever I’m currently in.

Use the Node package manager (NPM), install bower globally.

Installing bower globally

~> npm install -g bower
/usr/local/bin/bower -> /usr/local/lib/node_modules/bower/bin/bower
bower@1.3.12 /usr/local/lib/node_modules/bower
[snip]

Checking the bower version

~> bower --version
1.3.12

Remember this only installs the programs globally

If you use gulp (or bower) within a particular project you’ll still need to install them locally within that project with a npm install (omitting the -g option). This will be covered later.

Step 4 - Install Composer

Composer is the package manager for PHP. It can easily be installed from a terminal window in *nix systems (including both OS X and Linux). An alternative method of installing Composer in OS X using Homebrew is presented at the bottom of this section.

Installing Composer

~> curl -sS https://getcomposer.org/installer | php
#!/usr/bin/env php
All settings correct for using Composer
Downloading...

Composer successfully installed to: /Users/chuck/composer.phar
Use it: php composer.phar

Once you have composer.phar downloaded, move it to the global path.

Moving composer.phar

~> sudo mv composer.phar /usr/local/bin/composer

And then check the version to make sure it’s accessible.

Checking the Composer version

~> composer --version
Composer version 1.0-dev (b23a3cd36870ff0eefc161a4638d9fcf49d998ba)\
2014-11-21 17:59:11

Install using Homebrew

In OS X, if you are using homebrew, you can install composer using the instructions below.

Alternative installation in OS X with Homebrew

~> brew update
~> brew tap homebrew/dupes
~> brew tap homebrew/php
~> brew install composer

Step 5 - Adding SSH Keys

If you haven’t already added a SSH key for your machine, you’ll need to do it.

Checking for SSH Keys

~> ls ~/.ssh
config id_rsa id_rsa.pub

If you don’t see id_rsa and id_rsa.pub create them with the following command. (Press [Enter] all the way through to use the defaults and set up the SSH key with no pass phrase.)

Creating SSH Keys

~> ssh-keygen -t rsa -C "your@email.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/Chuck/.ssh/id_rsa):
Created directory '/Users/Chuck/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:

Step 6 - Adding the Homestead box

This step downloads the Laravel Homestead Vagrant box.

Adding the Homestead box in Linux

~> vagrant box add laravel/homestead
==> box: Loading metadata for box 'laravel/homestead'
    box: URL: https://vagrantcloud.com/laravel/homstead

[snip]

It can take a little while to download on slow connections.

Step 7 - Installing Homestead

Now we’ll use composer to install the homestead command. The homestead command line utility makes it easy to control the Homestead Virtual Machine.

Step 7.1 - Globally requiring Homestead

Globally requiring Homestead 2.0

~> composer global require "laravel/homestead=~2.0"
Changed current directory to /home/chuck/.composer
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Installing symfony/process (v2.5.7)
    Loading from cache

  - Installing symfony/console (v2.5.7)
    Loading from cache

  - Installing laravel/homestead (v2.0.7)
    Loading from cache

Writing lock file
Generating autoload files

Step 7.2 - Updating Path

Composer just installed Homestead into the vendor directory of your Composer installation. (For example /home/chuck/.composer in Linux or /Users/Chuck/.composer in OS X).

In order to access homestead from any command prompt, add this path to your path variable. This should be added to whatever the startup script is for your operating system. Common startup files are: .bashrc, .bash_profile, .zshrc, etc.

At the bottom of your startup script add the following line:

Updating path in the startup script

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

Notice the extra ‘vendor/bin’ in the path?

We’re adding this so any time you’re within the base directory of a Laravel project you can easily access any vendor utilities provided in that project such as phpunit.

Step 7.3 - Verifying Homestead Installed

Close any existing terminal windows and open a new terminal window so the latest changes to the path will be in effect. Then check the version of homestead to verify it installed.

Checking the Homestead Version

~>homestead --version
Laravel Homestead version 2.0.7

Step 7.4 - Initialize Homestead

Once you’ve installed the homestead command and added the composer bin directory to your path, then you need to initialize Homestead.

Initializing Homestead

~> homestead init
Creating Homestead.yaml file...
Homestead.yaml file created at: /home/chuck/.homestead/Homestead.yaml

Remember

You only need to initialize Homestead once on your machine

Step 8 - Bring up the Homestead VM

To bring up Homestead for the first time we’ll create a Code directory to store our projects and use the homestead up command.

Bringing up Homestead for the 1st time

~> mkdir Code
~> homestead up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'laravel/homestead'...
==> default: Matching MAC address for NAT networking...
==> default Checking if box 'laravel/homestead' is up to date...

[snip]

Now the Homestead VM is running. If you exit the terminal window, Homestead is still running. It’ll remain active until you issue a homestead halt command from a terminal window.

Now you can log onto homestead with the homestead ssh command.

Shelling to homestead

~> homestead ssh
Welcome to Ubuntu 14.04.1 LTS (GNU/Linux 3.13.0-11-generic x86_64)

 * Documentation:  https://help.ubuntu.com/

  System information as of Fri Nov 28 04:24:01 UTC 2014

  System load:  0.0               Processes:           92
  Usage of /:   5.2% of 39.34GB   Users logged in:     0
  Memory usage: 33%               IP address for eth0: 10.0.2.15
  Swap usage:   0%                IP address for eth1: 192.168.10.10

  Graph this data and manage this system at:
    https://landscape.canonical.com/

  Get cloud support with Ubuntu Advantage Cloud Guest:
    http://www.ubuntu.com/business/services/cloud

Last login: Fri Nov 28 04:24:01 2014 from 10.0.2.2
vagrant@homestead:~$

Step 9 - Installing the Laravel Installer

For the last step we’ll install the Laravel installer. Do the following from your console (not from within the Homestead Virtual Machine).

Globally requiring Laravel Installer

~> composer global require "laravel/installer=~1.1"
Changed current directory to /Users/chuck/.composer
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Installing guzzlehttp/streams (2.1.0)
    Downloading: 100%

  - Installing guzzlehttp/guzzle (4.2.3)
    Downloading: 100%

  - Installing laravel/installer (v1.1.3)
    Downloading: 100%

Writing lock file
Generating autoload files

Since your path was already updated in Step 7.2 to contain composer’s bin directory, the laravel command should already be accessible from the Console window. Verify it by checking the version.

Checking the Laravel Version

~> laravel --version
Laravel Installer version 1.2.1

Congratulations!

You now have a virtual Ubuntu 64-bit machine, ready for developing your Laravel 5.1 web applications.

Recap

This chapter contained a series steps to follow in order to get Laravel Homestead up and running on your OS X or Linux machine. The good news is, these steps only have to be performed once.

The next chapter, Using Homestead, contains information about using Homestead.

comments powered by Disqus