The Libertarian Party, the nation's third-largest political party, gave its 1984 presidential nomination today to David Bergland, an outspoken antigovernment activist who is considered a hard-liner even by the Libertarians' stern standards.

Bergland, who squeaked to a majority on the fourth ballot at the party's national convention here, pledged to hold true to the party's unyielding platform, which calls for legalizing all drugs, bringing all U.S. troops and weapons home from overseas, and abolishing the CIA, the IRS, the national parks, and the public schools.

The nominee, a lawyer from Costa Mesa, Calif., laughed off a suggestion that the party should moderate its stand to win support of voters aligned with the major parties. "What reasonable person," he said, "could take the Republicans or Democrats seriously?"

In choosing Bergland, 48, the Libertarian delegates chose ideological purity and turned away from the more pragmatic approach of their 1980 presidential nominee, Ed Clark.

Clark, who was on the ballot in all 50 states and the District of Columbia, won 920,000 votes, just over 1 percent of the total.

Today, the party's pragmatic wing backed the candidacy of Earl Ravenal, a political science professor at Georgetown University.

Ravenal and Bergland traded the lead through three extended ballots. The distinction between them was clearly drawn when the voting was interrupted to let both address the delegates. Bergland spoke of "the ideal of liberty" and the "ugliness" of government; Ravenal talked about the need to make the party "relevant" to the "broad sweep of the American people."

Then, on the fourth roll call, Bergland won a bare majority--270 delegates--without a single vote to spare. Ravenal had 242 votes.

Reflecting the party's undercurrent of anarchy, 24 delegates cast their ballots for "None of the Above."

Things were not supposed to be so close.

Until a week ago, a Florida radio personality named Gene Burns was the unchallenged choice for the presidential nomination. But Burns dropped out abruptly when the party could not guarantee reimbursement of his personal campaign spending. That brought on the hard-fought contest today.

This convention had one striking distinction from Republican and Democratic gatherings. Throughout the session it was clear that the party was still agonizing over whether to hold to principle or broaden its appeal. Bergland left no doubt that he places greater emphasis on policy than on practical politics.

Although Clark, the 1980 nominee, supported his fellow Californian, Bergland was critical of some of Clark's positions in the 1980 campaign.

To hold some elderly voters, for example, Clark had refined the Libertarians' opposition to Social Security into a 40-year phaseout plan.

But Bergland said today that the Social Security system should be "terminated forthwith," with all contributors getting a refund of taxes paid in.

"Government should not be in the charity business," Bergland said.

Bergland complained that moderation could only water down what all Libertarians consider their greatest asset: the consistency of their antigovernment principles.

Libertarians share some beliefs with both major parties, though to an extreme.

Libertarians oppose domestic federal spending and government regulation of business; they also oppose defense spending and involvement in foreign countries. They go farther than either party in opposing legal limits on abortion and the use of narcotics.

The consistency of the party's view is crystalized in a popular two-line slogan. "U.S. OUT of South America," reads the first line. "U.S. OUT of North America," reads the second.

Bergland went into specifics after his nomination. He said he would close all government schools and hospitals and let private organizations run them. He would sell all parks because "government shouldn't be in the recreation business."

He would let the private sector build all roads, bridges and dams. He would continue public police forces but shut down municipal fire departments.

Despite the attention they received in 1980, the Libertarians may have a harder road this time. Some states have made it more difficult for third parties to get on the ballot.

In 1980, moreover, the party gave itself a financial lift by nominating a multimillionaire for vice president. He gave the ticket $2 million of the $3 million it spent.

But for 1984 there seems to be no millionaire for the ticket. And naturally, the Libertarians would never accept federal matching funds.

Still, the delegates left the convention in a fighting spirit epitomized by Richard Siano, a township supervisor in New Jersey who is one of the Libertarians' few elected officials.

"Ask not what you can do for your country," Siano said in his speech. "Ask not what your country can do for you.

"Just go out and do whatever the hell you can do for yourself."