Your question does not have one jurisdiction. It has as many as there are maritime jurisdictions , multiplied by the number of nations which register ships.
That said, in each case, YES civilian ships are generally allowed to defend themselves against pirate attacks, especially in international waters.
As for getting “a license” – see your lawyer, who is an expert both in your flag state’s maritime laws, and in the every jurisdiction you plan to sail an armed ship into. Also talk to their coast guard, navy, harbormasters, etc.
One of the main issues tends to be that as soon as you arm your civilian ship, navies and coast guards start to look at your ships as less like a civilian and more like an armed ship of the state whose flag you fly.
Another main issue, is that the port authorities and coast guards may tend to suspect you of gun-running, and take action to investigate and prosecute you under their laws.
But yes, of course the answer to “has anyone done that?” is of course, yes, they have, but the results vary.