Using composer to manage dependencies for Drupal is great. However, two pain points (at least for me have been that...
- the directory structure for a Drupal install has changed several times in D8 rendering Composer updates problematic, and
- using Composer on the server often posses memory issues which require you to use some kind of local development strategy.
I know, I should have a dev>test>live setup, but for smaller sites without a lot of custom code that can be overkill. I found all kinds of documentation (cough) that was occasionally helpful but often misleading. Let me add to the stew. The way I found to safely deal with the memory issues for on-server development is to employ git in the process. Here are the steps that work for me.
-
Set up a central git repo. I'm using my home directory on the server, but you could put this somewhere like GitHub.
$ git init --bare fooproject.git
Install Drupal using Composer in your local machine. This will not only get Drupal, but all the dependencies you'll need to run it, by running composer install automatically. This directory will be put under version control in the next step. I'm specifying a version number here, but you can remove that with the colon to get the latest.
composer create-project drupal/recommended-project:8.9.15 fooproject_dir
- CD into that directory, and put it under version control.
$ git init
- Add the remote for your central git repo. Remember I did this on my production server.
$ git remote add origin path/to/your/central/git/repo (example: ssh://fooproject@mydomain.com/home/fooproject.git)
Add, commit, and push...all the usual git stuff
$ git add . $ git commit -m "Initial drupal install" $ git push origin master
- Now, back on your production server clone the repo to the root directory for your website. This will take so little resources cause it's right there. No memory issues, etc. You'll be prompted for your password unless you've set up keys.
$ git clone fooproject.git httpdocs
You'll now can install Drupal on the server by going to https://mydomain.com, and doing the usual install stuff. Adding modules, or updating core from here on out is as simple and really fast.
- To add a module, on you local machine go the root of your repo and use composer to install a module. One of the first I install is Admin Toolbar. I won't go into how to specify versions in composer here, but here's an example
$ composer require drupal/admin_toolbar:^2.0
- To see what needs updating...
$ composer outdated drupal/*
- And to actually update...
composer update drupal/*
- Add, commit, and push...
$ git push origin master
- ...and on the server...
$git pull
Run update.php and you're good to go. I hope this helps.