Setting Up A Local GitHub Hosted PHP Project on OS X

This article is part of the series Asher’s Tutorials

I’ve started a new GitHub project titled asherwunk/phuxer, which is essentially a PHP based textual game/chat server.  The original idea is to build a MU* server in PHP, but if I’m building a text processing server from the ground up, it’s going to do quite a number of things.  My ambitions are for it to be able to implement some IRC, Jabber, MU*, and Web functions all rolled into one to become a singular powerhouse of text-based role-playing and socializing.

Another reason I’m building it is to learn about implementing programming languages, as in building lexers and parsers and what not.  One of the things I thought would be cool, and this may or may not be fully realized, is if the engine was able to divorce the storage and implementation of game world objects from their programming.  Then, the game world could load and execute programs in different languages, enabling MUSH code, MUF, MPI, LPC and such to live side by side.  This is of course, probably not going to happen, but it’d be kind of neat!

I usually use Cloud9 for my programming development (as I have done on projects such as asherwunk/phabstractic), however in order to debug the server functionality of the programming I have to have more control over the host box.  There are a few ports openable and listenable on Cloud9, and I was able to successfully serve from a React PHP HTTP server from the installation, but I was unable to telnet to the port, which is necessary.  Likewise, Cloud9 for various reasons doesn’t have telnet default installed.

So now I’m programming on my own box with my own tools.  In order to do this, I need four functionalities: PHP7, Github, Composer, and PHPUnit.  So let’s see how we can make this happen.

Developing on OS X

OS X is a very powerful operating system provided by Apple for its desktop computers.  The nice thing about OS X is that you can get the tools for programming for the operating system for free.  In order to install new packages and other command-line tools, which we’re going to be doing in this post, you’ll need at least a compiler.  A compiler takes programming code and turns it into a program the computer can understand.  To get all the development tools, including the command-line tools, available for an OS X programmer you must install the developer package XCode from the Mac App Store:

xcode

When I installed XCode it was a paaaiiinn.  I don’t know why but it pretty much hung for a long time and then updated VERY VERY slowly.  I almost restarted and rebooted my computer, which would’ve probably screwed stuff up, but I read that this could occur, so I just let it sit.  It actually did complete all the way, but I thought my computer was broken a couple times.  It is sloooooow.

Once you have that installed you should now have all your needed developer tools.  Now it’s time we worked with some package management.

Homebrew

OS X is based on a UNIX environment and its terminal reflects that.  This nice thing about OS X, in my opinion, is that you can run and compile any code that is basically made to compile for a standard Linux system.  There are exceptions of course, but there have been many many times I’ve downloaded some source straight from the internet and compiled it successfully for use on my machine.

However, something OS X is somewhat missing is a package manager.  For my Ubuntu box, I can use all sorts of commands much like apt-get to install modules of software that I’d otherwise have to download, dependency check, and compile by hand.

Enter Homebrew for OS X, a package manager much like those included in other Linux distros.

Homebrew is fairly easy to use, and fairly easy to install.  Once you have XCode installed, open a terminal window (from the Utilities folder of the Applications), and type:

Now, it’ll do it’s thing, and might ask you for some input, and voila, you have a package manager.

I actually had to install wget onto my machine (it was missing it), which is surprising, but there you go.  So, for example, to install something like wget you might follow these steps:

  1. Search to see what packages are available by typing:   brew search wget
  2. On the console, homebrew will list the available packages with a checkmark next to the packages you have already installed.
  3. In my case, I wanted the wget utility, so now that I know the name, I can install by typing:   brew install wget

There are a various number of commands for brew.  You can find more information on using brew on their Github page.

PHP 7

Now that we have the developer tools to compile stuff and a package manager to make our lives easier, it’s time we install PHP 7.  I like the latest version of PHP, and it’s super fast, so I’ve decided that that’s what I want to use.  OS X comes preinstalled with an earlier version of PHP, 5.6 or so, so we’re going to install PHP 7 on our own.

Installing PHP7 with brew is as simple as typing:  brew install php70

It’ll go through all the necessary complications and dependencies, ending up with an installation of PHP 7 with many default extensions.  If you are interested in installing further extensions and other functionality perform a  brew search php70 to see what other packages are available for install.

We’re going to want to use this version of PHP on the command line for our project, but when you type in  PHP --version we still get the old version.  Here’s how to solve this using ~/.bashrc (you’ll have to edit your favorite shell rc file if you are using something else).  Add the following line to ~/.bashrc:

If you’re not sure how to do that try:

  1. Type  pico .bashrc at the command line in your home folder (~)
  2. copy and paste the above line into the file
  3. Type Control-O and enter to output the file to your home directory
  4. Type Control-X to exit the editor
  5. close and restart your terminal session

Now when you type  PHP --version you should receive something like the following:

GitHub

My projects are hosted for download on my GitHub account.  I want to clone my phuxer project onto my local computer.  I can use the command line tools for git, and for updating, merging, committing, and pushing I probably will.  But Github actually provides a very handy way of using the utility with a User Interface.

For OS X we need to download the Github client for mac at https://desktop.github.com

Now that we have the desktop client, let’s launch it.  After you launch the program it’ll ask you to log into Github with your account so that the desktop client can be linked.  As well, it should provide a button during the process for installing the command line tools for git, click on that as well.  Once you’ve done that you should see something like this:

githubscreenshot

I followed these steps to clone Phuxer onto my computer:

  1. Click on the + sign in the upper left
  2. Click on the clone tab for access to your repositories
  3. Select one of your repositories and then specify where on your file system it will be cloned

Voila, you now have a git repository cloned on to your desktop.  When you are in the project directory in the command line you should now be able to type in git commands.  If you try to do a push you’ll enter in your GitHub username and password.  You should be good to go then.

Composer

Now that I have my repository, I need to fill in the dependencies for my PHP.  I’m planning to use the react/react package and the asherwunk/phabstractic packages that are available on packagist.  I am also planning to use PHPUnit in my development, so that’s specified in my composer.json file as well.  For Phuxer, here is the first draft of its composer file:

So how do we get composer?

Well, let’s run the installer from the official site.  In your command line, type:

This will give you the ability to type  PHP composer.phar <composer commands here> on your command line, but what if we just want to be able to interact with composer as a global command line utility?  Well, let’s do a little command line moving and aliasing magic. First, move composer.phar to your bin directory, and then create an alias in your ~/.bash_profile pointing to it:

  1. Type  sudo mv composer.phar /usr/local/bin/ into your command line.  You’ll probably be prompted to enter your administration credentials.
  2. Type  pico .bash_profile at the command line in your home folder (~)
  3. Type the following line at the end of that file using the text editing interface:
    alias composer="php /usr/local/bin/composer.phar"
  4. As before, Type Control-O and enter to output the file to your home directory
  5. Type Control-X to exit the editor
  6. close and restart your terminal session

Now you should be able to type  composer <your command here> in your project directory and expect the proper result.

So I went ahead and did that, I went into my project directory and executed  composer update , and everything seemed to come out okay.  Now that I have the dependencies, let’s get phpunit up and running.

PHPUnit

Unit testing, or at least some form of testing, is important in a project.  Like Composer, phpunit is PHP based, but we can do a number of things to make it as easy to execute as Composer above.

Here’s where I had to install wget on my machine.  For some reason it just didn’t have it, so I executed   brew install wget on the command line at this point and successfully got it on there.

This is pretty simple, straight from the phpunit documentation:

  1. At the command line, type  wget https://phar.phpunit.de/phpunit.phar
  2. Then type,  chmod +x phpunit.phar
  3. And lastly, move it into your bin directory,  sudo mv phpunit.phar /usr/local/bin/phpunit

At this point I was able to type phpunit at the command line and for my own project, seeing as how there were no tests to run, received basic testing information.

An Editor

I actually tried using Aptana again, because I rememebr it being quite a bit like Cloud9, but I downloaded it and ran it today on my El Capitan machine… and it sucked.  Hard.  I hated it.

So I’ll be editing my PHP in a text editor, rather than an IDE.  If you want debugging support, you’ll want to use an IDE and point your project to your project directory, and I may end up doing that eventually, but for now I’ll be using a text editor.

There are a number of editors to suggest, and I’ll make a post regarding some of them, but for now I just use the editor married to Github for now, Atom.  I got turned on to this editor when I was investigating ways to make desktop apps using JavaScript and HTML.  It runs on the Electron code that Github has also put together.

Conclusion

It’s not too hard to set up a basic command line PHP coding environment using github, composer, and phpunit.  This tutorial covered how to set up the development environment if you’re using php purely from the command line.  This tutorial did not cover *AMP installations, such as getting your PHP to run in web server environments.

If you appreciate my tutorials please help support me through my Patreon.

photo credit: Working with music mixer via photopin (license)

kadar

I'm just a wunk, trying to enjoy life. I am a cofounder of http//originalpursuitssoc.com/ and I like computers, code, creativity, and friends.

You may also like...

Leave a Reply

%d bloggers like this: