That's what I did, at least to Jaca, and then I suppose you could go up to Somport by local bus and then walk back again.
Still, if you want to start out walking from Somport, you could take the train to Canfranc. Renfe goes from Barcelona to Zaragossa in an hour and a half, and you could then take a local train (3h45min) from Zaragossa to Canfranc through Jaca. Somport is just up the hill from Canfranc. There is likely a Barcelona-Jaca bus, but I couldn't find it.