In this article by Chaz Chumley and William Hurley, the author of the book Mastering Drupal 8, we will to decide on a local AMP stack and the role of a Composer.
(For more resources related to this topic, see here.)
Any developer workflow begins with having an AMP (Apache, MySQL, PHP) stack installed and configured on a Windows, OSX, or *nix based machine. Depending on the operating system, there are a lot of different methods that one can take to setup an ideal environment. However, when it comes down to choices there are really only three:
In the end, my recommendation is to choose an environment that is flexible enough to quickly install, setup, and configure Drupal instances. The above choices are all good to start with, and by no means is any single solution a bad choice.
If you are a single person developer, then a packaged AMP stack such as MAMP may be the perfect choice. However, if you are in a team environment I would strongly recommend one of the VM options above or look into creating your own VM environment that can be distributed to your team.
We will discuss virtualized environments in more detail, but before we do, we need to have a basic understanding of how to work with three very important command line interfaces. Composer, Drush, and Drupal Console.
Drupal 8 and each minor version introduces new features and functionality. Everything from moving the most commonly used 3rd party modules into its core to the introduction of an object oriented PHP framework. These improvements also introduced the Symfony framework which brings along the ability to use a dependency management tool called Composer.
Composer (https://getcomposer.org/) is a dependency manager for PHP that allows us to perform a multitude of tasks. Everything from creating a Drupal project to declaring libraries and even installing contributed modules just to name a few. The advantage to using Composer is that it allows us to quickly install and update dependencies by simply running a few commands. These configurations are then stored within a composer.json file that can be shared with other developers to quickly setup identical Drupal instances.
If you are new to Composer then let’s take a moment to discuss how to go about installing Composer for the first time within a local environment.
Composer can be installed on Windows, Linux, Unix, and OSX. For this example, we will be following the install found at https://getcomposer.org/download/. Make sure to take a look at the Getting Started documentation that corresponds with your operating system.
Begin by opening a new terminal window. By default, our terminal window should place us in the user directory. We can then continue by executing the following four commands:
Download Composer installer to local directory:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
Verify the installer
php -r "if (hash_file('SHA384', 'composer-setup.php') === 'e115a8dc7871f15d853148a7fbac7da27d6c0030b848d9b3dc09e2a0388afed865e6a3d6b3c0fad45c48e2b5fc1196ae') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
Since Composer versions are often updated it is important to refer back to these directions to ensure the hash file above is the most current one.
Run the installer:
php composer-setup.php
Remove the installer:
php -r "unlink('composer-setup.php');"
Composer is now installed locally and we can verify this by executing the following command within a terminal window:
php composer.phar
Composer should now present us with a list of available commands:
The challenge with having Composer installed locally is that it restricts us from using it outside the current user directory. In most cases, we will be creating projects outside of our user directory, so having the ability to globally use Composer quickly becomes a necessity.
Moving the composer.phar file from its current location to a global directory can be achieved by executing the following command within a terminal window:
mv composer.phar /usr/local/bin/composer
We can now execute Composer commands globally by typing composer in the terminal window.
One of the most common uses for Composer is the ability to create a PHP project. The create-project command takes several arguments including the type of PHP project we want to build, the location of where we want to install the project, and optionally, the package version. Using this command, we no longer need to manually download Drupal and extract the contents into an install directory. We can speed up the entire process by using one simple command.
Begin by opening a terminal window and navigating to a folder where we want to install Drupal. Next we can use Composer to execute the following command:
composer create-project drupal/drupal mastering
The create-project command tells Composer that we want to create a new Drupal project within a folder called mastering. Once the command is executed, Composer locates the current version of Drupal and installs the project along with any additional dependencies that it needs
During the install process Composer will prompt us to remove any existing version history that the Drupal repository stores. It is generally a good idea to choose yes to remove these files as we will be wanting to manage our own repository with our own version history.
Once Composer has completed creating our Drupal project, we can navigate to the mastering folder and review the composer.json file the Composer creates to store project specific configurations.
As soon as the composer.json file is created our Drupal project can be referred to as a package. We can version the file, distribute it to a team, and they can run composer install to generate an identical Drupal 8 code base.
With Composer installed globally we can take a look at another command line tool that will assist us with making Drupal development much easier.
In this article, you learned about how to decide on a local AMP stack and how to install a composer both locally and globally. Also we saw a bit about how to use Composer to create a Drupal project.
Further resources on this subject: