Magento 2 : Create customer group programmatically

Here is the way to add customer groups programmatically in Magento 2, use $group->save() on the model returned by the GroupFactory:

namespace A2bizz\MyCustomModule\Setup;
use Magento\Framework\Module\Setup\Migration;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Customer\Model\GroupFactory;
class InstallData implements InstallDataInterface
    protected $groupFactory;
     * Customer group factory
    public function __construct(GroupFactory $groupFactory) {
        $this->groupFactory = $groupFactory;
    public function install(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        // Create the new group
        /** @var \Magento\Customer\Model\Group $group */
        $group = $this->groupFactory->create();
            ->setCode('New Customer Group');