Beyond all the other reasons listed here: cost.

Building housing in the San Francisco Bay Area, where these companies are based (they, of course, have offices all over the world as well), is quite expensive. The per-unit cost of a 100-unit affordable housing project in California is over $425,000.

That cost could potentially be reduced a little in this case. It's possible the company already owns the land (though it won't be zoned for residential use). They can build microapartments and fit more in the same space. They could avoid building actual apartments entirely, figuring that the food and other amenities on campus are sufficient to not bother with kitchens (though they'd have to staff them up more on weekends and holidays), and build dorms instead. That would require approval from the local government (which won't be forthcoming), as local building codes are designed to stop you from doing that. And if you do manage to build a dorm instead of real apartments, you have a building no landlord is going to want to buy if your company ever wants to get out of the housing business.

As a comparison, consider the new MBA student housing that Stanford University recently built, which comes with the sort of design and amenities tech workers are likely to want: 202 units for $75 million, or $371,287/unit, and Stanford already owned the land. Plus you have to pay property tax on the place, maintain it, staff and service it, etc... The monthly costs, assuming you want to pay the thing off after a few decades, aren't going to be that much lower than the $2,600/mo average rent for a studio apartment vaguely in the area.

In other words, housing for employees costs radically more than cafeterias and haircuts.

Tech companies will rent corporate housing for their employees as needed (it's rather expensive), generally for a limited period of time when employees relocate, internships, and other circumstances. That said, Google has made inquiries into modular housing, which could might construction costs low enough to make a significant difference, and presumably will continue to consider whether the math works out.