How to install Devel and Kint on Drupal 9

How to properly setup Devel and Kint on Drupal 9?

I was recently setting up my local development environment for a new Drupal 9 project and despite abundance of documentation, couldn't get the Devel and Kint properly working at once. And because most of tutorials found online on the subject turned out to be buggy, outdated and/or obsolete, for posterity reasons I decided to log the installation steps that worked for me.

#TL;DR
drush pmu kint
composer require drupal/devel kint-php/kint
drush en devel

Now, first let's get to definitions of both development modules.

Devel

Devel is a popular module containing a variety of developer and debugging tools that make it breeze to perform common development tasks. The Devel module includes the Webprofiler, Devel generate and Kint modules. Devel also features Drush integration and provides several custom Drush commands.

Kint

Kint - a powerful and modern PHP debugging tool designed to present your debugging data in very convenient way replacing var_dump() and debug_backtrace(). It is easy to use, powerful and customizable and makes an essential addition to your development toolbox.

What is the problem?

There is no problem with installing the Devel module as it already has versions released specifically for Drupal 9. So you install and enable it with Composer and Drush as any other Drupal 9 module:

composer require drupal/devel
drush en devel

Now, the official page of Kint module at https://www.drupal.org/project/kint does not mention Drupal 9 yet, however thanks to core's compatibility layer we can use Drupal 8 modules on Drupal 9 websites, and since the same page states:

This module is oficially part of Devel 8.x module. Just enable Devel Kint module.

Which means it should be possible to download it with just firing up the composer require drupal/kint command, right? However here is the log:

composer require drupal/kint
Using version ^2.1 for drupal/kint
./composer.json has been updated
Running composer update drupal/kint
Loading composer repositories with package information
Updating dependencies
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - drupal/kint[dev-2.x, 2.1.0] require drupal/core ~8.0 -> found drupal/core[8.0.0-beta6, ..., 8.9.x-dev] but the package is fixed to 9.2.2 (lock file version) by a partial update and that version does not match. Make sure you list it as an argument for the update command.
    - drupal/kint 2.x-dev is an alias of drupal/kint dev-2.x and thus requires it to be installed too.
    - Root composer.json requires drupal/kint ^2.1 -> satisfiable by drupal/kint[2.1.0, 2.x-dev (alias of dev-2.x)].

Use the option --with-all-dependencies (-W) to allow upgrades, downgrades and removals for packages currently locked to specific versions.

Installation failed, reverting ./composer.json and ./composer.lock to their original content.

Ok, let's give it a try with the proposed --with-all-dependencies option:

composer require drupal/kint --with-all-dependencies
Using version ^2.1 for drupal/kint
./composer.json has been updated
Running composer update drupal/kint --with-all-dependencies
Loading composer repositories with package information
Updating dependencies
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - drupal/kint[dev-2.x, 2.1.0] require drupal/core ~8.0 -> satisfiable by drupal/core[8.0.0-beta6, ..., 8.9.x-dev].
    - drupal/core[8.0.0-beta16, ..., 8.3.0-rc2] require doctrine/common 2.5.* -> found doctrine/common[v2.5.0-beta1, ..., 2.5.x-dev] but it conflicts with your root composer.json require (^3.1).
    - drupal/core[8.3.0, ..., 8.7.x-dev] require doctrine/common ^2.5 -> found doctrine/common[v2.5.0-beta1, ..., 2.13.x-dev] but it conflicts with your root composer.json require (^3.1).
    - drupal/core 8.0.0-beta6 requires doctrine/common dev-master#a45d110f71c323e29f41eb0696fa230e3fa1b1b5 -> found doctrine/common[2.1.3, ..., 2.13.x-dev, 3.0.0, ..., 3.2.x-dev] but it does not match the constraint.
    - drupal/core[8.0.0-beta7, ..., 8.0.0-beta15] require doctrine/common ~2.4.2 -> found doctrine/common[v2.4.2, v2.4.3, 2.4.x-dev] but it conflicts with your root composer.json require (^3.1).
    - drupal/core[8.8.0-alpha1, ..., 8.9.x-dev] require doctrine/common ^2.7 -> found doctrine/common[v2.7.0, ..., 2.13.x-dev] but it conflicts with your root composer.json require (^3.1).
    - drupal/kint 2.x-dev is an alias of drupal/kint dev-2.x and thus requires it to be installed too.
    - Root composer.json requires drupal/kint ^2.1 -> satisfiable by drupal/kint[2.1.0, 2.x-dev (alias of dev-2.x)].

Installation failed, reverting ./composer.json and ./composer.lock to their original content.

Alas, it has also failed...

At this point I noticed that the Devel module's homepage doesn't say anything about Kint module, but references the Devel Kint Extras module as a related one. However, executing the drush en devel_kint_extras command unfortunately didn't make available the greyed out options for Kint on the /admin/config/development/devel page:

Kint
Wrapper for Kint debugging tool.
Not available. You may need to install external dependencies for use this plugin.
Kint Extended
Wrapper for Kint debugging tool with extra plugins.
Not available. You may need to install external dependencies for use this plugin.

How to properly setup Devel and Kint on Drupal 9?

So what's the solution?

Straight to the point, the only proper way of getting Kint installed on a Drupal 9 setup is to run:

composer require kint-php/kint

and that finally makes you able to set Kint as a variable dumper with the kint($variable) command in your custom modules or theme templates. Voilà! However, since Kint can be notoriously slow and it's output impossible to search, there is more to the installation process, so keep reading...

Additional steps to make Kint work flowlessly

Let's make Kint faster!

Open the settings.php or, better if enabled, the settings.local.phpfile and paste the following code:

include_once(DRUPAL_ROOT . './../vendor/kint-php/kint/src/Kint.php');
  if (class_exists('Kint')) {
    Kint::$depth_limit = 3;
   }  

If Kint's output is fast enough, then you can play with the above setting raising it to 4 or 5.

Install the fixed Search Kint module

Kint's debugging output can be really long and contain lot's of inner levels, so in order to be able to quickly find needed variable I strongly suggest to get the Search Kint module installed, which is, unfortunately, also broken and doesn't work with the latest version of Kint.

We have made necessary changes and pushed the working version of the Search Kint module to https://github.com/altagrade/search_kint, so you can either clone it with git or download and extract the .zip version into your modules directory, enable the module and enjoy happy kinting!

cd modules
git clone https://github.com/altagrade/search_kint.git
drush
en search_kint

or

cd modules
wget https://github.com/altagrade/search_kint/archive/refs/heads/main.zip
unzip
main.zip drush en search_kint

How to properly setup Devel and Kint on Drupal 9?

8 comments

The additional setting in settings.local.php file is only good for Drupal 8, because on Drupal 9 it gives:

Error: Access to undeclared static property: Kint\Kint::$max_depth in include()

Worked the first time I tried. Thanks a lot!

Too complicated.
Isn't there a library to simply put in Drupals /library folder?

Alex Shaposhnik's picture

Daniel,

The detailed steps are given for those who want to understand what's the problem and which steps were taken to get to the solution. However, you don't need to repeat all the steps described above. Just run the following command:

composer require kint-php/kint

$max_depth should now be $depth_limit

Nick Onom's picture

Thank you for a good catch, Raul, as $max_depth doesn't work anymore, but $depth_limit does. Changed the post accordingly.

Thanks for this article. Apologize in advance for newbie questions. The instructions I have found for both devel and kint modules is to install with dev option yet I have not easily found the explanation for why to include the dev option. Your article does not include installing either with the --dev option. Would appreciate if you explained the pros and cons of the dev option for installation of both devel and kint modules. Thanks.

Thank you for putting this altogether. Couldn't get it working until I've found this. Worth to note that it's better to use ksm() instead of kint() as otherwise redirections won't let you see the kint output.

We value your opinion. Please add your feedback.