Get User IP Address in Twig Template
Suppose you want to get the ip in the page.html.twig file.

in your 


  YOURTHEME.theme


you can try

use Symfony\Component\HttpFoundation;

function YOURTHEME_preprocess_page(&amp;$variables) {
  $variables['page']['ip'] = \Drupal::request()-&gt;getClientIp();

}


in your template 


  page.html.twig


you can try:

{{ page.ip }}