Five years after a U.N.-backed force began to push al-Qaeda-linked militants out of their strongholds, Somalia boasts clear signs of progress. Large swaths of the country have been reclaimed. Streets, beaches and markets have come back to life in once forsaken cities. The United States has promised to rebuild its long-shuttered embassy.

But as Somalia approaches a critical period, with parliamentary and presidential elections due by August, those gains are showing signs of reversal.

The al-Shabab rebels are “resurgent,” President Hassan Sheik Mohamud said in an interview last week. He and other senior officials acknowledged that ­Somalia’s government is still ­unable to provide security or public services to regions that have been liberated. The government must choose between giving its soldiers wages or weapons, he said.

“The Somali government cannot afford to pay the soldiers and at the same time to purchase lethal equipment,” Mohamud said. “This is the dilemma that we have.”

Western officials say they have provided ample aid but that much of it is diverted through corruption and that the Somali government must do a better job of constructing a security force that fits within its budget.

Somalia has been racked by near-constant conflict for the past quarter-century, resulting in chaos that provided fertile ground for the rise of al-Shabab in 2005. The United States has since spent more than $2 billion on Somalia, sending military trainers and killing militants, including two al-Shabab commanders, in drone strikes. The White House sees the group as one of its top concerns in sub-Saharan Africa, in part because its attacks extend beyond Somalia to civilian targets in neighboring Kenya, including the bloody strike on Nairobi’s upscale Westgate Mall in 2013.

African Union forces led an operation to push al-Shabab from Mogadishu in 2011 and went on to launch a series of offensives that prompted the militants to withdraw unilaterally from much of the territory they occupied.

But those African Union forces, which are supported by the United Nations and known by the acronym AMISOM, have largely remained confined to their bases over the past year, unwilling and unequipped to conduct offensive operations. Al-Shabab militants have encircled numerous population centers, cutting off supply lines and often moving into residential areas at night.

“AMISOM and the Somali national army have remained in the barracks,” Mohamud said, speaking in English in his office in a hilltop compound. “The operations were less. That’s what gave space to al-Shabab.”

Signs of the group’s resurgence can be seen every day. Hours before a Washington Post reporter interviewed Mohamud on April 5, a member of parliament was gunned down in the capital. The day before, two intelligence agents were killed.

“The enemy is more daring and more opportunistic,” Foreign Minister Abdusalam Omer said. “We’re working hard on stabilization, but for the Somali government it’s like building a plane and flying it at the same time.”

The Somali security forces, which are eventually supposed to assume responsibility for the fight, remain weak and disorganized, according to Western and Somali officials. On paper, the army has 22,000 troops, but analysts say there could be as few as 10,000. Last year, the U.N. Monitoring Group on Somalia and Eritrea wrote in a report that the army “hierarchy has systematically inflated their troop numbers in order to secure greater funding for salaries and rations.”

A European Union training mission was recently halted for several weeks because trainers worried that Somali troops who hadn’t been paid might hold them hostage to bring attention to their plight.

Many of the soldiers are allied with Somalia’s numerous clans, with varying loyalty to the central government. Outside the military, regional fighting forces also battle al-Shabab in Somalia’s semi-autonomous regions.

“How do we put them together?” Mohamud asked about the groups of pro-government fighters. “Will Shabab wait while we integrate these men and then send them back to the battlefield?”

The problem goes beyond integrating clan-based groups. The military lacks arms, vehicles and bases.

Maj. Gen. Mohamed Sheik Hamud, the country’s police commissioner, described the situation facing the security forces in stark terms. “It’s like the police are in Guantanamo Bay,” he said. “We send them to these newly liberated districts, but they’re surrounded. They can’t go anywhere.”

‘We don’t have the resources’

Some foreign donors have grown frustrated with the military’s dysfunction. The United States provides a $100 monthly stipend to 7,000 Somali soldiers — in theory a supplement to their wages, but it is often their only income.

“There’s now very serious rethinking in donor capitals about whether support for the army is appropriate,” said Matt Bryden, a Somalia expert and the founder of Sahan Research, a Nairobi-based research organization. “For the first two to three years of this [Mohamud’s] administration, there was almost blind support. Now there’s recognition that it’s not going to work.”

As it did years ago, al-Shabab is using children to fight on its behalf. Some of them are forcibly recruited. Others join willingly because the extremists are the only ones offering education or jobs.

“We don’t have the resources to provide what [young people] need,” said Mohamud, a former academic and UNICEF employee who was elected in 2012.

Outside his office were pictures of Somalia’s former leaders, including Mohamed Siad Barre, a military dictator who ruled from 1969 to 1991, when he was overthrown and Somalia disintegrated. His heavy-handed rule inflamed clan and regional rivalries, leading to fractures still visible today.

“When the state collapsed, people got allegiance to their clans,” Mohamud said. “Now, convincing them to realign themselves with the state, handing over their weapons . . . this is what we’re working very hard to do.”

Somalia experts say that while the country improves its security forces, it must simultaneously work on reaching a political settlement with al-Shabab. That is essential because “al-Shabab’s freedom to operate depends in large measure on its ability to exploit long-standing clan dynamics, mistrust among the ­Somali people and the lack of economic opportunity,” said a U.S. official, who spoke on the condition of anonymity because he was not authorized to comment publicly.

The U.S. government has been reluctant to send troops to Somalia since the “Black Hawk Down” operation that left 18 Americans dead in October 1993. Instead, it has conducted one of its largest drone programs, welcomed by the Somali government.

“I heard that in Pakistan and Afghanistan the experience of the drones was not good,” Mohamud said. “But here they are precise, and we are informed of them before.”

A strike last month killed 150 militants, according to the Pentagon. But the drone program hasn’t stopped al-Shabab from regenerating.

Hamud, the police commissioner, offers the example of Jalalaqsi, the town in southern Somalia where Mohamud was born. It was liberated by troops from Djibouti in 2014. But the national police could afford to send only 10 officers. They were dropped off by a U.N. helicopter, he said, but have little ammunition or logistical support.

“Now they’re totally neglected. They can’t move anywhere,” he said. “You can’t rebuild a country like that.”

Read more:

How a breakaway region of Somalia hopes to build a new country

U.S. Special Operations forces launch helicopter raid in Somalia

Somalia banned Christmas and New Year’s — even though it celebrates neither