Young men at the start of the sprint

How to install Magento 2 with composer

Having to install Magento 2 you can decide between different options:

- to install Magento 2 by downloading the source code .zip

- to clone the Magento 2 GitHub repository

- or to install Magento 2 using Composer.

What is Composer?

The composer is a dependency manager written in PHP. It allows you to keep track of the packages your project relies on. Just to give an idea, it is highly inspired by Node's NPM.
Using Composer to install Magento 2 is the best option in terms of maintainability of the software dependencies and their updates.

System requirements for Magento 2

We need to satisfy all the system requirements Magento 2 has.

The latest version of Magento 2 requires a web server running PHP 5.6 (or greater) and MySQL 5.6 (or greater) to complete successful installation and run properly.

Obtaining Access Keys from Magento.com website

In addition, if we want to install Magento 2 via Composer we need a ssh access to the web server and a developer account on the Magento.com website.
This account is required to obtain the access keys to download Magento 2, extensions and themes from Magento Marketplace repository.

While installation of the web server will not be part of this article I'm going to assume Magento 2 system requirements are satisfied.

First of all, let's obtain the Access Keys to authenticate into the Magento 2 source code repository.

Log into your account on the Magento.com website and click on the Marketplaces link. Then click on My Access Keys and Create A New Access Key.
This will generate a pair of public and private keys. It will allow you to access the Magento 2 code via Composer.

Magento.com Access keys creation in the Marketplace area

Composer installation

Furthermore, we need to install Composer.
If you don't have it yet, you can install it by following an official method. It is described on the Composer website:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('SHA384', 'composer-setup.php') === 'e115a8dc7871f15d853148a7fbac7da27d6c0030b848d9b3dc09e2a0388afed865e6a3d6b3c0fad45c48e2b5fc1196ae') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

or use a shortcut:

curl -sS https://getcomposer.org/installer | php

In both cases, you will get a composer.phar file in the current folder. I suggest to rename it as "composer" and move it to /usr/local/bin folder for convenience.
Don't forget to set the right execution permissions to Composer script file.

sudo mv composer.phar /usr/local/bin/composer
sudo chmod +x /usr/local/bin/composer

Magento 2 installation via Composer

Once we have Composer installed, let's move to a folder we want Magento 2 to be installed in and initiate the Composer process to fetch source code.

In our case, Apache web server is configured to use  /var/www/html/magento2/public_html as a web folder for Magento 2 and runs with the  apache user.
This is a very important detail. If you attempt to create files or folders in a directory where the current user has not the right privileges it will raise Permission denied error.
Given what stated above, the following commands need to be run by an owner of a destination folder.
In our case it is  apache user.

cd /var/www/html/magento2/public_html/
sudo -u apache /usr/local/bin/composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition .

When Composer connects to Magento repository for the first time you will be prompted for a username and a password.
Now you need to use the Public and Private Keys we obtained at the beginning of this tutorial.

If you configured everything correctly you should see Composer starting to download Magento 2 and its dependencies. An output should be similar to the following:

Installing magento/project-community-edition (2.1.0)
  - Installing magento/project-community-edition (2.1.0)
    Downloading: 100%         

Created project in .
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Installing magento/magento-composer-installer (0.1.11)
    Downloading: 100%         

  - Installing symfony/process (v2.8.8)
    Downloading: 100%         

  - Installing magento/framework (100.1.0)
    Downloading: 100%         

[...]

Once the download is finished, you can finally point your browser to the URL you configured in Apache. You can complete the database installation by following a wizard.

Magento 2 setup screen

 

Welcome to Magento 2!

 

FacebookTwitterPinterest

Enzo Perrotta

Senior Full Stack Developer

Magento enthusiast, technology addicted and wasabi lover. I embrace the whole eCommerce ecosystem, I’m focused on both, front-end and back-end development.