Barca were frustrated by opponents who defended well but the introduction of fit-again forward Neymar for the final half hour gave them an extra attacking edge.
It was Lionel Messi, though, who opened up the Villarreal rearguard with a cross along the goalline that was turned in by Ramirez to lift Barca to six points from their opening two games.
"I am happy on a personal level but above all for the work of the team," Ramirez told reporters. "Thanks to Leo I could score the winning goal. Messi and Neymar are fantastic players although this is the work of all of us and we are all happy."
Real Madrid, without the rested Cristiano Ronaldo, take on Real Sociedad in San Sebastian later on Sunday. Champions Atletico Madrid beat Eibar 2-1 on Saturday.
Luis Enrique played the promising Munir El Haddadi, who scored last weekend, ahead of Neymar in Barcelona's starting line-up.
With Andres Iniesta sidelined by a knee injury, Rafinha played in midfield rather than veteran Xavi who is set to have a fringe role in the team this season.
Barca had the majority of the ball but struggled to find space going forward against a well-organised Villarreal side and it was only following Neymar's entry that the chances began to build up.
Messi hit the post with a free-kick from the right of the area in the first half but Barca were also lucky when Jeremy Mathieu turned a cross onto an upright.
Tomas Pina and Messi again hit the woodwork for either side midway through the second half before Neymar found Messi, who looked up and picked out Sandro to score and give the Catalan side the three points.