2019-11-18 05:58:33 +01:00
---
title: Composer
summary: What is composer and how to use it with Silverstripe CMS
---
2021-06-30 11:48:52 +02:00
# Using Silverstripe CMS with Composer
2012-11-05 04:30:35 +01:00
2019-11-18 08:02:00 +01:00
## Requirements
2012-11-03 06:00:58 +01:00
2021-06-30 11:48:52 +02:00
[Composer ](http://getcomposer.org/ ) is a package management tool for PHP that lets you install and upgrade Silverstripe CMS
2021-06-22 10:41:48 +02:00
and its modules. We also have separate instructions
for [installing modules with Composer ](/developer_guides/extending/modules ).
2012-11-03 06:00:58 +01:00
2021-06-22 10:41:48 +02:00
Before installing Composer you should ensure your system has the version control
system, [Git installed ](http://git-scm.com/book/en/v2/Getting-Started-Installing-Git ). Composer uses Git to check out
2021-12-13 09:05:33 +01:00
the code dependencies you need to run your Silverstripe CMS website from the code repositories maintained on GitHub.
2015-04-13 03:28:13 +02:00
2021-06-22 10:41:48 +02:00
Next, [install composer ](https://getcomposer.org/download/ ). For our documentation we assume the `composer` command is
installed globally. You should now be able to run the command:
2012-11-11 23:48:49 +01:00
2017-01-24 04:52:53 +01:00
```
composer help
```
2012-11-11 23:48:49 +01:00
2012-11-06 00:26:43 +01:00
## Create a new site
2012-11-03 06:00:58 +01:00
2021-06-22 10:41:48 +02:00
Composer can create a new site for you, using the installer as a template. By default it will download the latest stable
version:
2012-11-03 06:00:58 +01:00
2017-01-24 04:52:53 +01:00
```
2019-11-18 08:02:00 +01:00
composer create-project silverstripe/installer my-project
2017-01-24 04:52:53 +01:00
```
2012-11-03 06:00:58 +01:00
2021-06-22 10:41:48 +02:00
If you want to get all additional fixtures for testing, such as behat and phpunit configuration, an
example `.env.example` file, and all documentation, then it's recommended to use `--prefer-source`
2017-06-30 03:19:18 +02:00
to include these files.
2021-06-22 10:41:48 +02:00
If you want a minimal installation with the bare essentials to get working without any additional overhead, and don't
plan on contributing back changes to framework, use `--prefer-dist` (default) for a more lightweight install.
2017-06-30 03:19:18 +02:00
2021-06-22 10:41:48 +02:00
`./my-project` should be the root directory where your site will live. For example, on OS X, you might use a
subdirectory of `~/Sites` . As long as your web server is up and running, this will get all the code that you need. Now
visit the site in your web browser, and the installation process will be completed.
2012-11-26 04:29:20 +01:00
2019-11-18 08:02:00 +01:00
You can also specify a version to download that version explicitly, i.e. this will download the older `4.3.3` release:
2017-01-24 04:52:53 +01:00
```
2019-11-18 08:02:00 +01:00
composer create-project silverstripe/installer ./my-project 4.3.3
2017-01-24 04:52:53 +01:00
```
2012-11-03 06:00:58 +01:00
2021-06-22 10:41:48 +02:00
When `create-project` is used with a release version like above, it will try to get the code from archives instead of
2021-06-30 11:48:52 +02:00
creating git repositories. If you're planning to contribute to Silverstripe CMS,
2013-01-04 12:21:07 +01:00
see [Using development versions ](#using-development-versions ).
2012-11-04 09:50:56 +01:00
2012-11-03 06:00:58 +01:00
## Adding modules to your project
2021-06-30 11:48:52 +02:00
Composer isn't only used to download Silverstripe CMS, it is also used to manage all Silverstripe CMS modules.
You can find thousands of modules on [https://addons.silverstripe.org ](https://addons.silverstripe.org ).
Installing a module can be done with the following command:
2017-01-24 21:33:23 +01:00
```
2019-11-18 08:02:00 +01:00
composer require silverstripe/blog
2017-01-24 21:33:23 +01:00
```
2012-11-05 04:30:35 +01:00
2021-06-22 10:41:48 +02:00
This will install the `silverstripe/blog` module in the latest compatible version. If you know the specific version you
want to install already (such as `^2` ), you can add it after the package name as
a [version constraint ](http://getcomposer.org/doc/01-basic-usage.md#the-require-key ):
2013-04-18 16:08:08 +02:00
2017-01-24 04:52:53 +01:00
```
2019-11-18 08:02:00 +01:00
composer require silverstripe/blog ^2
2017-01-24 04:52:53 +01:00
```
2013-04-18 16:08:08 +02:00
2019-11-18 05:58:33 +01:00
[warning]
2021-06-22 10:41:48 +02:00
**Version constraints:** `master` is not a legal version string - it's a branch name. These are different things. The
version string that would get you the branch is `dev-master` . The version string that would get you a numeric branch is
a little different. The version string for the `4` branch is `4.x-dev` .
2019-11-18 05:58:33 +01:00
[/warning]
2012-11-11 23:58:41 +01:00
## Updating dependencies
2021-06-22 10:41:48 +02:00
Except for the control code of the Voyager space probe, every piece of code in the universe gets updated from time to
2021-06-30 11:48:52 +02:00
time. Silverstripe CMS modules are no exception.
2012-11-11 23:58:41 +01:00
To get the latest updates of the modules in your project, run this command:
2017-01-24 04:52:53 +01:00
```
2019-11-18 08:02:00 +01:00
composer update
2017-01-24 04:52:53 +01:00
```
2012-11-11 23:58:41 +01:00
2021-06-22 10:41:48 +02:00
Updates to the required modules will be installed, and the `composer.lock` file will get updated with the specific
commits and version constraints for each of them.
2012-11-11 23:58:41 +01:00
## Deploying projects with Composer
2021-06-22 10:41:48 +02:00
When deploying projects with composer, you could just push the code and run `composer update` . This, however, is risky.
In particular, if you were referencing development dependencies and a change was made between your testing and your
deployment to production, you would end up deploying untested code. Not cool!
2012-11-11 23:58:41 +01:00
2021-06-22 10:41:48 +02:00
The `composer.lock` file helps with this. It references the specific commits that have been checked out, rather than the
version string. You can run `composer install` to install dependencies from this rather than `composer.json` .
2012-11-11 23:58:41 +01:00
2017-01-24 04:52:53 +01:00
So your deployment process, as it relates to Composer, should be as follows:
2012-11-11 23:58:41 +01:00
2021-06-22 10:41:48 +02:00
* Run `composer update` on your development version before you start whatever testing you have planned. Perform all the
necessary testing.
* Check `composer.lock` into your repository.
* Deploy your project code base, using the deployment tool of your choice.
* Run `composer install --no-dev -o` on your production version. In this command, the `--no-dev` command tells Composer
not to install your development-only dependencies, and `-o` is an alias for `--optimise-autoloader` , which will
convert your PSR-0 and PSR-4 autoloader definitions into a classmap to improve the speed of the autoloader.
2013-01-04 23:26:21 +01:00
2015-03-05 22:28:07 +01:00
## Composer managed modules, Git and .gitignore
2021-06-30 11:48:52 +02:00
Modules and themes managed by Composer should not be committed with your projects source code. Silverstripe CMS ships with
2021-06-22 10:41:48 +02:00
a [.gitignore ](http://git-scm.com/docs/gitignore ) file by default which prevents this. For more details
read [Should I commit the dependencies in my vendor directory? ](https://getcomposer.org/doc/faqs/should-i-commit-the-dependencies-in-my-vendor-directory.md )
.
2015-03-05 22:28:07 +01:00
2019-11-18 08:02:00 +01:00
## Dev Environments for Contributing Code {#contributing}
2013-01-04 23:26:21 +01:00
2021-06-30 11:48:52 +02:00
So you want to contribute to Silverstripe CMS? Fantastic! You can do this with composer too. You have to tell composer three
2021-06-22 10:41:48 +02:00
things in order to be able to do this:
2013-01-04 23:26:21 +01:00
2021-06-22 10:41:48 +02:00
- Keep the full git repository information
- Include dependencies marked as "developer" requirements
- Use the development version, not the latest stable version
2013-01-04 23:26:21 +01:00
2013-02-20 13:24:40 +01:00
The first two steps are done as part of the initial create project using additional arguments.
2017-01-24 04:52:53 +01:00
```
2019-11-18 08:02:00 +01:00
composer create-project --keep-vcs --dev silverstripe/installer ./my-project 4.x-dev --prefer-source
2017-01-24 04:52:53 +01:00
```
2013-01-04 23:26:21 +01:00
2021-06-22 10:41:48 +02:00
The process will take a bit longer, since all modules are checked out as full git repositories which you can work on.
The command checks out from the 4.x release line. To check out from master instead, replace `4.x-dev`
with `dev-master` (more info
on [composer version naming ](http://getcomposer.org/doc/02-libraries.md#specifying-the-version )).
2013-01-04 23:26:21 +01:00
The `--keep-vcs` flag will make sure you have access to the git history of the installer and the requirements
2021-06-30 11:48:52 +02:00
The `--dev` flag is optional, and can be used to add a couple modules which are useful for Silverstripe CMS development:
2013-01-04 23:26:21 +01:00
2021-06-22 10:41:48 +02:00
* The `behat-extension` module allows running [Behat ](http://behat.org ) integration tests
* The `docsviewer` module will let you preview changes to the project documentation
2021-06-30 11:48:52 +02:00
* The `buildtools` module which adds [phing ](http://phing.info ) tasks for creating Silverstripe CMS releases
2013-01-04 23:26:21 +01:00
2021-06-22 10:41:48 +02:00
Once the `create-project` command completes, you need to edit the `composer.json` in the project root and remove
the `@stable` markers from the `silverstripe/cms` and `silverstripe/framework` version entries.
Another `composer update --dev` call will now fetch from the development branch instead. Note that you can also convert
an existing composer project with these steps.
2013-02-20 13:24:40 +01:00
2021-06-22 10:41:48 +02:00
Please read the ["Contributing Code" ](/contributing/code ) documentation to find out how to create forks and send pull
requests.
2012-11-11 23:48:49 +01:00
2012-11-06 00:26:43 +01:00
# Advanced usage
2012-11-05 04:30:35 +01:00
## Manually editing composer.json
2021-06-22 10:41:48 +02:00
To remove dependencies, or if you prefer seeing all your dependencies in a text file, you can edit the `composer.json`
file. It will appear in your project root, and by default, it will look something like this:
2017-01-24 04:52:53 +01:00
```json
{
2017-04-23 11:14:12 +02:00
"name": "silverstripe/installer",
2021-06-22 10:41:48 +02:00
"description": "The Silverstripe Framework Installer",
2017-04-23 11:14:12 +02:00
"require": {
2019-11-18 08:02:00 +01:00
"php": ">=7.3",
"silverstripe/cms": "^4",
"silverstripe/framework": "^4",
"silverstripe-themes/simple": "^3"
2017-04-23 11:14:12 +02:00
},
"require-dev": {
2019-11-18 08:02:00 +01:00
"silverstripe/docsviewer": "^3"
2017-04-23 11:14:12 +02:00
},
"minimum-stability": "dev",
"prefer-stable": true
2017-01-24 04:52:53 +01:00
}
```
2021-06-22 10:41:48 +02:00
To add modules, you should add more entries into the `"require"` section. For example, we might add the blog and forum
modules. Be careful with the commas at the end of the lines!
2012-11-03 06:00:58 +01:00
2012-11-06 00:26:43 +01:00
Save your file, and then run the following command to refresh the installed packages:
2012-11-03 06:00:58 +01:00
2017-01-24 04:52:53 +01:00
```
composer update
```
2012-11-06 00:26:43 +01:00
2012-11-26 04:29:20 +01:00
## Using development versions
2021-06-22 10:41:48 +02:00
Composer will by default download the latest stable version of silverstripe/installer. The `composer.json` file that
comes with silverstripe/installer may also explicitly state it requires the stable version of cms and framework - this
is to ensure that when developers are getting started, running `composer update` won't upgrade their project to an
unstable version
2012-11-26 04:29:20 +01:00
2021-06-22 10:41:48 +02:00
However it is relatively easy to tell composer to use development versions. Not only is this required if you want to
2021-06-30 11:48:52 +02:00
contribute back to the Silverstripe CMS project, it also allows you to get fixes and API changes early.
2012-11-26 04:29:20 +01:00
2021-06-22 10:41:48 +02:00
This is a two step process. First you get composer to start a project based on the latest unstable
silverstripe/installer
2012-11-26 04:29:20 +01:00
2017-01-24 04:52:53 +01:00
```
2019-11-18 08:02:00 +01:00
composer create-project silverstripe/installer ./my-project dev-master
2017-01-24 04:52:53 +01:00
```
2012-11-26 04:29:20 +01:00
2017-11-08 05:10:58 +01:00
Or for the latest development version in the 4.0.x series
2012-11-26 04:29:20 +01:00
2017-01-24 04:52:53 +01:00
```
2019-11-18 08:02:00 +01:00
composer create-project silverstripe/installer ./my-project 4.0.x-dev
2017-01-24 04:52:53 +01:00
```
2012-11-26 04:29:20 +01:00
2012-11-06 00:26:43 +01:00
## Working with project forks and unreleased modules
2021-06-22 10:41:48 +02:00
By default, Composer will install modules listed on the Packagist site. There are a few reasons that you might not want
to do this. For example:
2012-11-06 00:26:43 +01:00
2021-06-22 10:41:48 +02:00
* You may have your own fork of a module, either specific to a project, or because you are working on a pull request
* You may have a module that hasn't been released to the public.
2012-11-06 00:26:43 +01:00
2021-06-22 10:41:48 +02:00
There are many ways that you can address this, but this is one that we recommend, because it minimises the changes you
would need to make to switch to an official version in the future.
2012-11-06 00:26:43 +01:00
This is how you do it:
2021-06-22 10:41:48 +02:00
* **Ensure that all of your fork repositories have correct composer.json files.** Set up the project forks as you would
a distributed package. If you have cloned a repository that already has a composer.json file, then there's nothing you
need to do, but if not, you will need to create one yourself.
2012-11-06 00:26:43 +01:00
2021-06-22 10:41:48 +02:00
* **List all your fork repositories in your project's composer.json files.** You do this in a `repositories` section.
Set the `type` to `vcs` , and `url` to the URL of the repository. The result will look something like this:
2012-11-06 00:26:43 +01:00
2017-01-24 04:52:53 +01:00
```json
{
2017-04-23 11:14:12 +02:00
"name": "silverstripe/installer",
2021-06-22 10:41:48 +02:00
"description": "The Silverstripe Framework Installer",
2017-04-23 11:14:12 +02:00
"repositories": [
{
"type": "vcs",
"url": "git@github.com:sminnee/silverstripe-cms.git"
}
]
2017-01-24 04:52:53 +01:00
}
```
2012-11-06 00:26:43 +01:00
2021-06-22 10:41:48 +02:00
* **Install the module as you would normally.** Use the regular composer function - there are no special flags to use a
fork. Your fork will be used in place of the package version.
2012-11-06 00:26:43 +01:00
2017-01-24 04:52:53 +01:00
```
composer require silverstripe/cms
```
2012-11-06 00:26:43 +01:00
2021-06-22 10:41:48 +02:00
Composer will scan all of the repositories you list, collect meta-data about the packages within them, and use them in
favour of the packages listed on packagist. To switch back to using the mainline version of the package, just remove
the `repositories` section from `composer.json` and run `composer update` .
2012-11-06 00:26:43 +01:00
2013-03-20 11:37:20 +01:00
Now add an "upstream" remote to the original repository location so you can rebase or merge your fork as required.
2017-01-24 04:52:53 +01:00
```
cd cms
git remote add -f upstream git://github.com/silverstripe/silverstripe-cms.git
```
2013-03-20 11:37:20 +01:00
2021-06-22 10:41:48 +02:00
For more information, read
the ["Repositories" chapter of the Composer documentation ](http://getcomposer.org/doc/05-repositories.md ).
2012-11-06 00:26:43 +01:00
2012-11-06 04:48:03 +01:00
### Forks and branch names
2021-06-22 10:41:48 +02:00
Generally, you should keep using the same pattern of branch names as the main repositories does. If your version is a
fork of 4.0, then call the branch `4.0` , not `4.0-myproj` or `myproj` . Otherwise, the dependency resolution gets
confused.
2012-11-06 04:48:03 +01:00
2021-06-22 10:41:48 +02:00
Sometimes, however, this isn't feasible. For example, you might have a number of project forks stored in a single
repository, such as your personal GitHub fork of a project. Or you might be testing/developing a feature branch. Or it
might just be confusing to other team members to call the branch of your modified version `4.0` .
2012-11-06 04:48:03 +01:00
2021-06-22 10:41:48 +02:00
In this case, you need to use Composer's aliasing feature to specify how you want the project branch to be treated, when
it comes to dependency resolution.
2012-11-06 04:48:03 +01:00
2017-01-24 04:52:53 +01:00
Open `composer.json` , and find the module's `require` . Then put `as (core version name)` on the end.
2012-11-06 04:48:03 +01:00
2017-01-24 04:52:53 +01:00
```json
{
2017-04-23 11:14:12 +02:00
"require": {
"php": ">=5.5.0",
"silverstripe/cms": "3.5.1.2",
"silverstripe/framework": "dev-myproj as 4.0.x-dev",
"silverstripe-themes/simple": "~3.2.0"
}
2017-01-24 04:52:53 +01:00
}
```
2012-11-06 04:48:03 +01:00
2021-06-22 10:41:48 +02:00
What this means is that when the `myproj` branch is checked out into a project, this will satisfy any dependencies
that `4.0.x-dev` would meet. So, if another module has `"silverstripe/framework": "^4.0.0"` in its dependency list, it
won't get a conflict.
2012-11-06 04:48:03 +01:00
2021-06-22 10:41:48 +02:00
Both the version and the alias are specified as Composer versions, not branch names. For the relationship between
branch/tag names and Composer versions,
read [the relevant Composer documentation ](http://getcomposer.org/doc/02-libraries.md#specifying-the-version ).
2012-11-06 04:48:03 +01:00
2021-06-22 10:41:48 +02:00
This is not the only way to set things up in Composer. For more information on this topic, read
the ["Aliases" chapter of the Composer documentation ](http://getcomposer.org/doc/articles/aliases.md ).
2012-11-06 04:48:03 +01:00
2013-02-25 11:46:56 +01:00
## FAQ
2013-10-04 11:20:39 +02:00
### Error "The requested package silverstripe/framework 1.0.0 could not be found"
2017-01-24 04:52:53 +01:00
Composer needs hints about the base package version, either by using `composer create-project`
2021-06-22 10:41:48 +02:00
as described above, or by checking out the `silverstripe-installer` project directly from version control. In order to
use Composer on archive downloads from silverstripe.org, or other unversioned sources, an advanced workaround is to set
the `COMPOSER_ROOT_VERSION` before every command
2013-10-04 11:20:39 +02:00
([details](http://getcomposer.org/doc/03-cli.md#composer-root-version))
2013-02-25 11:46:56 +01:00
### How do I convert an existing module to using Composer?
2021-06-22 10:41:48 +02:00
Simply decide on a [unique name and vendor prefix ](https://packagist.org/about ), create a `composer.json` , and either
commit it or send a pull request to the module author. Look at existing modules like
the ["blog" module ](https://github.com/silverstripe/silverstripe-blog/blob/master/composer.json ) for good examples on
what this file should contain. It's important that the file contains a custom "type" to declare it as a
`silverstripe-module` or `silverstripe-theme` (
see [custom installers ](http://getcomposer.org/doc/articles/custom-installers.md )). Then register the module
on [packagist.org ](http://packagist.org ).
2013-02-25 11:46:56 +01:00
2013-02-25 11:58:12 +01:00
### How should I name my module?
2021-06-22 10:41:48 +02:00
Follow the packagist.org advice on choosing a [unique name and vendor prefix ](https://packagist.org/about ). Please don't
use the `silverstripe/<modulename>` vendor prefix, since that's reserved for modules produced by Silverstripe Ltd. In
2021-06-30 11:48:52 +02:00
order to declare that your module is in fact a Silverstripe CMS module, use the "silverstripe" tag in the composer.json
2021-06-22 10:41:48 +02:00
file, and set the "type" to "silverstripe-module".
2013-02-25 11:58:12 +01:00
2013-02-25 11:46:56 +01:00
### What about themes?
2021-06-22 10:41:48 +02:00
Themes are technically just "modules" which are placed in the `themes/` subdirectory. We denote a special type for them
in the `composer.json` (`"type": "silverstripe-theme"`), which triggers their installation into the correct path.
2013-02-25 11:46:56 +01:00
### How do I convert an existing project to Composer?
2021-06-22 10:41:48 +02:00
Copy the `composer.json` file from a newer release, and adjust the version settings in the "require" section to your
needs. Then refer to the [upgrading documentation ](/upgrading ). You'll also need to update your webserver configuration
from there (`.htaccess` or `web.config` files), in order to prevent web access to the composer-generated files.
2013-02-25 11:46:56 +01:00
### Do I need composer on my live server?
2021-06-22 10:41:48 +02:00
It depends on your deployment process. If you copy or rsync files to your live server, the process stays the same. If
the live server hosts a git repository checkout, which is updated to push a newer version, you'll also need to
run `composer install` afterwards. We recommend looking
into [Composer "lock" files ](http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file ) for this purpose.
2013-02-25 11:46:56 +01:00
### Can I keep using Downloads, Subversion Externals or Git Submodules?
2019-11-18 08:02:00 +01:00
Composer is more than just a file downloader. It comes with additional features such as
2017-01-24 04:52:53 +01:00
[autoloading ](http://getcomposer.org/doc/01-basic-usage.md#autoloading )
or [scripts ](http://getcomposer.org/doc/articles/scripts.md )
2021-06-22 10:41:48 +02:00
which some modules will start relying on. Please check the module README for specific installation instructions.
2013-02-25 11:46:56 +01:00
### I don't want to get development versions of everything!
2021-06-22 10:41:48 +02:00
You don't have to, Composer is designed to work on the constraints you set. You can declare
the ["minimum-stability" ](http://getcomposer.org/doc/04-schema.md#minimum-stability )
on your project as suitable, or even whitelist specific modules as tracking a development branch while keeping others to
their stable release. Read up
on [Composer "lock" files ](http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file ) on how this all
fits together.