As others pointed out, there is software for it. Many people use Postfix as main software and then combine it with Dovecot for IMAP.
But of course there are other softwares and the choice pretty much depends on your requirements regarding scale, security and ease of setup or maintenance. The reason why things are so scattered is probably because even in basic setups, there are a number of technologies involved, like for Spam filtering, SPF and DKIM. And not many people do this kind of thing because there's Gmail. ;-)
But if you are just looking for anything to get started, there are some Docker setups which do exactly what you ask for: https://github.com/tomav/docker-mailserver