What is Packagist and Why You Need It for WordPress Development
If there’s one important lesson I’ve learned in my years of working with WordPress, it’s that if you can find a way to do less work, absolutely take it. Obviously, I’m not encouraging careless development work. What I am an advocate for, however, is employing a series of tools that cut out unnecessary, repetitive steps in your process.
For those of you coding with PHP, you know there are certain libraries you like to call on often because they’re reliable and produce consistent and high-quality results in your development of websites. Wouldn’t it be great if you could cut out the time it takes to dig up those libraries, find the latest versions of them, and then add them into your site?
Of course it would!
Much like how we use Git repositories to store our plugin, theme, and website files, we have something called Packagist for PHP. One of the key differences between these two types of repositories, however, is that Packagist works just as well for the programmer that wants access to another developer’s PHP library as it does for the programmer that wants to store and share their own PHP library somewhere.
Today, let’s take a look at Packagist as both the contributor and the user, and why you should think about integrating this into your WordPress workflow going forward.
What Is Packagist and Why Do You Need It?
Packagist is a repository for PHP code libraries.
As a contributor, you can use Packagist to store your own PHP libraries and maintain version control. It’s also a great opportunity to get your name out there and bolster your reputation within the WordPress community for developing and sharing high-quality code.
As a bonus, you can log in and submit PHP packages with your Github account. This is especially convenient as it allows you to sync with Github where the source files live. Once a hook is set up, Packagist will then automatically grab any updates made in the Git repository and pull them into your package and log the new version. Again, it’s always important to look for ways to simplify the repetitive actions you’d otherwise take throughout the web development process.
As a user, you can search for PHP packages using Packagist and then incorporate them into your Composer dependency manager tool. If you’re coding with PHP, then you absolutely need this.
Much like how Github syncs to WordPress, Packagist is meant to work directly with Composer. Composer, as you know, is the tool you use to manage dependencies (like PHP libraries) for your WordPress site.
By using Packagist and Composer in conjunction with one another, you’ll cut down on any unnecessary back-and-forth between your libraries and site. The two tools speak to one another through a composer.json file. Basically, once you’ve found a library you want to use in Packagist, all you need to do is add the “name” of it to your Composer file and it pulls in everything included in that package, including the name of the tool, the latest version, and all the metadata the programmer included.
To make things even more convenient, you can actually add as many PHP libraries to your Composer file as you want—which is the beauty of this whole thing. There’s no need to go about creating multiple composer.json files every time. Your PHP Libraries are stored in an area of your root called “Vendors” and can all be managed simultaneously from within there.
Additionally, you won’t have to check in on your PHP libraries to see if updates have been issued. All it takes is a single line of code to quickly update all the PHP libraries you’ve added to your composer.json file.
How to Use Packagist as a User
If you’re ready to streamline the process of using PHP libraries and want to set up this connection between Packagist and Composer, here is what you need to do:
1. Download Composer if this is your first time using it.
1.6 million WordPress Superheroes read and trust our blog. Join them and get daily posts delivered to your inbox - free!
2. Create an account on Packagist.
3. Find the package you want to save to Composer. There is a search bar at the top or you can use the Browse function.
4. Open the package. You’ll find the command line at the top of the page. Copy this for use in Composer in the next step.
5. Navigate to the Composer terminal. Go to the root directory of your site and paste in the command line for your PHP library. Hit your return button and run the command. Your PHP library will now download the latest version of the package to your site.
6. The first time you do this, you’ll discover that a Vendor directory has been created in the root of your site. Everything you’ve downloaded from Packagist saves here now.
7. You will also now find a file called composer.json in your root folder. It will contain the following information:
There will be a “vendor/package” line for each PHP library dependency you add from Packagist. You can now manage all your dependencies here.
8. When you want to add additional packages, simple enter the following command line into Composer:
Your composer.json file will automatically update with the newly added package’s information.
9. The last thing you should be aware of is Composer’s rules for adding version numbers to your downloaded packages. The whole point in using Packagist and Composer (and Github, too) is to simplify your process. And you don’t want to get stuck with an outdated PHP library, right? Nor would you want to have to constantly check back in Github or Packagist to see when it was last updated.
The quick solution to this is an update command line. Simply type this into Composer and all your PHP libraries will update to their latest versions:
How to Use Packagist as a Contributor
If you’d like to take advantage of Packagist as a contributor of code, here is what you need to do:
1. Upload your package to Github or the repository of your choice.
2. Create an account on Packagist if you don’t have one already.
3. Create a composer.json file for your library. This will provide users with the metadata they need. Here’s Tom McFarlin’s example as a guide:
4. Before submitting your library, don’t forget to create a composer.lock file in Composer in order to maintain version control of your package.
5. Submit the link to your package from Github to Packagist. Don’t forget to give your package a unique name (that’s the “vendor/package” piece).
Save Development Time With Packagist
Any amount of time savings is a good thing when you’re working as a WordPress developer. It’s even better when you can cut down on the chance for human error, too. Both of these things can be accomplished by incorporating Composer and Packagist into your workflow, whether you’re a developer of PHP code or want to use someone else’s library with your own work.
P.S. Did you know WPMU DEV provides support for Composer to help you automate WordPress installs and test builds? Check out our Composer announcement post for all the details.