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:

or use a shortcut:

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.

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.

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:

 

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!

 

Enzo Perrotta

Senior Magento 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.