On the Chaouia plain south of Casablanca, where Morocco’s best wheat has grown for a century, Hassan Ouazzani did not bother to plant last autumn. “Four years in a row I put seed in the ground and watched it die,” he said, standing in a field of thistles. “This year I sold the tractor.”

Morocco is in the seventh year of the longest drought in its recorded history, and the arithmetic has become stark. The country harvested 3.1 million tons of cereals this year, the agriculture ministry said last week, about half the 10-year average and a third of the 2015 crop. To feed 37 million people it will import close to 7 million tons of wheat in the 12 months to June, the ministry projected, a figure that would make the kingdom the world’s fourth-largest buyer, behind only Egypt, Indonesia and Turkey.

Reservoirs behind the country’s 150 large dams stood at 31 percent of capacity on Oct. 1, the water ministry reported, compared with an average of 54 percent for the date over the previous two decades. The Al Massira dam, which supplies Casablanca and irrigates the Doukkala farming region, was at 9 percent. Irrigation from it has been suspended since 2023.

“We are watching a climate shift happen in real time, not in a model,” said Fatima Zahra Benjelloun, an agricultural economist at the Hassan II Institute of Agronomy in Rabat. Rainfall in the northern grain belt has fallen about 20 percent since the 1980s, she said, but the more damaging change is timing: the autumn rains that germinate wheat now often arrive a month late or not at all. “A farmer can survive less rain. He cannot survive rain that comes after he has given up.”

The government’s response has been to build. A desalination plant outside Casablanca, due to be fully operating next year, will be Africa’s largest, producing 300 million cubic meters of water annually, and a 67-kilometer pipeline completed in 2024 now moves water from the wetter Sebou basin in the north to Rabat and Casablanca. Youssef El Amrani, a director at the water ministry, said Morocco would have 16 desalination plants by 2030, powered largely by the solar and wind farms that have made the country a renewable energy showcase. “We have decided that the cities will not run dry,” he said. “The question is what happens to the countryside.”

That question is already being answered by migration. The agriculture ministry estimates that drought has eliminated the equivalent of 180,000 full-time farm jobs since 2019, and the slums on the edges of Casablanca and Marrakesh have grown by hundreds of thousands. Herders in the south have lost up to 40 percent of their sheep, and in May the government canceled the traditional sacrifice for the Eid al-Adha holiday, as it had the year before, to protect the national flock.

Ms. Benjelloun said the country needed to think about which crops it could afford to grow at all. Morocco exports water-hungry tomatoes, avocados and watermelons to Europe, earning foreign currency that then pays for wheat. “We are, in effect, exporting our groundwater to Rotterdam,” she said. “It made sense when the aquifers were full.”

Mr. Ouazzani said his two sons now work on a construction site in Casablanca. “If it rains in November, properly, I will borrow for seed,” he said. “I say that every year.”