Greek politicians have ratified an agreement to end a nearly three-decade-long dispute over neighbouring Macedonia's name, in a landmark vote that will see the small country renamed North Macedonia and clear its path to Nato membership.

The deal passed with 153 votes in the 300-member parliament, two more than needed. It has faced fierce opposition in both countries and recently cost Prime Minister Alexis Tsipras his parliamentary majority after a small right-wing party quit the governing coalition in protest. It passed with the support of independent MPs.

Mr Tsipras' left-wing Syriza party holds 145 seats in parliament, six short of a majority.

"Today is a historic day," he said. "Greece is safeguarding an important part of its history, its heritage of ancient Greek Macedonia. Today we are writing a new page for the Balkans."

Under the deal, Macedonia changes its name to North Macedonia, and Greece drops its objections to the country joining Nato and eventually the European Union.

"Congratulations my friend @tsipras_eu [Alexis Tsipras], together with our peoples we reached a historical victory," Macedonian Prime Minister Zoran Zaev tweeted.

The ratification was quickly welcomed by both Nato and the European Union.

The vote was "an important contribution to the stability and prosperity of the whole region", tweeted Nato Secretary-General Jens Stoltenberg. "I look forward to the future Republic of North Macedonia joining #NATO."

The ratification came after three days of acrimonious parliamentary debate and numerous street protests, some of which turned violent. A rally last Sunday saw tens of thousands of people gather outside parliament, with clashes erupting between groups of demonstrators and riot police.

An opponent of the Macedonia agreement holds a flag with the Star of Vergina, the emblem of the ancient Greek kingdom of Macedonia and Alexander the Great (AP)

Torrential rain and driving wind kept many protesters away on the final day of the debate. Scores of demonstrators who braved the weather conditions outside parliament chanted "traitors" as politicians voted inside.

Senior European officials said the ratification had "written a new page of our common EU future".

European Commission president Jean-Claude Juncker, the EU's top diplomat and the senior official supervising the bloc's enlargement, said it took political courage, leadership and responsibility on all sides to resolve one of the most entrenched disputes in the region.

Britain's Foreign Office congratulated Greece's parliament for ratifying the agreement, which it described as an historic moment.

"Both countries have demonstrated great courage and the will to work together to affect real change. The deal brings the prospect of increased stability and prosperity to the wider region," foreign secretary Jeremy Hunt said. "We look forward to continued working with two trusted allies in the interests of European security.”

Greece has long argued use of the term Macedonia implied territorial claims on its own northern province of the same name. The issue has been a contentious one for decades, blocking repeated rounds of UN-mediated negotiations and whipping up nationalist and patriotic sentiment in both countries.

Opposition has been particularly fierce in the northern Greek region of Macedonia, which borders the republic that claimed the same name after declaring independence from the former Yugoslavia in 1991. Critics claimed the deal signed away their identity and a cultural heritage dating back to Alexander the Great more than 2,300 years ago.

More than 150 people have been detained for questioning this week following violence at demonstrations against the deal in Athens and two towns in northern Greece. Most were released without charge.

At least two politicians from the governing Syriza party have said their homes were targeted by violent demonstrators, while another was targeted in a firebombing that caused no injuries. Greece's tourism minister, an independent conservative who supports the government, said she had received multiple death threats.