Bitbucket & Composer

The next step in my Laravel self-education is building my own packages and figuring out how to pull them into a project, using Composer, when they are needed.

We use private Bitbucket git repositories at work since our projects are for clients and not open source. Because of that, it is a little more tricky to pull in the packages that I plan on developing. Luckily, Bitbucket is popular enough that Composer supports it and makes it easy to pull a repository into your project.

I’m not going to go into detail regarding how to build your package in this post. I’ll save that for another day. You can find general information on package development in the Laravel documentation. Instead, I’m going to tell you how to pull a package that you’ve already built into your Laravel project.

First, find the composer.json file in your project root. Open that up in your favorite IDE so that you can make some changes to it.

You will need to add a repositories block. This will tell Composer where to find your package so that it can pull it into your project. Add the following block to the file, replacing the Bitbucket repository url with the url for your repository. You can put it anywhere in the file, but I like to put it right after the require and require-dev blocks. The type value of vcs tells Composer that it is a version control system repository.

Then you will need to add your package to the require block.

To install your package, open up your favorite command line utility and run the following command.

The --lock flag will update your composer.lock file and install the new package. I like doing this rather than composer update so that the other packages I’m using don’t get updated and cause problems before I’m ready to fix them.

Composer will have downloaded your package and put it in the /vendor/ directory …

Your package is now ready for you to use in your project. Just don’t forget to add your service provider to the /config/app.php file.

If Composer gives you warnings about not being able to find git on your server, you most likely will need to add the path to the git executable to the global PATH variable. To do this, open the .bashrc file in the root of your account directory (i.e. /home/account/.bashrc) with your favorite command line editing tool (I use nano). Add the following code to the file.

If the PATH variable is already being set in the file on your server, then just add the path to the git executable (/usr/local/cpanel/3rdparty/bin in my case) to the end of the list (prepending with a colon).