Semantic Versioning and Composer
Published: 2020-01-28 09:40:07

---
title: Semantic Versioning and Composer
published: true
tags: 
canonical_url: https://codeinphp.github.io/post/semantic-versioning-and-composer/
---

Today any seriously written application/software follows the _[Semantic Versioning](http://semver.org/)_ (also called _SemVer_). It boils down to this (emphasis mine):

1. MAJOR version when you make **incompatible API changes**
2. MINOR version when you add functionality in a **backwards-compatible** manner
3. PATCH version when you make **backwards-compatible bug fixes**

Let's take example of [Symfony](http://symfony.com/) framework which follows SemVer. As of this writing, its stable version stands at `2.6.6`

**Symfony Versioning**

```
+-------+---------+ | Name | Version | +-------+---------+ | MAJOR | 2 | +-------+---------+ | MINOR | 6 | +-------+---------+ | PATCH | 6 | +-------+---------+
```

Here, we can see that MINOR and PATCH versions are backwards-compatible. This means if you install Symfony version `2.0.0` or `2.x.x` with `x` being any number, it will be backwards-compatible. Meaning if you had initially installed Symfony version `2.0.0` and now you want to install version `2.6.6`, you can do so **without worrying** it breaking functionality. However when Symfony announces a version starting with `3` as MAJOR version then it will most likely break some of the functionality with all previous version less than `3.x.x`. Thanks to SemVer, we can easily find that out now. Not to mention, almost all nicely written frameworks or libraries or software in general now follow SemVer. [Please read more about it](http://semver.org/).

As a PHP developer, [composer](https://getcomposer.org/) is something we can't do without these days. If you think you are a PHP developer and haven't started using composer yet, you are not a PHP developer DOT In that case, search on Google on what it is and its tutorials or see its [official documentation](https://getcomposer.org/doc/). In short, it is package manager for PHP that has superseded older methods such as PEAR, etc. It is like NPM for NodeJS or RubyGem for Ruby developers.

Let's say I want to install Symfony via composer, I can specify specific version to use in quite some ways like:

```
+--------------------------------+----------------------------------------------------------------+ | Version | Description | +--------------------------------+----------------------------------------------------------------+ | symfony/symfony: "2.6.6" | Exact 2.6.6 version | +--------------------------------+----------------------------------------------------------------+ | symfony/symfony: "\>= 2.6" | Any version greater than or equal to 2.6 like 2.6.7, 6.6.9 | +--------------------------------+----------------------------------------------------------------+ | symfony/symfony: "\>= 2.6,\<3.0" | Any version greater than or equal to 2.6 but less than 3 Major | +--------------------------------+----------------------------------------------------------------+ | symfony/symfony: "2.\*" | Any version as long as Major version is 2 | +--------------------------------+----------------------------------------------------------------+ | symfony/symfony: "\*" | Latest version | +--------------------------------+----------------------------------------------------------------+ | symfony/symfony: "dev-master" | Latest version from "master" branch | +--------------------------------+----------------------------------------------------------------+ | symfony/symfony: "dev-testing" | Latest version from "testing" branch | +--------------------------------+----------------------------------------------------------------+
```

We can also use special tilde character `~`:

```
+---------------------------+-----------------------------------------------------------------------+ | Version | Description | +---------------------------+-----------------------------------------------------------------------+ | symfony/symfony: "~2.6.6" | Any version starting from 2.6.0 to 2.6.x with x being any number | +---------------------------+-----------------------------------------------------------------------+ | symfony/symfony: "~2.6" | Any version starting from 2.0 to 2.x or 2.x.x with x being any number | +---------------------------+-----------------------------------------------------------------------+ | symfony/symfony: "~2" | Any Major version greater than or equal to 2 like 2.x or 5.x.x | +---------------------------+-----------------------------------------------------------------------+
```

Once you know how to specify versions in composer, you can easily make sure you are only installing those packages that are **backwards-compatible** unless you want fresh installation of some package. Similarly, if you have wrote some package, you should choose versioning sensibly so that users of your package don't go **incompatible** accidentally if you have not made Major changes to your package.

* * *

By the way, here is extremely useful [Composer cheat sheet](http://composer.json.jolicode.com/), have fun !

 ![](http://feeds.feedburner.com/~r/codeinphpfeed/~4/PDs-ovbIvJI)