Monday 10 July 2017

Installing Laravel on Windows - Tinker, Tenor ... no wait... Composer, Tinker ...

(I accidentally deleted my original posting of this attempting to fix colour issues with the theme, oops!  Thought I was just discarding a draft ... ack!)

So like many others I started reading about how to install Laravel and immediately backtracked.  What is composer?  Oh it manages packages for PHP.  My small experience in the Linux world has prepared me for the idea of a package manager.  For anyone who has been living in the Windows world and hasn't played in Linux then this little bit of weirdness helps save you time.

The simplest way I can think to explain it is like this: you've just decided to include Laravel as a module in your program.  Laravel relies on other 3rd party modules to provide things like database support, etc.  Which version of library x matches up with Laravel version y?  What happens if there's a security issue in library z and you have to figure out what version to migrate to - if you can find the right site once you're aware there's an issue?  There should be an easier way to do this - oh wait, that's a good part of what the package manager is going to help you with.  Someone maintaining Laravel changes a dependency in their configuration and magic happens and you can get the update from the command line without having any idea what the change was about, unless you want to know more.

Step 1 is to install PHP to your local machine.  I couldn't think of a good reason not to install PHP7 since this is a new project and has no legacy considerations so that's what I did by following the instructions at Dac Chartrand's blog.  I chose the zip file and simply extracted it into C:\PHP7 since this machine isn't a live server for anything.

Dac's instructions aren't specialized to include information about configuration of PHP for use with Laravel but they were more than good enough to get up and running with PHP and composer.

The section I'm going to expand on here is where Dac writes "Scroll down to the DLL extensions section and uncomment the extensions you want to use". The first thought I have is "how do I know what I want to use?" given my seconds of experience doing this. No sweat, don't uncomment any, finish his install and return to the php.ini file after you've confirmed things are working.

When you move along to installing composer you'll have to make an edit to your php.ini. I followed his updated and externally linked instructions for downloading composer and I hit a snag...

C:\PHP7>php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" 
PHP Warning:  copy(): Unable to find the wrapper "https" - did you forget to enable it when you configured PHP? in Command line code on line 1

In order to resolve this you need to uncomment one of those extensions we ignored, specifically uncomment the line that reads extension=php_openssl.dll.  If the php package changes somewhere down the line I'm sure a quick google will return the appropriate Stack Overflow answer for you.  Once I did this the rest of the steps worked like a charm.

Step 2 involves returning to the Laravel installation documentation and reading the server requirements.  PHP version is high enough and we just enabled OpenSSL.  PDO, Mbstring are also already in your php.ini for windows, so if you're doing this naively without being aware of what these things are doing, you can simply uncomment the following lines:

  • extension=php_mbstring.dll
  • extension=php_pdo_firebird.dll
  • extension=php_pdo_mysql.dll
  • extension=php_pdo_oci.dll
  • extension=php_pdo_odbc.dll
  • extension=php_pdo_pgsql.dll
  • extension=php_pdo_sqlite.dll

Here lies snag #2.  When I ran the install command line for Laravel's installer I got the following warnings:

C:\PHP7>composer global require "laravel/installer"
Warning: PHP Startup: Unable to load dynamic library 'ext\php_pdo_firebird.dll' - The specified module could not be found.
PHP Warning:  PHP Startup: Unable to load dynamic library 'ext\php_pdo_oci.dll' - The specified module could not be found.

Of course this is another wtf moment right?  I just downloaded the package from php.net how can it not be complete?  The files are even in the directory!?!

Before I explain how to get rid of the warning let's look at what these things are.  PDO means PHP Data Object ... yay, tells me nothing, next?  PDO is used here to encapsulate the database technology and provide you with a standard PHP interface so that you don't have to deal with the database server at the technology level - that is, you should be able to write a php command to execute a query and have it work regardless of whether you've chosen mySQL, sqLite, or a number of other database technologies - or even more to the point, if you change database technology down the line you don't have to rewrite your website.  THAT is what this section is about.  If you know which technology you will work with you can PROBABLY comment out the other PDO lines and live life with no further worries until you need to use one of the others.  If you KNOW you're using mysql then you probably only need to uncomment that line.  There may be some hidden gotcha down the line, but at my current level of understanding I'm not aware of any.  As another side note, if you leave them uncommented then I believe most Linux php installations will support those extensions without the need for the following fixes but we'll deal with the linux php.ini when we get there.  Remember this is strictly about getting this thing working on Windows.

Ok, so maybe you just want to fix the errors anyways ... which is what I initially did because I wasn't thinking about what I was doing.  I just wanted the warnings to go away and was not confident I could comment the extensions back out.

The answer is that you need fbclient.dll which can be obtained from firebirdsql.org. I downloaded the Win32 zip file and extracted the dll to my PHP7 directory and the first warning died. To kill the second you need to obtain files from Oracle which can be found at Orcale's site, search for "Enabling the PHP OCI8 Extension on Windows". I'm going to move forward with the Stack Overflow advice - I don't plan on using Oracle databases so I'm going to comment the line back out. I'm also going to guess I could have commented out the Firebird line and lived a happy life. This is all about database access and I think I'm only going to need mysql and sqlite access for this project but ... live and learn.

So just Tokenizer and XML PHP extensions left to go. According to NuSphere Tokenizer and XML are now built into the Windows version so let's see what happens from here.

Rerunning the composer command line completes after installing some packages.

Ok so the next step says
Make sure to place the $HOME directory (or the equivalent directory for your OS) in your $PATH so the laravel executable can be located by your system.
I never gave it a path name so where is it right?  The installer ended up in my Users\username\AppData\Roaming\Composer\vendor\laravel\installer directory.  If you can't find it try dir /s \laravel*  and it should turn up.

Change into that directory and issue the command php laravel new project-name ... my experience was that it ignored path names so just move the folder after the install completes.

In hindsight using the composer create-project method probably would have been easier but hey, reading documentation that looks like step by step instructions top to bottom never leads anyone astray right? :-)

This got me up and running enough to follow the remaining steps easily enough.  If you want to work more easily I suggest adding XAMPP or something similar to your new installation.  For a very useful XAMPP tip under Windows 10 see how to free up port 80 for Apache on Stack Overflow.

No comments:

Post a Comment