DUBLIN – Debt-struck Ireland on Sunday formally appealed for a massive EU-IMF loan to stem the flight of capital from its banks, joining Greece in a step unthinkable only a few years ago when Ireland was the economic envy of Europe.

European Union finance ministers quickly agreed to the bailout, saying it “is warranted to safeguard financial stability in the EU and euro area.”

Irish Finance Minister Brian Lenihan spent much of the night talking to other finance chiefs in the 16-nation eurozone about the complex terms of the emergency aid package taking shape.

Lenihan said Ireland needed less than $140 billion to use as a credit line for its state-backed banks, which are losing deposits and struggling to borrow funds on open markets.

The money will come from the EU’s executive commission and a financial backstop set up by eurozone nations earlier this year. There may also be additional bilateral loans from countries outside the eurozone.

Ireland has been brought to the brink of bankruptcy by its fateful 2008 decision to insure its banks against all losses — a bill that is swelling beyond $69 billion and driving Ireland’s deficit into uncharted territory.

This country of 4.5 million now faces at least four more years of deep budget cuts and tax hikes totaling at least $20.5 billion just to get its deficit, which set a European record of 32 percent of GDP this year, back to the eurozone’s 3 percent limit by 2014.

The European Central Bank and other eurozone members had been pressing behind the scenes for Ireland — long struggling to come to grips with the true scale of its banking losses — to accept a bailout that would reassure investors the country won’t, and can’t, go bankrupt. Those fears have been driving up the already inflated borrowing costs of several eurozone members, particularly Portugal and Spain, on bond markets.

Still, the rapid pace of Sunday’s humiliating Irish U-turn surprised many analysts. More than 30 banking experts from the International Monetary Fund, ECB and European Commission had arrived in Dublin only three days before to begin poring over the books and projections of the government, treasury and banks.

But Lenihan said it was now painfully clear that Ireland couldn’t go it alone any longer, and its plans for recovery would require a major shot of “financial firepower” immediately.

Lenihan said Ireland was asking eurozone and IMF donors to loan money to a “contingency” fund from which Irish banks could borrow. He said the funds would “not necessarily” be used. He emphasized that the government’s own operations are fully funded through mid-2011.

“Not all the money will go in (to the banks) at all. It’s a standby fund,” Lenihan told Irish state broadcasters RTE.

Ireland’s move comes just six months after the EU and IMF organized a $150 billion bailout of Greece and declared a $1.05 trillion safety net for any other eurozone members facing the risk of imminent loan defaults. It demonstrates that creating the three-layered fund didn’t, by itself, reassure global investors that it would be safe, or smart, to keep lending to the eurozone’s weakest members.

The United Kingdom, the country with the most exposure to Ireland’s banks, reiterated Sunday it was ready to help fund Ireland’s loan facility. The U.K. Treasury said in a statement that it would be “closely involved in discussions on the scale and type of assistance.”