Magento 2 Tutorials

Installing Magento 2 with Composer

Today we are hosting Tim Bezhashvyly. Not for an interview, but as guest author. He is sharing his personal experience on setting up Magento 2 with Composer and nginx. Join him, as he walks us through the process.

Introduction

Installing Magento with composer was my dream for literally years. I first got this idea when we built a Magento Composer Installer at the Magento Hackathon in Munich back in October 2012. However, for a number of reasons it is not possible to install Magento 1.X with composer completely.

Good news arrived when the Magento 2.X core team added the composer.json file to the GitHub repository. It goes without notice that I was intrigued to try and install Magento 2 using composer. Here, I will describe the process of setting up a new project with Magento 2.X as a composer module.

Let’s get started

Assuming you already have composer installed on your system, let’s initiate a blank project with the composer -n init command. This will give us a composer.json bootstrap file to work with. Secretly, Magento 2 is already available via packagist, but because it is still in its early stages, for now it’s a better idea to directly attach it to the Github repository:

This is all we need. Now let’s install Magento with the composer install --no-dev command. This will download Magento 2 to the vendor directory. Now what?

Magento 2 has a pub directory which contains files supposed to be exposed to the web. You can just copy them all to your web root but there is room for experimentation. I always try to add as little number of files to my project as necessary, so my initial idea was to symlink all of them except those which have to be modified.

In the project root, which only contains the vendor directory, composer.json and composer.lock files, the public directory is added and configured as a web root. At this point, it’s a matter of analyzing which files of Magento’s pub directory have to be copied and which can be symlinked. Here is the file listing:

Copying the media directory is obvious. At this stage, I decided to symlink the opt and errors directories as I was not yet planning to do any modifications there. At the moment of writing, the pub/index.php file was broken so I had to copy it to my public directory and fix the issues. The remaining .php files, which are cron.php, get.php and static.php can be safely symlinked.

Because index.php has been moved, the bootstrap path inside it has to be modified from

to

The only remaining directory in pub is called static. This directory only contains a .htaccess file which redirects all static files requests to static.php. Theoretically, this directory can be symlinked too, but as long as my server runs nginx, the .htaccess file has no purpose. Ignoring this directory, the following rewrite-rule was added to the nginx configuration file of my host:

The complete configuration file looks like this:

Note that there is also a .htaccess file in the pub directory. The rule-sets in it are optional and transferring them to nginx configuration is outside the scope of this tutorial. Basically everything will work without them.

Finally, the contents of the .gitignore file are compiled which is of course a matter of preferences; there are some holy wars whether to include the vendor directory and/or composer.lock into the repository. Here is the list of files I choose to ignore:

Final words

And that’s all we need. Of course, this article is far from being called a guide, more like field notes. Hopefully it will be helpful for anyone getting started with using Magento 2.X with composer.

Editor’s note: Alan Kent, technical staff at Magento, notes that Magento 2 Composer integration is still under active development and might change in the future.

UPDATE

  • pub/index.php was fixed in 2.0.0.0-dev82 commit so now it can be also safely symlinked unless you want to modify something in the entry point.
  • Magento 2 is not on packagist.org so the entire "repositories" node of composer.json can be dropped.

  • sylvainraye

    You could also add additional scripts into your composer to automate all the tasks you need to do after composer download and install Magento 2. For example you could add the followings:nnn{n "scripts": {n "post-install-cmd": [n "DiglinMagento2Composer::postInstall"n ]n }n}nnnIn your PSR-4 PHP class file /Diglin/Magento2/Composer.phpnnnnamespace DiglinMagento2;nuse ComposerScriptEvent;nnclass Composern{n public static function postInstall(Event )n { n // Do your stuffn }n}n

  • Dusan Lukic

    I am wondering if other .htaccess files in the project need to be taken into consideration when running on nginx.

    • tim_bezhashvyly

      As I have written the .htaccess from pub/static directory has to be transformed into nginx rules. The only other one from pub directory is located in its root and I already mentioned that the directives inside it are more or less tweaks.nnnThe rest of .htaccess files outside of pub dir to my understanding make no sense as they lie outside of web-server scope. And this is one of main advantages of using composer.

      • Dusan Lukic

        Yes, I think you are right. So configuring Magento 2 on nginx requires only one .htacess to be “transformed”. Thanks.

  • Pingback: Magento-Neuigkeiten der Wochen 25/26 2014()

  • Pingback: Magento 2 Product and Composer Version Numbering | Alan Kent's Blog()

  • Mike

    Could you do an update of this post ? The composer install seems to be broken. I get an error when running composer install : “The requested package magento/community-edition could not be found in any version, there may be a typo in the package name.”nTried to change package to project-community-edition and got “- Installation request for magento/project-community-edition *@dev -> satisfiable by magento/project-community-edition[dev-master].nnnnnnnnnn – magento/project-community-edition dev-master requires zendframework/zend-stdlib 2.0.3 -> no matching package found.”nnnThanks in advance !

    • We are aware of the situation and will be working on it!

      • Mike

        Thanks ! Looking forward for the update. Being the top hit for “magento 2 composer install”, I think many others are too :)

  • Pingback: How to Install Magento 2 using Composer | Tutor...()