Installing Magento 2 with Composer

Installing Magento 2 with Composer

With the Magento 2 Developer Beta out in the open, it’s time to revise our old guide and have a fresh, clear, step-to-step guide which helps with installing Magento 2 with Composer.

Introduction

In June, we hosted Tim which wrote a guide on installing Magento 2 with Composer on an nginx webserver. The Magento 2 project structure has changed a lot and that guide is not really applicable anymore.

There is official documentation. This is merely an alternative step-to-step guide if you just want to get to it without reading the documentation thorough. We plan to keep this guide updated with newer Magento 2 code drops.

This guide assumes you already have a functioning LAMP-stack with the minimum Magento 2 requirements (>= PHP 5.4.11 & >= MySQL 5.6.X).

1. Install Composer

You can skip this step if you have Composer already installed. Windows users have an installer available.

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

1.1 Make Composer Globally Available

If you wish, you can additionally install Composer globally so you don’t have to type php /path/to/composer.phar every time. The Windows-installer will automatically set up the PATH system variable.

mv composer.phar /usr/local/bin/composer

2. Download Magento 2

2.1 Via Git

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

This clones the repository and puts it under the magento2 directory. The thing is that you now still have to manually invoke Composer to fetch all the dependencies:

cd magento2 && composer install

2.2 Via Composer

composer create-project magento/community-edition magento2 -s dev --prefer-dist

Because the magento/community-edition package refers to the magento/magento2 repository on Github, using the --prefer-source option will make Composer figure out the rest for us.

2.3 Build from source

At the moment of writing, the Magento 2 project is divided into two Github repositories: magento2 and magento2-community-edition. The former hosts the complete Magento 2 code base and the latter has recently been completely wiped and only hosts the composer.json for “building from source”.

The magento2 repository is known on Packagist as magento/community-edition. magento2-community-edition is known on Packagist as magento/product-community-edition. They couldn’t have made it any more confusing!

If you prefer have all the Magento modules collected for you instead of having them prepared already, as is the case with the magento/community-edition package, you can create a new magento/product-community-edition project.

composer create-project magento/product-community-edition magento2 -s dev --prefer-source

3. Set Up Permissions

After all dependencies are retrieved, you should set the correct permissions on the entire Magento 2 installation directory. The official documentation recommends chmod’ing all directories to 700 and all files to a level of 600:

find . -type d -exec chmod 700 {} \; && find . -type f -exec chmod 600 {} \;

In case you are still facing problems, you are probably logged in under a different user and have to give ownership of the installation directory to the web server user.

4. Create The Database

Given that you already have a MySQL user with the correct permissions, create an empty database to work with. If you really want to, you can also use an existing database and pick a table-prefix during the installation process but… that’s not something a lot of people do.

echo "CREATE DATABASE magento2" | mysql -u[mysqluser] -p

5. Install Magento 2

That’s about it! You can now either follow the installation wizard or use the command line.

5.1 Installation Wizard

Just fire up the browser, navigate to the host where you installed Magento 2 and if everything went correct, you should be redirected to the setup tool.

5.2 Command Line Installer

Magento 2 ships with a command line installer and can be invoked as followed:

php setup/index.php
--db_host=localhost \
--db_name=magento2 \
--db_user=root \
--db_pass=canbempty \
--backend_frontname=admin \
--admin_username=username \
--admin_password=password \
--admin_email=admin@email.local \
--admin_firstname=First \
--admin_lastname=Last \
--base_url=http://magento2.local/ \
--base_url_secure=http://magento2.local/ \
--language=en_US \
--currency=USD \
--use_rewrites=true \
--use_secure=false \
--use_secure_admin=false \
--cleanup_database

The above command should be edited to your needs and executed from the Magento 2 root directory. What follows, is a bit of scrolling text and if everything went correct, you are informed that the installation has been completed.

6. Fill the database

You should now have a fresh Magento installation. If you want to fill it with some data, you can either make use of the sample data or use the performance toolkit.

6.1 Using Sample Data

Because the sample data is a separate development tool available via the Magento 2 packages repository, you first have to update the composer.json file so it is aware that the repository exists:

composer config repositories.magento composer http://packages.magento.com

Then, you should require the magento/sample-data and magento/sample-data-media package:

composer require magento/sample-data:0.42.0.beta1 magento/sample-data-media:0.42.0-beta1 --dev

Due to the size of the sample-data-media package, it will take a while before those dependencies are installed. After it’s done, execute the sample data installer:

php dev/tools/Magento/Tools/SampleData/install.php --admin_username=username

6.2 Using The Performance Toolkit

Another option is to use the generator of the performance toolkit to fill the database with static entries. The following profiles are available: small, medium, large and extra_large. Each profile is simply an XML file which defines how many items of each fixture should be created.

php dev/tools/performance-toolkit/generate.php --profile dev/tools/performance-toolkit/profiles/small.xml

Final words

Hopefully this is a clear guide on how to install Magento 2 with Composer. Most of this information is simplified and readily available with more depth in the official documentation. In case we missed anything, you have a trick to share or think something is wrong, your input is welcome.

Follow us on Twitter →

  • http://www.openstream.ch Nick Weisser

    Thanks for the tutorial, very helpful. Are you sure that it’s possible to set up sample data after running the installer? In Magento 1 it was not as far as I know.

  • http://www.crazybrainz.in/ Girish

    Nice :-) Thank you

  • http://www.haiku.co.nz Sergei Filippov

    Great writeup! Thank you.

    NB: Quick fix >

    php setup/index.php

    should read

    php setup/index.php install

  • project test

    Hi,
    I have installed magento 2 succesfully but it’s very vevery slowly. Do you know why ?
    Thanks

  • David Diomede

    Thanks for the tutorial, it was helpful. Easier to use https to clone the repository. Kept getting authorization errors any other way.

  • Afroz Alam

    It was a nice explanation. Can we have steps to install the stable version of Magento 2.0 as it has been already released ?

  • Ayushman Shrestha

    Hi, I was thing to configure Magento 2 in Ubuntu, with the permission setup as you mentioned “find . -type d -exec chmod 700 {} ; && find . -type f -exec chmod 600 {} ;” having trouble on theme development. Is it necessary to apply it during development, I believe we can go with that permission setup on Production environment? I tried to change the folder permission for applying for theme development lost access to my administrator panel then after. Please advise.

  • Sugan Hudda

    Hello,

    i use same method like magento1.9 . simple upload all magento code in htdocs folder magento2 and start installing and its completed and running…but i want to ask is it Right way? or its wrong..? i did not use composer.
    Kindly let me know. i am new in magento2.0

  • Sugan Hudda

    may i know, why we need to run composer? i think its completed compare to magento1

  • Sugan Hudda

    Hi Nick,
    Do you know about magento2, could we install without composer? i did install without composer. but not sure is it right or not

  • Sugan Hudda

    how to re-indexing magento 2 at localhost xampp? when i run command than showing error.
    you may not have proper read+execute permission tories under to var/generation magento2

  • Riddhi

    Hello,

    I have followed all the steps. But when i am running below command
    php setup/index.php install
    –db_host=localhost
    –db_name=mage2
    –db_user=root
    –db_pass=
    –backend_frontname=admin
    –admin_username=admin
    –admin_password=admin1020
    –admin_email=riddhi.shah@brainvire.com
    –admin_firstname= Developer
    –admin_lastname= Test
    –base_url=http://192.168.10.235/mage/
    –base_url_secure=http://192.168.10.235/mage/
    –language=en_US
    –currency=USD
    –use_rewrites=true
    –use_secure=false
    –use_secure_admin=false
    –cleanup_database

    It is not working for me and says that use “bin/magento” instead. I am installing magento2 for windows. Can anyone please tell me that how can i configure mydatabase for magento2 through command line.