Question

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 ?

Was it helpful?

Solution

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

OTHER TIPS

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();

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.

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)

You can run below command to get the magento version:

php bin/magento --version

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.

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:

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

"version": "2.1.2",

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

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/

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top