how to include library in magento 2 using helper as below is the class for magento 1 that is Mage::getBaseDir ,


  namespace vendor\Module\Model;

   require_once(Mage::getBaseDir('lib') '/vendor/libname/class.php');
  • please check my answer. Nov 12, 2018 at 11:00

2 Answers 2


Using Factory Method :

protected $_dir;

public function __construct(
    \Magento\Framework\Filesystem\DirectoryList $dir,
) {
    $this->_dir = $dir;

Now, get path like this below way :

$this->_dir->getPath('lib'); // Output: /var/www/html/yourmagento/lib

You need to use this below line :

  • thanks for your answer dear @Rohan Hapani, i know this method already but my question was to solve it using require_once Nov 12, 2018 at 11:04
  • 1
    For good practise and as per magento standard way. You should use this answer. Nov 12, 2018 at 11:08
  • yes you are right but i am converting task of another developer from m1 to m2, so i have to do in that way Nov 12, 2018 at 11:09

The answer is also available in magento 2 Vendor files and it worked for me


    namespace Vendor\Module\Model;
    require_once __DIR__ . '/../../../../../lib/vendor/LibName/LibClass.php';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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