The developer behind a controversial affordable housing project in South Hingham has abandoned the project and could allow the town to buy the land instead.

Hingham officials announced Thursday night that they had reached an agreement with AvalonBay Communities, the developer that had been pushing a proposed 177-unit rental project, to buy 18 acres of land off Recreation Park Drive from several landowners for $3.7 million. In a statement, officials said the town is looking to use the property for the development of a wastewater treatment plant.

Such a facility has been in the works for years. Town meeting voters in 2011 allocated $250,000 to allow the town to purchase land for a South Hingham sewer plant and begin engineering work. Officials have said the project would help spur development in South Hingham, where many facilities rely on outdated septic systems, and ease tax bills for residents by raising more taxes from commercials properties.