For more than 40 years, Col. Moammar Gaddafi was the eccentric, unpredictable and brutal face of Libya, an oil-rich country that became an international pariah. Defiant to the last, he was killed Thursday in Sirte, his home town, eight months after he vowed to die rather than concede defeat to a popular uprising.

It was an ignominious end for Col. Gaddafi, who had managed in his waning years to rehabilitate himself in foreign eyes but then left even supporters appalled and sickened as he unleashed his army against his people in what proved to be a doomed effort to suppress this year’s revolution.

Deposed in August when rebel forces won control of the capital, he was killed in crossfire in Sirte, his loyalists’ last redoubt, after being dragged alive from a sewer culvert where he had taken refuge, said Mahmoud Jibril, the rebel leader who is Libya’s interim prime minister.

He became the first Arab ruler to be slain by his people in the transformative revolt that has come to be known as the Arab Spring, pitting thousands of citizen demonstrators against aging dictators and despots. His downfall followed the toppling of authoritarian rulers in Tunisia and Egypt, who were ousted before protesters took to the streets of eastern Libya in February.

Col. Gaddafi was thought to be 69, although his birth date was not known. At his death, he had been one of the world’s longest-serving rulers.

Many in the international community had long dismissed him as a clown for his quirky behavior. He traveled with an all-female praetorian guard and received guests in a Bedouin tent. But much of his reign was brutal.

Col. Gaddafi drew global condemnation for many years because of his decades-long patronage of terrorist groups. President Ronald Reagan referred to the Libyan leader as “this mad dog of the Middle East,” and for many, Col. Gaddafi’s name will always be associated with the 1988 terrorist bombing of Pan Am Flight 103 over Lockerbie, Scotland, that killed 270 people.

At home, where Col. Gaddafi was more benignly known as “Brother Leader and guide of the revolution,” his long reign attested to his intelligence and political acumen and made Libya a nation to be noticed. It also brought the North African desert country economic ruin and decades of brutal political repression, as Col. Gaddafi used pervasive fear and frequent policy swings to keep his subjects off-balance.

As Libyan political scientist Mansour O. El-Kikhia wrote in “Libya’s Qaddafi: The Politics of Contradiction”: “The rules of the game in Libya continually change,” and Col. Gaddafi’s “genius . . . is his ability to maintain and manipulate this chaos . . . because the survival of his regime hinges on continued turbulence.”

Col. Gaddafi was 27 when he led a military coup to overthrow King Idris in 1969. With the death a year later of Col. Gaddafi’s childhood hero, Egyptian leader Gamal Abdel Nasser, the Libyan became a key voice of revolution, espousing a socialist pan-Arabism and militant rhetoric against Israel.

But Col. Gaddafi’s erratic behavior embarrassed and alienated his Arab peers. Weary of his antics and wary of his plots to overthrow and assassinate other leaders in the region, they turned a deaf ear to his appeals for Arab unity. They also had no affinity for Col. Gaddafi’s idiosyncratic worldview, the Third Universal Theory, which he laid out in a mass-produced manual, the Green Book.

Col. Gaddafi’s real personality was difficult to grasp, partly because he maintained a distracted air in conversation. Observers of his early career described a winning smile, but later interviewers found him generally poker-faced. He controlled his image, dressing in tribal robes and at one point flying a Brazilian plastic surgeon to his Tripoli compound for an operation to reduce wrinkles on his face and add hair plugs.

His 1970 marriage to a former nurse named Safia produced seven children, of whom the most prominent, Saif al-Islam, had cultivated a reputation as a reformer; that image was shattered after he came out in support of his father’s crackdowns this year.

The Gaddafi family was known for elaborate spending, on anything from vanity construction projects inside Libya to private concerts by international stars such as Lionel Richie, Beyonce and Mariah Carey; once this year’s uprising began, some of those entertainers said they regretted performing for the Gaddafis.

An earlier, brief marriage, from which Col. Gaddafi has one son, ended in divorce.

Inspired by Nasser

Col. Gaddafi was born in 1942 near the coastal town of Sirte. His father was a Bedouin farmer of modest means, and Col. Gaddafi herded the family animals as a child. He graduated from the University of Libya in 1963 and attended the Libyan Royal Military Academy. In 1966, he trained in armored warfare for six months in England.

Inspired by Nasser, he began secretly plotting in his teens against Libya’s monarchy, which he considered too beholden to foreigners, some biographers have said.

On Sept. 1, 1969, the army signal corps captain and his co-conspirators easily wrested control of key government installations in Tripoli and Benghazi. King Idris, abroad on a trip, abdicated within days.

Gaddafi took the rank of colonel and leadership of the Revolutionary Command Council, which asked British and U.S. military forces to leave the country. Washington, which had recognized the junta as Libya’s new government, evacuated its leased air base at Wheelus Field by mid-1970.

The coup launched a period of major social, cultural and economic change in Libya. With Islam and socialism as the government’s lodestars, banks, businesses and some foreign-owned oil ventures were nationalized. Alcohol, gambling and nightclubs were banned. The government also used its oil wealth to build homes, schools and hospitals. Literacy and life expectancy rose; women gained political and economic rights.

In 1977, Col. Gaddafi accelerated his social engineering by implementing what he called the jamahiriya, or “state of the masses,” a system in which the country was ruled by hundreds of locally based “basic popular congresses,” “people’s congresses” and “revolutionary committees.” These reported to the national General People’s Congress, chaired by Col. Gaddafi. The country was renamed the Great Socialist People’s Libyan Arab Jamahiriya. A year later, Col. Gaddafi unveiled the first volume of his Green Book.

In later years, he would say that he relinquished power in 1977 and that Libya was “self-managed by the people.” In reality, he was Libya’s ruler. And as opposition to his regime grew, so did repression by his security apparatus. Over the next two decades, hundreds were imprisoned and many never seen again. At one point, opponents were publicly hanged on university campuses and hit squads targeted exiled Libyan dissidents.

Along with repression came more bizarre policies. Personal bank accounts were nationalized, and families were told to economize by raising chickens at home and using only one bar of soap a week.

U.S.-Libya relations

U.S-Libya relations worsened as Col. Gaddafi’s rhetoric grew increasingly anti-American and anti-Israeli and as he sought to undermine moderate, pro-Western states such as Morocco and Jordan.

His backing of an array of terrorist groups caused the greatest consternation. In 1985, Libya was linked by U.S. intelligence to the hijacking by Palestinians of the Achille Lauro cruise liner and to attacks on passengers in the Rome and Vienna airports. U.S. officials also alleged that Col. Gaddafi was intending to assassinate Reagan and other U.S. officials.

In April 1986, an explosion ripped through a West Berlin discotheque frequented by U.S. soldiers, killing two servicemen and a Turkish woman. Washington blamed Libya, and at a news conference Reagan uttered the “mad dog” jibe, which he said he first heard used by an American reporter on television.

On the night of April 15, 1986, U.S. warplanes bombed airfields, government offices and suspected terrorist training camps near Tripoli and Benghazi. Col. Gaddafi said his adopted daughter was killed in the raids.

Two years later, New York-bound Pan Am 103 exploded over Lockerbie, killing all aboard and 11 people on the ground. A year later, a French airliner with 170 people on board blew up over the Niger desert.

Western governments concluded that Libya was responsible for both attacks. In 1991, the United States and Britain indicted two Libyans in the Lockerbie explosion, and French courts charged four Libyans in the French plane bombing.

When Col. Gaddafi, who once called Reagan “an aging third-rate actor,” refused to extradite the two charged in the Lockerbie attack, the United Nations imposed sanctions, including an air embargo. The United States, which had some sanctions in place, initiated more restrictive measures that shut down all bilateral contact.

The upshot for Libya was devastating. With the country isolated, its economy withered and public disaffection with the regime spread. Col. Gaddafi had to rethink his approach. “In the mid-’90s, he discovered the practical necessity of coming to terms with the world,” said David Mack, a former State Department official who served in Libya.

In 1999, Col. Gaddafi handed over the Lockerbie suspects for trial by a Scottish court. One was acquitted; the other was convicted and sentenced to life, but he was released in 2009 on compassionate grounds because he has terminal cancer. He is alive, and Libya’s transitional government has said it will not re-try him. That year, Libya paid Paris $33 million for the 1989 downing of the French airliner.

In 2003, Libya agreed to the remaining terms for lifting U.N. sanctions by formally accepting “responsibility for the actions of its officials” in connection with Lockerbie and agreeing to pay up to $2.7 billion to victims’ families.

Col. Gaddafi never accepted personal responsibility for Lockerbie, and when asked about it in subsequent years, he said that it was “part of the past.” In 2009, The Washington Post quoted him saying, “We never acknowledged any guilt.”

Some longtime observers of Libya interpreted the Pan Am downing as retaliation for the 1986 U.S. bombing of Libya. Others remained unconvinced that Libya was behind the attack or, if involved, that it had acted alone. Syria and Iran were also suspected of having played roles.

Col. Gaddafi’s quick condemnation of the Sept. 11, 2001, terrorist attacks on the World Trade Center and the Pentagon facilitated Libyan-U.S. reconciliation, as did Libya’s declaration in December 2003 that it was abandoning secret programs to acquire weapons of mass destruction. In September 2004, U.S. sanctions were canceled and, in 2009, the countries established full diplomatic relations for the first time since 1973.

Back in the good graces of the international community, Col. Gaddafi in 2009 made his first visit to the United States, for the annual meeting of the U.N. General Assembly, which was then chaired by a Libyan.

He addressed the General Assembly for more than 90 minutes, pushing his idea for Israel and the Palestinian territories to unite in a state called “Isratine.” At the same time, he placed the blame for Libya’s failures on the country’s 6 million people.

“If we raised the Libyan people’s awareness of the Green Book, we could create a model to be followed at the world level,” he said in a 2009 interview with al-Jazeera TV. However, he said, “the awareness was backward and not up to the idea of a society of the masses. This is what we feel sorry for. I wish this was not the case. I wish the Libyan people had a different level of awareness.”

A violent turn

When this year’s popular revolutions spread into Libya, Col. Gaddafi was quick to resort to violence. After rebels seized control of the eastern city of Benghazi, Col. Gaddafi vowed to hunt down his critics “alley by alley, house by house” and ordered his security forces to gun down unarmed demonstrators.

Many members of his diplomatic service and armed forces defected, some joining the opposition government and military in the eastern part of the country. Col. Gaddafi’s crackdown drew condemnation from Western leaders, the U.N. Security Council and the Arab League, which suspended Libya’s membership.

Unlike the Tunisian and Egyptian leaders, however, Col. Gaddafi dug in, vowing to “die as a martyr” in Libya. Calling his opponents “cockroaches,” he accused them of having ties to al-Qaeda and taking hallucinogenic drugs.

Libya’s military had long been kept weak and fragmented, apparently to prevent any possible challenge to his rule. But militias led by his sons — and staffed in many cases by paid mercenaries from outside Libya — seemed initially poised to crush the opposition’s army, largely composed of enthusiastic but untrained civilians.

An international coalition began airstrikes in March and the battle evened out, although Col. Gaddafi’s forces continued to pound rebel-held cities and towns in the west, killing hundreds of civilians.

As NATO strikes intensified and more countries recognized the opposition government, Col. Gaddafi seemed to exist in a fantasy world. His loyalists stage-managed pro-government rallies and denied that his forces were targeting civilians. For weeks it was unclear whether Col. Gaddafi was hiding in his compound in Tripoli, a labyrinth of luxury underground bunkers, even as NATO bombs killed one of his sons, Saif al-Arab, and three grandchildren.

Finally, as the rebels closed in on the capital, the Gaddafis vanished from Tripoli. Col. Gaddafi’s daughter and other family members fled to Algeria, one son was killed and others remained at large. Exhorted by messages from Col. Gaddafi and others in his inner circle, loyalists had continued to put up resistance in Sirte and Bani Walid until those towns finally fell this week to the anti-Gaddafi forces.

Murphy is a special correspondent.