CAPE TOWN, South Africa — What do you do when your city is running out of water? The answer, at least in one of the world’s most unequal countries, depends on how much money you have.

Within the next few months, Cape Town’s taps could run dry, the result of a protracted drought and a government failure to provide an alternative water source to this city of 4 million. Now residents are scrambling to find their own private solutions.

For the wealthy, that means hiring companies to dig boreholes and wells. It means buying truckloads of bottled water, even at inflated prices. It means ordering desalination machines to make groundwater drinkable — or safe enough to fill a swimming pool.

For the poor, it means waiting to see what the government comes up with, and contemplating whether you can afford to cut back on food to be able to buy water.

In modern history, no city in the developed world has ever run out of water. Cape Town’s experience, then, may be a Hobbesian test of the way people on opposite ends of the 21st century’s income gap access the most basic resources in the most dire times.

“Inequity plays out in water very obviously, and what we’re seeing in Cape Town risks becoming an example of that,” said Giulio Boccaletti, the global managing director for water with the Nature Conservancy. “The social contract breaks down if the rich find their own solution and leave the rest to fend for themselves.”

A few miles from the city’s glittering coastline, Portia Ngqulana sat in her home in the Gugulethu settlement, an agglomeration of small concrete homes and sheet-metal shacks where water arrives via communal taps, each shared by about 200 people. So far, it’s still flowing – most of the time.

“If the water stops, what can we do? We’ll eat less, I guess, and we’ll find a way to buy bottles,” she said.

Settlements such as Gugulethu have long been marginalized. During decades of white-minority rule, blacks were forcibly relocated there, and residents frequently clashed with police. In 1986, security forces in Gugulethu killed seven young black activists, a landmark in the struggle against apartheid.

Even after the end of apartheid and the election of Nelson Mandela, the neighborhood remained poor and neglected. The public water taps were among the scant examples of government assistance, and even they leaked much of the time.

“I don’t know what we’ll do if they stop flowing,” said Richard Ndabezitha, 60, who lives on a $200-per-month government pension.

Elsewhere in the city, the water shortage has prompted a surge of spending, with families investing millions of dollars to insulate themselves from the drought.

For $6,000, a borehole can be dug, tapping into underwater reservoirs. For $2,000, a company sells a machine that claims to turn moisture in the air into potable water. For $400, people can buy special washing machines that use small quantities of water. In upscale areas, bottled water has been sold out for days at a time.

“The lesson here is that you can’t trust the government to provide water for you,” said Gabby De Wet, whose family owns De Wet’s Wellpoints and Boreholes. The company now puts new clients on a waiting list and says it may not get to their requests until September. “People are taking things into their own hands.”

Last year, the World Bank surveyed 154 countries and determined the South Africa had the world’s highest Gini coefficient, a common measure of inequality. Ten percent of the population owns more than 90 percent of the country’s wealth, according to Anna Orthofer, a professor at South Africa’s Stellenbosch University.

Cape Town is a remarkable illustration of those statistics. Last year a handful of homes overlooking the Atlantic Ocean sold for around $10 million each. A luxury hotel opened in a renovated grain silo, offering its penthouse suite for $10,000 a night. Fifteen minutes away, in the settlement of Khayelitsha, the per capita income is less than $2,000 per year.

Inequality here is often racialized, with whites concentrated in the city’s wealthiest enclaves. The government says the poor, informal settlements will be prioritized in its emergency water distribution plan, making them among the last places to lose water. So far, because of the shortages, the city has imposed a daily consumption limit of 13 gallons per person.

“The city is very aware of the need to be sensitive to the vulnerable and poor,” said Xanthea Limberg, the member of Cape Town’s city council in charge of water and waste services. Under the city’s disaster plan, informal settlements will receive water as long as it is available to prevent disease from spreading through densely populated areas, she said.

Cape Town was originally expected to run out of water in April; now, thanks to conservation, that potential “Day Zero” has been pushed back to July, officials say.

In the United States and Europe, until the early 20th century, clean water was largely supplied to homes by private wells or utilities, and poor residents often had less access to it. The result was frequent outbreaks of diseases such as cholera in places with poor sanitation. In the early 1900s, urban planners began to consider water as a public good, distributed without regard for economic status and funded by a broad tax base. When water runs out, that system and its underlying philosophy could be weakened.

Cape Town officials have tried to respond in a democratic way, opening up natural springs at the base of Table Mountain to anyone with a water jug.