When new software comes out it has a version number like 8.2.0. If you're confused about what that means you're not alone. The software industry needed a standard way to describe software releases so we as consumers of the software could determine how big of a change it was and determine if we should upgrade. Semantic Versioning, or SemVer for short, creates that standard.
In this article, we'll discuss how SemVer strings are composed, how you can read them to understand what's changing quickly, what entries in our composer.json mean, and how you can create your own SemVer for published libraries (like composer libraries).
The Problem
One of the difficult challenges with software development is managing dependencies. It can be such a pain point we've even named it "dependency hell". In the days before Composer, we didn't know what version of different libraries might be installed on the server our software was being installed on. If we had multiple applications running on the same server we would run into conflicts all the time or we would develop our code using one version of a package only to have the server use a lower version without features we relied on. Thankfully Composer stepped in and create a solution to determine if a package would work with our software and localize it to just our application. We always want to make sure we're using the most up-to-date version of the libraries our application relies on. This makes sure we have the newest features and the latest patches for bugs. Composer uses SemVer to determine exactly what version of each package we can use without us manually having to figure it out.Anatomy of a SemVer String
A SemVer release string is broken into three numbers working left to right:- The major release
- The minor release
- The patch level
Using SemVer String
Now there are two ways that we can use SemVer. As a consumer of the libraries that use it or as a maintainer of one of these libraries.Composer
As PHP developers we use SemVer string mostly through Composer. Composer keeps a composer.json file that contains a listing of libraries we need and it uses SemVer to manage those dependencies for us. If we look into a composer.json file for a Laravel project we'll see lots of libraries listed. For example, we'll see `"laravel/framework": "^9.19",`. The "^9.19" is telling Composer what versions it can safely use. The carat (^) at the start is will keep us "locked" into the 9 major release branches so we don't accidentally upgrade to a new major release and break our application. We can also use the tilde (~) character to lock us into the current minor release. Another option is the asterisk ("*") character for any version. This is helpful for command line tools not tightly coupled to our software like PHP_CodeSniffer. PHP_CodeSniffer doesn't directly interact with our code so upgrading between major versions should be simple. We might also see ranges or >, <, or = signs. It's not common but it's possible. If you take only one thing away from this video it should be this. We should all be using the caret inside of our composer.json. Then we must run `composer update` at least once a month to make sure we're at the most current version of our libraries (with a degree of testing as well). Now the amazing thing about Composer and all of the modern package managers is that it allows our dependencies to have dependencies which in turn might have more dependencies. It's really dependencies all the way down. Each one uses SemVer but two libraries might support a different major version of the same library. Maybe both use the faker library but one only supports version 1 and the other supports versions 1 and 2. The composer will determine the best option for us. Hopefully, it's the newest version but we could be locked to a previous major release until all the libraries upgrade.Library Maintainer
Now as a library maintainer here are your rules to live by.- Breaking changes only in major versions
- New features in minor versions as long as they don't break backward compatibility
- Bug fixes in patch releases
What You Need to Know
- Semantic Versioning is a method to label releases
- Broken down into major, minor, and patch
- Use the caret notation in your composer.json files and run update regularly