Is it legal or applicable to arm yachts or other recreational vessels for defense against piracy, thieves, terrorism, etc?
Gun control laws
The moment you enter the 12 nautical miles zone of a country, you need to abide by its weapon laws. Most functioning large-caliber and fully-automatic weapons are not allowed in civilian hands globally. The moment the ship leaves the 12 nautical miles zone, the country law of its flag applies, so unless you happen to start in the US with a duly registered curio/relic deck gun with proper stamps... you'll have a hard time being allowed to have the thing on board in the first place, and entering any other country's water is pretty much violating their gun control laws and gun import laws.
Ship hulls don't support them.
That deck gun there is a type of Bofors 40mm L/60 twin mounting - 40x311mmR. That means, its installation weighs upwards of half a ton, as that's the smallest carriage setup according to Wikipedia. Navweapons helpfully provides gun weights of roundabout half a ton per gun. That puts the minimum weight at about that of a PAK 40, but as Navweapons tells us, a US Mark 1 Twin clocks in at 4.4 to 5.8 tons including guns. All on the one mounting spot.
With the bulk they are, no fiberglass ship hull could support that much load on the gun's small footprint. It needs a steel-hulled superyacht to even bear the deck load of such an installation. If your ship is the size of a coastguard or navy vessel, it probably is such a ship redesigned, a commercial fishing boat (similar specs), a cargo vessel, a large cruise vessel, or built to your specifications from the ground up.