Thousands of people have staged protests across Poland after the country’s president, Andrzej Duda, signed into law a measure effectively letting the government choose the next supreme court chief.

The European Union, human rights groups and opposition parties in Poland said the legislation and other changes pushed by the ruling Law and Justice (PiS) party undermined judicial independence and democracy.

Crowds gathered outside the presidential palace in Warsaw chanting “shame”. Many held candles and pens, referring to Duda’s readiness to sign. They shouted “break the pen” and “you will go to prison”.

Similar protests took place in more than two dozen cities and towns across Poland.

The PiS party said an overhaul was needed to make the courts more efficient and eradicate the influence of Poland’s communist past.

“Without [judiciary] reforms, we cannot rebuild the Polish state so that it serves its citizens,” said Jarosław Kaczyński, the leader of the Eurosceptic party which combines left-leaning economics with nationalist politics.

Earlier this month, 22 supreme court judges were forced into early retirement but the chief judge Małgorzata Gersdorf has refused to go, saying her constitutional term does not expire until 2020.

The latest amendment, which was adopted by the upper house of parliament earlier this week, has been designed to make it easier to name the new supreme court head.

Since PiS won power in 2015, dozens of judges have been effectively dismissed from the constitutional tribunal, the national judiciary council, which decides judicial appointments and now the supreme court.

New appointments have used procedures that give parliament, where the PiS has a majority, greater say over the courts and the government more control over judges.

The European commission has been running an unprecedented rule of law investigation and has opened several separate legal cases against Poland, the largest former communist EU state, including some over the supreme court.

