How can I set some checkboxes to selected upon the first loading of the form?
Use a #default_value:

$form['my_checkboxes'] = [
  '#type' =&gt; 'checkboxes',
  '#options' =&gt; ['1' =&gt; 'one', '2' =&gt; 'two', '3' =&gt; 'three'],
  '#default_value' =&gt; [1, 2],
];