3

In Drupal 8, Admin-Report > Status report > Twig C extension: Not available Enabling the Twig C extension can greatly increase rendering performance. See the installation instructions for more detail.

I installed Twig C Extension, but it's reported to not be enabled.

How do I install and enable the Twig extension for PHP, in Drupal 8?

1
  • From Drupal 8.2 onwards, this warning message seems to have gone away. As per this thread github.com/twigphp/Twig/issues/1695 discovered below by @Clive, PHP7 performance is excellent even without the C extension. The C extension has not been ported to PHP7 and it's not clear it will ever be.
    – AdamS
    Commented Aug 24, 2016 at 9:04

4 Answers 4

2

This should just work. Make sure to follow http://twig.sensiolabs.org/doc/installation.html#installing-the-c-extension and run the commands from within /path/to/docroot/vendor/twig/twig. Next, you should check again that extension=twig.so exists in your php.ini file and that Apache has been restarted/reloaded.

To confirm that the extension is being loaded, you can run php -c /path/to/php.ini -i | grep twig

4
  • This answer is deprecated - at least at time of writing, which is day of release of D8.0.0 - the folder structure doesn't exist, or the answer is incorrect.
    – NikLP
    Commented Nov 19, 2015 at 18:12
  • Path to the vendor dir had changed since the answer was provided. Thanks to @Berdir for updating the answer drupal.stackexchange.com/posts/162813/revisions
    – anavarre
    Commented Nov 20, 2015 at 8:04
  • Running phpize (from PHP 7.0.1) in vendor/twig/twig fails for me with Cannot find config.m4. And indeed that file is nowhere to be found. What am I missing?
    – user55490
    Commented Dec 18, 2015 at 18:16
  • This answer is not true anymore. vendor/twig/twig/ext doesn't exist in the latest Drupal 8 releases.
    – avpaderno
    Commented Feb 25, 2016 at 17:23
1

In the latest Drupal 8 releases, the vendor/twig/twig/ext directory doesn't exist anymore.

To build the extension, I first globally required Twig, and then compiled the extension.

composer global require twig/twig:~1.0
cd ~/.composer/vendor/twig/twig/ext/twig
phpize
./configure
make
make install

Then, I edited the php.ini file to add a line about the new extension.

extension=twig.so

I ran the command on OS X 10.11. In a different OS, the directory where Composer put the packages globally required could be different.

I am using Drupal 8.0.4, but it could be that previous versions didn't have the ext/twig version already. From the other answers given here, I take that Drupal 8.0.1 already missed the necessary directory.

On OS X 10.11, I got the following error:

PHP Warning: PHP Startup: Unable to load dynamic library '/Applications/DevDesktop/php5_5/ext/twig.so' - dlopen(/Applications/DevDesktop/php5_5/ext/twig.so, 9): no suitable image found.
Did find: /Applications/DevDesktop/php5_5/ext/twig.so: mach-o, but wrong architecture in Unknown on line 0

Following what reported in Compiling PHP extensions on Snow Leopard with XAMPP, I then tried with the following commands.

composer global require twig/twig:~1.0
cd ~/.composer/vendor/twig/twig/ext/twig
phpize
CFLAGS=-m32 CPPFLAGS=-m32 CCASFLAGS=-m32 ./configure
make
make install

After restarting Apache, the Twig extension was listed between the PHP extensions.

screenshot

As of today (February 26, 2016), I was not able to compile the extension for PHP 7.0, since make fails with a warning and a fatal error.

/Applications/DevDesktop/php7_0/include/php/Zend/zend_hash.h:134:15: warning:
fastcall calling convention ignored on variadic function
[-Wignored-attributes]
ZEND_API void ZEND_FASTCALL zend_hash_apply_with_arguments(HashTable *ht...

/Applications/DevDesktop/php7_0/include/php/Zend/zend_portability.h:231:39: note:
expanded from macro 'ZEND_FASTCALL'
# define ZEND_FASTCALL __attribute__((fastcall))

.composer/vendor/twig/twig/ext/twig/twig.c:23:10: fatal error:
'ext/standard/php_smart_str.h' file not found
#include "ext/standard/php_smart_str.h"

2
  • Looks like they might not be bothering with support for php7: github.com/twigphp/Twig/issues/1695
    – Clive
    Commented Apr 5, 2016 at 0:26
  • To clarify Clive's comment "not bothering" is perhaps more "not needed": PHP7 without the C extension is much faster than PHP5 with the extension (figures available in the thread Clive links to).
    – AdamS
    Commented Aug 24, 2016 at 8:59
0

I've just setup a Drupal 8.0.1 site

When I cd to /path/to/docroot/vendor/twig/twig and run composer require twig/twig:~1.0

It creates another vendor folder with psr and symphony in it. Like Simon South, if I run phpize in /path/to/docroot/vendor/twig/twig, it can't find config.m4.

I found that if I cd to /path/to/docroot/vendor/twig/twig/vendor and run composer require twig/twig:~1.0 again, it creates another vendor folder which contains another twig folder with the phpize config.m4 file at

/path/to/docroot/vendor/twig/twig/vendor/vendor/twig/twig/ext/twig

I also experimented with cd /path/to/docroot/vendor, and run composer require twig/twig:~1.0, and it create the needed config.m4 at path/to/docroot/vendor/vendor/twig/twig/ext/twig

Both work, but seems awful confusing for a newbie

0

A Drupal 8.0.2 Step-by-Step Solution derived from ir4georgia's comment.

I have to use "sudo". ymmv.

cd /path/to/docroot/vendor
sudo composer require twig/twig:~1.0

The above command creates a "vendor" sub-directory in the "vendor" directory.

Move the "ext" subdirectory of the vendor subdirectory into the original d8 twig directory:

sudo mv /path/to/docroot/vendor/vendor/twig/twig/ext /path/to/docroot/vendor/twig/twig/

Delete the second vendor directory

sudo rm -r /path/to/docroot/vendor/vendor

cd to the ext/twig directory

cd /path/to/docroot/vendor/twig/twig/ext/twig

Run the installation commands.

sudo phpize
sudo ./configure
sudo make
sudo make install

Add extension=twig.so to php.ini

Restart apache:

sudo apachectl restart
1

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.