Beginner’s Guide to Installing Magento 2 using Composer

If you’re new to Composer or package managers like Node’s NPM then you’ve got a lot to take in before you start working with Magento 2. You see, Magento 2 is dependent on Composer and all of its package/dependency goodness.

It’s true that Magento and several other developers have written articles detailing what I’m about to go through, but I decided to write this for the absolute beginner with deeper explanation of what is going on throughout the steps – particularly the role of Composer.

Step 1: Server Environment

Magento 2 has new minimum requirements of your LAMP stack. I’m not going to list them here in case they change so please check the Magento 2 documentation for those prerequisites. A Vargrant setup is preferable but MAMP Pro can also work.

Step 2: Composer

If you’re unaware of what composer is then try this analogy. Composer is exactly like…. a composer. In this case Magento 2 is a musical and the packages installed are the members of the orchestra. Once installed in your project, Composer can be used to manage packages based on its dependency system. You can’t have a live musical without the orchestra and you can’t have Magento 2 without its packages. In both cases the composer is responsible for assembling components to allow the rest to work.

Fire up your command line tool and navigate to an empty folder where you’d like to install your copy of Magento 2. We need to ensure Composer is installed globally on your system so that you don’t have to repeat this step again.

Type the following command to see if composer is installed:

composer -help

If it’s not installed you’ll see the response command not found, which means you need to install Composer. Luckily it’s as simple as running the following two commands:

curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer

The first command installs Composer into your local directory, the second will move the composer.phar file to a directory where it can be accessed globally by your system.

If you run the original command again you should see that Composer is installed and ready:

Install Composer

Step 3: Clone Magento 2

To clone Magento CE 2 into your current directory run the Git following command:

git clone https://github.com/magento/magento2.git

It will clone the repo into a magento2 subdirectory, and this should take about 2-3 minutes depending on your broadband speed. Once it’s done run the following command to move into the Magento 2 root directory and checkout the master branch.

cd magento2
git checkout master

In order to work with Composer a project must have a composer.json file within its root directory. If you take a look at the file you’ll see a huge list of Magento 2 package dependencies. I’ve picked out first three below:

  • require: a list of packages (and versions) required by Magento 2. Magento 2 will not install if  any of those package dependencies are not met.
  • require-dev: a list of packages required by Magento 2 for development (includes running units tests, hence why PHPUnit is in there)
  • replace: shows a list of packages that are replaced by Magento 2, which is mostly made up of the main modules that make up the Magento 2 framework – this allows developers to fork specific packages with improved or customised code

To run Composer for the first time and install your packages simply run:

composer install

When upgrades become available you can install future updates by using:

composer update

The beauty is that upgrades will only run if Composer identifies a package’s dependencies can be met meaning the stack should always run smoothly.

Vagrant users should not find this a problem, however you may encounter missing PHP extensions when you install Composer, such as:

  • The requested PHP extension ext-mcrypt * is missing from your system.
  • The requested PHP extension ext-intl * is missing from your system.

For MAMP users, you can get around this by creating a symlink of your system’s PHP version to MAMPs. Firstly find out your PHP location:

which php // returns for example: /usr/bin/php

And then create the symlink:

sudo mv /usr/bin/php /usr/bin/php.bak
sudo ln -s /Applications/MAMP/bin/php/php5.6.10/bin/php /usr/bin/php

When Composer has finished its install you’ll be ready to install Magento. You can do this via command line, with Composer or through the installer interface.

Step 4: Install Magento 2

The installer interface is better looking than before and it checks your server ‘readiness’ before allowing you to proceed with the install.

Magento Install

Follow the process and once fully installed you’re ready to go.