HAVANA (Reuters) - Fidel Castro, the Cuban revolutionary who built a communist state on the doorstep of the United States and for five decades defied U.S. efforts to topple him, died on Friday. He was 90.

A towering figure of the 20th century and Cold War icon, Castro stuck to his ideology beyond the collapse of Soviet communism and remained widely respected in parts of the world that struggled against colonial rule.

Castro had been in poor health since an intestinal ailment nearly killed him in 2006. He formally ceded power to his younger brother, Raul, two years later.

Wearing a green military uniform, a somber Raul Castro, 85, appeared on state television on Friday night to announce Fidel's death, 60 years to the day since the two brothers and dozens of supporters left Mexico on a boat to take revolution to Cuba.

"At 10:29 at night, the chief commander of the Cuban revolution, Fidel Castro Ruz, died," he said, without giving a cause of death.

"Ever onward, to victory," he said, using the slogan of the Cuban revolution.

A mix of tributes and condemnation poured in from around the world.

"History will record and judge the enormous impact of this singular figure on the people and world around him," U.S. President Barack Obama said, extending "a hand of friendship" to Cuba.

Obama's elected successor, Donald Trump, said he would work to ensure the Cuban people "can begin their journey toward prosperity and liberty," even though "the tragedies, deaths and pain caused by Fidel Castro cannot be erased."

Trump's Vice President-elect, Mike Pence, was more blunt about the late leader: "The tyrant Castro is dead. New hope dawns. ... Viva Cuba Libre!" he said on Twitter.

Venezuela's socialist president, Nicolas Maduro, urged revolutionaries to follow Castro's legacy, while Pope Francis said he was grieving and praying for the repose of the professed atheist, whom he met in Cuba last year.

China's president, Xi Jinping, said "the Chinese people have lost a close comrade and a sincere friend."

DECADES OF HOSTILITY

Raul Castro, who glorified his older brother, has nonetheless changed Cuba since taking over by introducing market-style economic reforms and agreeing with the United States in December 2014 to re-establish diplomatic ties and end decades of hostility.

It is unclear whether Trump, who takes office on Jan. 20, will continue those efforts to normalize relations with Cuba or fulfill a campaign promise to close the U.S. embassy in Havana once again.

Fidel Castro himself offered only lukewarm support for the 2014 deal with Washington, raising questions about whether he approved of ending hostilities with his longtime enemy, a conflict that took the world to the brink of nuclear war during the Cuban Missile Crisis in 1962.

He did not meet Barack Obama when he visited Havana earlier this year, the first time an American president had stepped foot on Cuban soil since 1928.

Days later, Castro wrote a scathing newspaper column condemning Obama's "honey-coated" words and reminding Cubans of the U.S. efforts to overthrow and weaken the Communist government.

Cuba declared nine days of mourning, during which time his ashes will be taken to different parts of the country. A burial ceremony will be held on Dec. 4.

Hundreds of students gathered in Havana on Saturday to honor Castro, and mass rallies are planned for the coming days.

"(We're here) for everything Fidel has done since the triumph of the revolution," said one student, Raul Alejandro Palmeros. "Fidel put Cuba on the map, and made Cuba a paradigm for the people of the world, especially the poor and the marginalized."

But in Miami, where many exiles from Castro's government live, a large crowd waving Cuban flags cheered, danced and banged on pots and pans.

MILITARY FATIGUES, CIGARS

The bearded Fidel took power in a 1959 revolution and ruled Cuba for 49 years with a mix of charisma and iron will, creating a one-party state and becoming a central figure in the Cold War.

He was demonized by the United States and its allies but admired by many leftists around the world, especially socialist revolutionaries in Latin America and Africa.

Nelson Mandela, once freed from prison in 1990, repeatedly thanked Castro for his efforts in helping to weaken apartheid.

In April, in a rare public appearance at the Communist Party conference, Fidel Castro shocked party apparatchiks by referring to his own imminent mortality.

"Soon I will be like all the rest. Our turn comes to all of us, but the ideas of the Cuban communists will remain," he said.

Transforming Cuba from a playground for rich Americans into a symbol of resistance to Washington, Castro crossed swords with 10 U.S. presidents while in power, and outlasted nine of them.

He fended off a CIA-backed invasion at the Bay of Pigs in 1961 as well as countless assassination attempts.

Wearing green military fatigues and chomping on cigars for many of his years in power, Castro was famous for long, fist-pounding speeches filled with blistering rhetoric, often aimed at the United States.

At home, he swept away capitalism and won support for bringing schools and hospitals to the poor. But he also created legions of enemies and critics, concentrated among the exiles in Miami who saw him as a ruthless tyrant.

"With Castro's passing, some of the heat may go out of the antagonism between Cuba and the United States, and between Cuba and Miami, which would be good for everyone," said William M. LeoGrande, co-author of a book on U.S.-Cuba relations.

Castro's death - which would once have thrown a question mark over Cuba's future - seems unlikely to trigger a crisis as Raul Castro is firmly ensconced in power.

Fidel Castro no longer held leadership posts in the last years of his life. He wrote newspaper commentaries on world affairs and occasionally met foreign leaders, but lived in semi-seclusion.

Still, the passing of the man known to most Cubans as "El Comandante" - the commander - or simply "Fidel" leaves a huge void in the country he dominated for so long. It also underlines the generational change in Cuba's communist leadership.

Raul Castro has vowed to step down when his term ends in 2018, and the Communist Party has elevated younger leaders to its Politburo, including 56-year-old Miguel Diaz-Canel, who is first vice president and the heir apparent.

REVOLUTIONARY ICON

A Jesuit-educated lawyer, Fidel Castro led the revolution that ousted U.S.-backed dictator Fulgencio Batista on Jan 1, 1959. Aged 32, he quickly took control of Cuba and sought to transform it into an egalitarian society.

His government improved the living conditions of the very poor, achieved health and literacy levels on a par with rich countries and rid Cuba of a powerful Mafia presence.

But he also tolerated little dissent, jailed opponents, seized private businesses and homes, and monopolized the media.

Hundreds of thousands of Castro's opponents fled the island.

"The dictator Fidel Castro has died, the cause of many deaths in Cuba, Latin American and Africa," Jose Daniel Ferrer, leader of the island's largest dissident group, the Patriotic Union of Cuba, said on Twitter.

Many dissidents settled in Florida, influencing U.S. policy toward Cuba and plotting Castro's demise. Some even trained in the Florida swamps for the disastrous Bay of Pigs invasion.

But they could never dislodge him.

Castro claimed he survived or evaded hundreds of assassination attempts, including some conjured up by the CIA.

In 1962, the United States imposed a damaging trade embargo that Castro blamed for most of Cuba's ills, using it to his advantage to rally patriotic fury.

Over the years, he expanded his influence by sending Cuban troops into faraway wars, including 350,000 to fight in Africa. They provided critical support to a left-wing government in Angola and contributed to the independence of Namibia in a war that helped end apartheid in South Africa.

Castro also won friends by sending tens of thousands of Cuban doctors abroad to treat the poor and bringing young people from developing countries to train them as physicians.

'HISTORY WILL ABSOLVE ME'

Born on Aug. 13, 1926, in Biran in eastern Cuba, Castro was the son of a Spanish immigrant who became a wealthy landowner.

Angry at social conditions and Batista's dictatorship, Castro launched his revolution on July 26, 1953, with a failed assault on the Moncada barracks in the eastern city of Santiago.

"History will absolve me," he declared during his trial.

He was sentenced to 15 years in prison but was released in 1955 after a pardon that would come back to haunt Batista.

Castro went into exile in Mexico and prepared a small rebel army to fight Batista. It included Argentine revolutionary Ernesto "Che" Guevara, who became his comrade in arms.

On Nov. 25, 1956, Castro and a ragtag band of 81 followers set sail from the Mexican port of Tuxpan aboard an overloaded yacht called "Granma," reaching Cuba in early December.

Only 12, including Fidel, Raul and Guevara, escaped a government ambush when they landed in eastern Cuba.

Taking refuge in the rugged Sierra Maestra mountains, they built a guerrilla force of several thousand fighters who, along with urban rebel groups, defeated Batista's military in just over two years.

Early on, at the height of the Cold War, Castro allied Cuba to the Soviet Union, which protected the Caribbean island and was its principal benefactor for three decades.

The alliance brought in $4 billion worth of aid annually, including everything from oil to guns, but also provoked the 1962 Cuban Missile Crisis when the United States discovered Soviet missiles on the island.

Convinced that the United States was about to invade Cuba, Castro urged the Soviets to launch a nuclear attack.

Cooler heads prevailed. Soviet leader Nikita Khrushchev and U.S. President John F. Kennedy agreed the Soviets would withdraw the missiles in return for a U.S. promise never to invade Cuba. The United States also secretly agreed to remove its nuclear missiles from Turkey.

'SPECIAL PERIOD'

When the Soviet Union collapsed in 1991, an isolated Cuba fell into an economic crisis that lasted for years known as the "special period." Food, transport and basics such as soap were scarce and energy shortages led to frequent and long blackouts.

Castro undertook a series of tentative economic reforms to get through the crisis, including opening up to foreign tourism.

The economy improved when Venezuela's late socialist leader, Hugo Chavez, who looked up to Castro as a hero, came to the rescue with cheap oil. Aid from communist-run China also helped, but Venezuelan support has eased since Chavez's death in 2013.

Plagued by chronic economic problems, Cuba's population of 11 million has endured decades of hardship, although not the deep poverty, violent crime and government neglect of many other developing countries.

Cuban state workers earn on average the equivalent of $25 a month and struggle to make ends meet even in an economy where education and healthcare are free and many basic goods and services are heavily subsidized.

Still, for most Cubans, Castro has been the ubiquitous figure of their lives.

Many love him and share his faith in communism, and even some who abandoned their political belief still respect him.

"For everyone in Cuba and outside his death is very sad," said Havana resident Luis Martinez. "It is very painful news."