Martin Braithwaite cancelled out a first-half strike from Ousmane Dembele to leave the two teams level in the 57th minute, but Luis Suarez and Lionel Messi both netted for the home side in the final 20 minutes.
The result has seen Barcelona once again open up a five-point lead over second-placed Atletico Madrid at the top of the table.
Thomas Vermaelen and Carles Alena both came in for rare starts, while there was a spot in the Leganes attack for Braithwaite, who scored the only goal of the match against Real Madrid in the Copa del Rey last week.
Dembele's searing pace created the first half-chance of the match in the ninth minute, but Philippe Coutinho saw his attempt from inside the Leganes box deflected over the crossbar.
The visitors were showing plenty of attacking intent, however, with Braithwaite and Youssef En-Nesyri receiving good support in the final third from the wide positions.
Barcelona's next half-chance arrived in the 24th minute when Luis Suarez found space inside the box following a driven cross from Jordi Alba, but his effort was always too high.
Barcelona did make the breakthrough in the 32nd minute, however, when Dembele's low effort from an Alba cross clipped the inside of the post before dropping into the back of the net.
It was far from a vintage first-half performance from the Spanish champions, but Dembele's clever finish saw them lead at the interval as they moved to within 45 minutes of yet another three points.
Coutinho had Barcelona's first chance of the second period after meeting a clever flick from Dembele, but the Brazilian's effort was always too high.
The hosts continued to dominate the ball as the half developed, but Leganes shocked Camp Nou in the 57th minute when they equalised through on-loan Middlesbrough forward Braithwaite.
Braithwaite scored the winner against Real Madrid in the Copa del Rey last week and levelled against Barcelona when he converted a low cross from En-Nesyri, who had been afforded too much space in a dangerous position.
Dembele flashed a low attempt wide of the post moments later as Barcelona looked for a second, but Leganes were so dangerous down the other end and Oscar tested Marc-Andre ter Stegen with a free kick moments after Sergi Roberto had hacked Braithwaite to the deck.
Valverde made a double change in the 64th minute with Messi and Rakitic replacing Alena and Arthur, who struggled to make their mark on the night.
Barcelona were then forced into a third change in the 69th minute when Dembele limped off the field to be replaced by Malcom, who has been strongly linked with a move away from the club.
The home side re-took the lead in the 71st minute when Luis Suarez converted from close range after Leganes goalkeeper Ivan Cuellar had made a wonderful save to keep out a strike from Messi.
VAR had a look at the incident while Cuellar was on the deck receiving treatment and after a short period, the goal was given.
Cuellar made a smart save to prevent Luis Suarez from making it 3-1 in the 79th minute, but Barcelona did have their third in stoppage time when Messi combined with Alba before finding the top corner with a wonderful strike.
BARCELONA (4-3-3): Ter Stegen; Roberto, Pique, Vermaelen, Alba; Alena (Messi 64'), Arthur (Rakitic 64'), Busquets; Coutinho, Suarez, Dembele (Malcom 69')
LEGANES (3-4-1-2): Cuellar; Tarin, Omeruo, Siovas; Nyom, Vesga (Eraso 86'), Recio, Silva; Oscar (El Zhar 86'); Braithwaite (Santos 78'), En-Nesyri body check tags ::