How do I get the Magento version in Magento2.x (e.g. 2.0.0) ?

In Magento1.x, I could do it with

Mage::getVersion()

So what's the equivalent in Magento2.x ?

up vote 13 down vote accepted

Up until Magento version 2.0.7 the version number was maintained in the AppInterface::VERSION constant.

With the release of Magento 2.1 the constant was removed.

So till 2.0.7 if you check the adminhtml footer file where the version is shown

Admin Panel Footer

It had reference to the \Magento\Framework\AppInterface::VERSION constant.

But since Magento 2.1 release the footer file now uses the \Magento\Backend\Block\Page\Footer::getMagentoVersion() which in turn calls the \Magento\Framework\App\ProductMetadata::getVersion().

Previously the ProductMetadata::getVersion() used to return the value of the constant \Magento\Framework\AppInterface::VERSION, but now it parses the composer.json as well as composer.lock and returns the appropriate magento version

So no matter which version you are on either 2.0.x or 2.1.x, if you use the \Magento\Framework\App\ProductMetadata::getVersion() method, you will always get the proper Magento version.

Conclusion:

Magento 1:

Mage::getVersion() //will return the magento version

Magento 2:

//Updated to use object manager
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productMetadata = $objectManager->get('Magento\Framework\App\ProductMetadataInterface');
$version = $productMetadata->getVersion(); //will return the magento version
  • composer.json might contain a version constraint like "^2.1" which doesn't indicate the exact version, so Magento is (also) checking the composer.lock file to see what exact version is currently installed. – 7ochem May 29 at 8:34
  • @7ochem thanks for the information, updated the answer as well :) – Atish Goswami May 31 at 19:21

You can use this in 2.0.x versions:

echo \Magento\Framework\AppInterface::VERSION;

For version 2.1:

Way 1, using DI:

public function __construct(
        \Magento\Framework\App\ProductMetadataInterface $productMetadata
) {
    $this->productMetadata = $productMetadata;
}

public function getMagentoVersion()
{
    return $this->productMetadata->getVersion();
}

Way 2, using ObjectManager directly:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productMetadata = $objectManager->get('Magento\Framework\App\ProductMetadataInterface');
echo $productMetadata->getVersion();
  • Such implementation you can find at app/code/Magento/Backend/view/adminhtml/templates/page/footer.phtml – Valery Statichnyi Jan 12 '16 at 12:05
  • NOTE: this does NO LONGER work for Magento 2.1+ (see other answers!) – Manuel M Jun 29 '16 at 5:55
  • other answer actually work? In my case through error report. See my new answer. – Sohel Rana Jun 29 '16 at 7:34

Mentioned solutions are not applicable starting from Magento 2.1 (\Magento\Framework\AppInterface::VERSION constant was removed)

The new way to get version is to retrieve it from Product Metadata Instance (reads version from composer.json):

$productMetadata = new \Magento\Framework\App\ProductMetadata();
$version = $productMetadata->getVersion();

(better to inject Product Metadata to constructor as \Magento\Framework\App\ProductMetadataInterface)

On top of the other answers, you can get the major version (for example 2.1) by accessing /magento_version on your Magento 2 website.

  • Thanks for pointing this out. Seems like a security leak to me: this is a great way for hackers to find vulnerable Magento 2 sites! Of course, you only seem to be able to get the major and minor version this way, but it still seems like a bad idea. – toon81 Dec 4 '17 at 11:44
  • great "quick & dirty" trick, thanks! – maoizm Sep 11 at 13:01

In case someone has to find it manually. The base Magento module version is located in this composer file:

vendor/magento/magento2-base/composer.json

Also as suggested above, to make below code work:

$productMetadata = new \Magento\Framework\App\ProductMetadata();
$version = $productMetadata->getVersion();

\Magento\Framework\App\ProductMetadata now requires instance of ComposerJsonFinder to be passed at the time of instantiation.

An example I found in dev/tests/integration/testsuite/Magento/Framework/Composer/ComposerInformationTest.php:

$directories = [
    DirectoryList::CONFIG => [DirectoryList::PATH => __DIR__ . '/_files/'],
    DirectoryList::ROOT => [DirectoryList::PATH => __DIR__ . '/_files/' . $composerDir],
    DirectoryList::COMPOSER_HOME => [DirectoryList::PATH => __DIR__ . '/_files/' . $composerDir],
];

$this->directoryList = $this->objectManager->create(
    'Magento\Framework\App\Filesystem\DirectoryList',
    ['root' => __DIR__ . '/_files/' . $composerDir, 'config' => $directories]
);

$this->composerJsonFinder = new ComposerJsonFinder($this->directoryList);

Above code is purely for informational purposes. You have to dig in more to make it work.

You can run below command to get the magento version:

php bin/magento --version
  • it seems that this doesn't differentiate Community vs. Enterprise editions of Magento – maoizm Sep 11 at 13:04

For unix like users

No need to write any PHP code for this. Because Magento 2 utilises composer it makes it all easier. You can do this two ways:

Check the composer.json and look for a key called version. If you're like me who likes to use the terminal you can do something like on the root of the project.

composer licenses | grep Version:

This returns the version of the M2 if you want to check wether its a community or a enterprise version then just do the following:

composer licenses | grep Name:

Try this:

<?php echo __('Magento'); ?>
<?php  echo __('ver. %1', \Magento\Framework\AppInterface::VERSION) ?>

For version 2.1:

<?php
$productMetadata = new \Magento\Framework\App\ProductMetadata();
$version = $productMetadata->getVersion();
?>
<?php  echo __('ver. %1', $version) ?>

simply check composer.json file on magento2 root, you'll find text like this

"version": "2.1.2",
  • composer.json might contain a version constraint like "^2.1" which doesn't indicate the exact version. You should check the composer.lock file to see what exact version is currently installed. – 7ochem May 29 at 8:25

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.