23

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 ?

11 Answers 11

20

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 '18 at 8:34
  • @7ochem thanks for the information, updated the answer as well :) – Atish Goswami May 31 '18 at 19:21
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
  • 1
    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
10

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)

10

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 '18 at 13:01
6

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.

6

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 '18 at 13:04
4

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:
1

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 '18 at 8:25
1

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) ?>
1

Here are some ways to check Magento version

Method #1: Use PHP Code checking

Check Magento 1 version
Mage::getVersion() //will return the magento version
Check Magento 2 version

You can use this in 2.0.x versions:

echo \Magento\Framework\AppInterface::VERSION;

For version 2.1:

The first way, using DI:

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

The second way, using ObjectManager directly:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
$productMetadata = $objectManager->get('Magento\Framework\App\ProductMetadataInterface'); 
echo $productMetadata->getVersion();

Method #2: Check Magento version via command line

You can run the below command to get the Magento 2 version:

php bin/magento --version

Actually, since Magento 2 has utilized its composer so that you do not need to write any PHP code for this. Instead, there are two options to find Magento 2 version:

Firstly, please try out the composer.json and look for version keyword. Should you prefer using terminal, you can add something on the root of project.

composer licenses | grep Version:

Another way to check Magento 2 version weather it is community or enterprise edition, please write the following

composer licenses | grep Name:

Use web service to check https://www.mageplaza.com/check-magento-version/

-1

Pls try this tool online, it works for both versions of magento1.x and magento2.x https://www.talktojobs.com/check-magento-version/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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