How do I create menu links programmatically? I need to add some links to existing menu (or to new) programmatically in Drupal 8.
In order to create a menu item automatically this can be placed in a hook_update_N on the file mymodule.install and will run when the database is updated (/update.php):

use Drupal\menu_link_content\Entity\MenuLinkContent;
$items = array(
  '1' =&gt; 'Menuitem 1',
  '2' =&gt; 'Menuitem 2',
  '3' =&gt; 'Menuitem 3'
);

foreach($items as $nid =&gt; $title) {
  $menu_link = MenuLinkContent::create([
    'title' =&gt; $title,
    'link' =&gt; ['uri' =&gt; 'internal:/node/' . $nid],
    'menu_name' =&gt; 'main',
    'expanded' =&gt; TRUE,
  ]);
  $menu_link-&gt;save();
}


You can also create an entire Menu programmatically:

\Drupal::entityTypeManager()
  -&gt;getStorage('menu')
  -&gt;create([
    'id' =&gt; 'menu_test',
    'label' =&gt; 'Test menu',
    'description' =&gt; 'Description text',
  ])
  -&gt;save();