A Republican bill to provide permanent resident visas for foreigners who graduate from American universities with advanced degrees in science and technology failed to pass the House on Thursday, a setback for technology companies that had strongly supported it.

Republican leaders called the vote under a fast-track procedure that limits debate but also requires a two-thirds majority to pass. The final tally was 257 to 158, with all but a few Republicans joined by 30 Democrats in voting yes, well short of passage.

The bill, sponsored by Representative Lamar Smith, Republican of Texas, chairman of the House Judiciary Committee, would have eliminated an annual lottery and instead allocated 55,000 visas for legal permanent residency, known as green cards, each year to foreigners who have completed master’s and doctoral degrees from American universities in the STEM fields: science, technology, engineering and mathematics. The lottery now distributes the same number of green cards to foreigners from countries with low rates of immigration to the United States.

While Congressional Republicans have taken a hard line on illegal immigration, they said they wanted to show before the November elections that they were ready to pass a measure to fix a widely acknowledged flaw in the legal immigration system.