Install Magento 2 module from GitHub or Bitbucket repository using Composer

In the previous article, we discussed How to install Magento 2 on MEMP (Mac OS X + Nginx + MySQL + PHP-FPM) stack using Composer

In this article, we will discuss How to install Magento 2 modules from any GitHub or Bitbucket repositories.

Let’s say we have Easy Template Path Hints (a Magento 2 module used for triggering the template path hints on the fly) on GitHub.

Install Module

In order to install the Easy Template Path Hints Module from Github, run the following commands:


composer config repositories.magesycho-magento2-easy-template-path-hints git [email protected]:MagePsycho/magento2-easy-template-path-hints.git
composer require magepsycho/magento2-easy-template-path-hints:dev-master

Breakdown

1. Registering the module git repository

composer config repositories.magesycho-magento2-easy-template-path-hints git [email protected]:MagePsycho/magento2-easy-template-path-hints.git

Composer config repository command syntax:

composer config repositories.<unique-repo-name> <vcs-type> <vcs-url-https-or-sshl>

Composer will register a new repository to composer.json (under “repositories” node). Updated composer.json looks like:


{  
  "repositories": {
    "magesycho-magento2-easy-template-path-hints": {
      "type": "git",
      "url": "[email protected]:MagePsycho/magento2-easy-template-path-hints.git"
    }
  }
}

2. Registering the module package itself

composer require magepsycho/magento2-easy-template-path-hints:dev-master

composer require command syntax:

composer require <vendor>/<package>:dev-<branch>

This will add a new dependent package under node “require” as:

{
  "name": "magento/magento2ce",
  "description": "Magento 2 (Community Edition)",
  "type": "project",
  "require": {
    "magepsycho/magento2-easy-template-path-hints": "dev-master"
  }
}

followed by Module installation from the repo.

Install Magento 2 Module from Github
Install Magento 2 Module from Github

Few Notes:
This approach will only work for the modules which have been packaged with composer.json
Please read more on Packaging a Magento 2 Module

If you are loading a package from VCS repository (git, svn, etc.), version should be a branch name prefixed with ‘dev-‘ else you may get an error like

The requested package magepsycho/magento2-easy-template-path-hints could not be found in any version, there may be a typo in the package name.

Source: https://getcomposer.org/doc/05-repositories.md#vcs

Enable Module

After the module has been downloaded from the git repository, Module needs to be enabled.
1. Disable the cache under System->Cache Management
2. Run following Commands

php bin/magento module:enable MagePsycho_Easypathhints --clear-static-content
php bin/magento setup:upgrade

Go to Admin menu > Stores > Configuration, you will be able to see the ‘Easy Template Path Hints’ settings:

Easy Template Path Hints - Backend Settings
Easy Template Path Hints – Backend Settings

Disable Module

In order to disable the Module, simply run:

php bin/magento module:disable --clear-static-content MagePsycho_Easypathhints

That’s all.
Isn’t that easier & powerful way of installing, enabling or disabling the Magento 2 Modules than Magento 1?