Programmatically create a user account I know that I can programmatically create a user using the following code, in Drupal 7. 

$new_user = array(
  'name' =&gt; $name,
  'pass' =&gt; $sifra, // note: do not md5 the password
  'mail' =&gt; $email,
  'status' =&gt; 1,
  'init' =&gt; $email,
  'roles' =&gt; array(
    DRUPAL_AUTHENTICATED_RID =&gt; 'authenticated user',
    3 =&gt; 'custom role',
  ),
);

// The first parameter is sent blank so a new user is created.
user_save('', $new_user);


What is the equivalent code in Drupal 8?
For Drupal 8, the code to create a user entity is the following one.

$language = \Drupal::languageManager()-&gt;getCurrentLanguage()-&gt;getId();
$user = \Drupal\user\Entity\User::create();

// Mandatory.
$user-&gt;setPassword('password');
$user-&gt;enforceIsNew();
$user-&gt;setEmail('email');
$user-&gt;setUsername('user_name');

// Optional.
$user-&gt;set('init', 'email');
$user-&gt;set('langcode', $language);
$user-&gt;set('preferred_langcode', $language);
$user-&gt;set('preferred_admin_langcode', $language);
$user-&gt;set('setting_name', 'setting_value');
$user-&gt;addRole('rid');
$user-&gt;activate();

// Save user account.
$result = $user-&gt;save();


The code can be found on Create a user account programmatically in Drupal 8.