I have a third party php library which I would like to use in a module. How do I do that in Drupal 8? In drupal 7, I have done things like the following code, but libraries_get_path() doesn't exist in Drupal 8, and I'm not exactly sure how things change, not that we use namespaces (of which I am a beginner).

require(libraries_get_path('twitter-api-php') . '/TwitterAPIExchange.php');

And, where should I place my library? Should I create a new libraries directory and put it in the root, just like modules and themes are now in the root?

  • 1
    libraries_get_path also doesn't exist in Drupal 7 - it comes from the contrib Libraries module </nitpick>
    – Clive
    Aug 19 '14 at 17:02

The 8.x-3.x branch of the Libraries module still provides the libraries_get_path() function. And (as of now) it looks pretty much unchanged. So you should be able to use it in the same way.

As far as where to put your library, the libraries_get_libraries() function has been updated with the following addition to the $searchdir array:

// Always search the root 'libraries' directory.
$searchdir[] = 'libraries';

Which indicates that you can place your libraries in a directory named "libraries" at your site root.

  • Ok, I was able to use libraries_get_path and it returned the correct path. But when I tried to require it, I got this error: Fatal error: Class 'Drupal\twitter_pull\Plugin\Block\TwitterAPIExchange'. My code is: require(libraries_get_path('twitter-api-php') . '/' . 'TwitterAPIExchange.php'); Whats wrong? Sep 5 '14 at 13:17
  • @user1015214 Did you get solution for your error? As i am getting similar error on using require.
    – neha
    Feb 13 '17 at 12:19


Composer is a tool for dependency management in PHP. It allows you to declare the dependent libraries your project needs and it will install them in your project for you.

plus the Composer Manager module:

Composer Manager provides a gateway to the larger PHP community by enabling Drupal modules to more easily use best-in-breed libraries that are managed by Composer.

can handle this.


If you use the libraries module and you put your library files in /libraries/library (which is where mine end up using drush make) you can simply call library_load('library') and that will do the require_once for you.

  • Library_load() function does not exists in drupal 8 even after installing libraries module. Error is : PHP Fatal error: Call to undefined function .
    – neha
    Feb 13 '17 at 9:12

As clive said, libraries_get_path is not a function that is shipped with Drupal core. Just for your information as of the change log #2169605, any libraries that you need to add to a page is by following,

$example = array(
  '#attached' => array(
    'library' => array(

Also have a look at the Replace hook_library_info() by *.libraries.yml file link.

  • I actually saw that change log, but it seemed that it was only talking about adding a library to a form. I am not creating a form, just want to import the library Aug 19 '14 at 18:53
  • This is not for PHP libraries; it is for JavaScript and CSS.
    – apaderno
    May 3 '16 at 21:53
  • Yes, for external php libraries (containing composer.json) one can use the Composer Manager module. May 8 '16 at 5:43

Using Composer Manager is good solution but it need access to command line to init Also you can use module_load_include like we use it in Drupal7. Put your Php file in you module directory and use

  module_load_include('php', 'modulename','filename');

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.