Magento 2 - Programmatically Check Magento Version (CE / EE)

You have to inject \Magento\Framework\App\ProductMetadataInterface into your constructor.

protected $productMetadata;

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

Then you can get current Magento version by (assuming that ProductMetadataInterface object is assigned to $productMetadata field):

$version = $this->productMetadata->getVersion();

And edition (Community/Enterprise) by:

$edition = $this->productMetadata->getEdition();


Method 1:

Use Magento standard way to get version of your site, Using Block - Template way is proper way to do call any function in magento 2,

Inside Block file,

<?php
namespace Vendor\Modulename\Block
class Version extends \Magento\Framework\View\Element\Template{

protected $_productMetadata;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\App\ProductMetadataInterface $productMetadata,
        array $data = []
    ) {
        parent::__construct($context,$data);
        $this->_productMetadata = $productMetadata;
    }

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

inside template file,

echo $block->getVersion();

Using Direct Objectmanager is not proper way to use in magento 2,

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