how to prevent ubuntu from sleeping when lid is closed

C++
Go to System Settings and then click on Power. In the power setting, make sure that option for 'When the lid is closed' is set to Suspend. If you had a different setting here, you should check if you are able to suspend Ubuntu by closing the lid.
Source

Also in C++: