GDANSK, Poland — An estimated 20,000 Poles gave a tumultuous and emotional welcome Friday to British Prime Minister Margaret Thatcher, who met Lech Walesa at the doomed Lenin Shipyard and said she felt "the spirit of Poland."

Crowds shouted slogans against communism as Thatcher--the first Western leader to meet Walesa at the shipyard--came to this Baltic port city after telling the government it must negotiate with the banned trade union Solidarity before it can receive long-term Western aid.

Thatcher later returned to Warsaw for a final round of talks with the Polish leader, Gen. Wojciech Jaruzelski, at the end of a three-day visit. She then boarded a Royal Air Force jet for London.

Her emotional reception in Gdansk was heightened by her tough attitude regarding Jaruzelski and by the government's announcement this week that it will close the Lenin shipyard, the birthplace of Solidarity, on Dec. 1 as part of an economic austerity program.

"I came to see and to have a long talk with Mr. Walesa . . . . I knew that I had to come and feel the spirit of Poland for myself," said Thatcher, referring to Gdansk, the heartland for the long struggle by workers for a free trade union.

At a farewell news conference, Thatcher said if the government recognizes Solidarity, Britain will respond. "We shall not be found wanting when Poland makes the progress toward freedom and democracy its people clearly seek," she said.

Government spokesman Jerzy Urban said officials were not convinced. "We are skeptical of such attempts to define conditions, and of such promises," he said.

Polish leaders have told Thatcher the country is crippled by a $39-billion foreign debt, but she has said Poland must show evidence of real economic and political reform before it gets more aid.

Shipyard workers in hard hats climbed on surrounding rooftops and crowds burst into deafening chants of "Solidarity! Solidarity!" as Thatcher laid a wreath with Walesa at a towering monument to scores of workers slain here in 1970 price riots.

As security police trucks ringed surrounding streets, Thatcher had lunch with Walesa and other union leaders at St. Brygida's Parish Church near the shipyard.

Crowds poured into the streets around the church and shipyard and shouted, "Viva Thatcher!" to the conservative British leader, who has curbed the power of trade unions in her own country.

At the shipyard and monument, a section of the crowd yelled "Down with communism!" and "Out with Rakowski!"--the new Polish premier, Mieczyslaw F. Rakowski, who announced the closing of the yard Monday.

Walesa said security police tried to stop the crowds by clogging streets with police trucks and stopping Solidarity leaders from getting to the monument until shortly before Thatcher arrived.

"See how many came despite the efforts to stop them," said Walesa. "They have filtered through somehow. We will not be defeated."

At a service in St. Brygida's after lunch, the high red-bricked church and its nearby streets echoed with chants of Solidarity supporters.

With Walesa standing beside her in the sacristy, his hand raised in a V-for-victory sign, against a background of red, white and gold Solidarity banners, Thatcher was visibly moved as the congregation broke into a traditional national hymn, "God Watches Over Poland."

"Thank you for coming out and giving me and everything I believe in such a warm welcome," said Thatcher, her eyes glistening.

Before the meeting with Walesa, Thatcher visited the Westerplatte monument on the bay of Gdansk, scene of the first shots of World War II, accompanied by Jaruzelski. The solemn wreath-laying ceremony recalled the Polish-British wartime alliance.