Installing Magento 2 Using Composer

Install Magento

Standard Install

This will install Magento with no settings present – you will enter these from visiting your site’s URL.

composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition <directory>

Setup Install

This will install Magento pre-populated with settings, so that you won’t have to enter them in the setup process.

php -d memory_limit=4G  bin/magento setup:install \
--backend-frontname=admin --cleanup-database \
--db-host=localhost --db-name=DATABASE_NAME --db-user=DB_PASSWORD --admin-password=PASSWORD \
--admin-user=ADMIN_USERNAME --admin-firstname=ADMIN_FIRSTNAME --admin-lastname=ADMIN_LASTNAME --admin-email=ADMIN@EMAILADDRESS \
--currency=GBP --use-rewrites=1

bin/magento cron:run
bin/magento cron:run
bin/magento setup:static-content:deploy
bin/magento deploy:mode:set developer
rm -rf var/cache

If you are prompted to enter a username and password, you can retrieve or create these by logging into magentocommerce.com;

  • Click the ‘Connect’ Tab
  • Click ‘Secure Keys’ from the left navigation
  • If you don’t have a secure key listed, enter a name and click ‘Generate new’.
  • The username you need to enter is the Public Key, and the password is the Private Key.

Change the Directory Permissions

find var vendor pub/static pub/media app/etc -type f -exec chmod g+w {} \;
find var vendor pub/static pub/media app/etc -type d -exec chmod g+w {} \;
chmod u+x bin/magento

Complete Installation 

Visiting your website’s URL should walk you through the rest of the installation process. This step will be omitted if the ‘Setup Install’ section above was followed

Installing Sample Data 

The following will install Magento’s sample data with an increased memory limit, as it will tend to fail without it.

php -d memory_limit=2G bin/magento sampledata:deploy

Run Upgrade

bin/magento setup:upgrade

Registering a Module

Location

For the purpose of this example, we’ll make our module in app/code/.

Create a namespace directory – E.g. ‘llapgoch’. Within that should be another directory which is typically the name of the module; for this example we’ll use ‘developertoolbar’. The structure of this folder is quite similar to Magento 1’s module structure.

Within our developertoolbar directory, we need to create a registration.php file which Magento will look for in order to register our module. This would be a rough equivalent of the old module registration stub files back in Magento 1, which in this case would be Llapgoch_Developertoolbar.xml.

Add the following to the registration.php file to tell Magento about our new module:

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Llapgoch_Developertoolbar',
    __DIR__
);

The Register Method’s Parameters

  1. Type– Either MODULE , LIBRARY, LANGUAGE, or THEME which are constants within the ComponentRegistrar class.
  2. Name
  3. Location – Typically, we’ll use PHP’s magic constant __DIR__ to tell Magento that our module exists in the same place as our registration.php file

Check The Module’s Status

To check Magento can actually see the module, navigate to the bin folder in terminal and run:

./magento module:status

Your module should be listed at the bottom, in “List of disabled modules”.

Enable the Module

Run the following from terminal which will enable your module:

./magento module:enable Llapgoch_Developertoolbar

And that’s it! We’ve got a module registered with Magento. Now there’s the task of actually making it do something…

Magento 2.0: Fixing a failed install

Seemingly due to some issues with my MySQL Server, Magento 2’s installer failed before it could complete. It left me with the following error in my browser:

Unable to proceed: the maintenance mode is enabled. 

Aha! I can turn maintenance mode off – that’s easy! Just watch as this error falls like that gummy bear tower you tried to build in junior school! Let’s hop over to Magento’s bin folder…

./magento maintenance:disable

Woohoo, and all is… erm.. yup. Still broken:

SQLSTATE[42S02]: Base table or view not found: 1146 Table &#039;magentorc.store_website&#039; doesn&#039;t exist, query was: SELECT `main_table`.* FROM `store_website` AS `main_table` WHERE (`is_default` = &#039;1&#039;) AND (main_table.website_id &gt; 0) ORDER BY main_table.sort_order ASC, main_table.name ASC

Hmm, a quick look at the magentorc table I'd previously created shows it's completely empty – looks like the MySQL issue was the dastardly culprit! After trying to get it to perform the setup again using a variety of cli commands (and a lot of swearing), I instead opted to remove the following file:

app/etc/env.php

This is where Magento keeps its database details. Removing this started the install process once more – voilà!