Barbary Wars The Barbary Wars were two wars fought at different times over the same reasons between the United States of America and the Barbary States of North Africa in the late 18th and early 19th centuries . At issue was the Barbary pirates ' demand for tribute from American merchant vessels in the Mediterranean Sea . If ships of a given country failed to pay , pirates would attack the ships and take their goods , and often enslave crewmembers and hold them for ransom . When Thomas Jefferson became president he refused to pay tribute and sent a United States Naval fleet to the Mediterranean and bombarded the various fortified pirate cities and ultimately extracted concessions of fair passage from their rulers . Both the administrations of Thomas Jefferson and James Madison undertook the actions against the Barbary States at different times . Jefferson led the first , from 1801 to 1805 , against pirates ' cities in what are today Libya , Tunisia , and Algeria . Madison directed forces for the second war in 1815 . Barbary corsairs . The Barbary Corsairs , sometimes called Ottoman Corsairs or Berber Pirates , were pirates and privateer s who operated from North Africa , based primarily in the ports of Tunis , Tripoli and Algiers . This area was known in Europe as the Barbary Coast , a term derived from the name of its Berber inhabitants . Their predation extended throughout the Mediterranean , south along West Africa ' s Atlantic seaboard and even South America , and into the North Atlantic as far north as Iceland , but they primarily operated in the western Mediterranean . In addition to seizing ships , they engaged in " Razzia s " , raids on European coastal towns and villages , mainly in Italy , France , Spain , and Portugal , but also in England , Scotland , the Netherlands , Ireland , and as far away as Iceland . The main purpose of their attacks was to capture Christian slaves for the Arab slave market in North Africa and the Middle East . Attacks . The Barbary pirates had long attacked British and other European shipping along the North Coast of Africa . They had been attacking British merchant and passengers ships since the 1600s . The many captives required regular fundraising by families and local church groups , who generally raised the ransoms for individuals . The British became familiar with captivity narratives written by Barbary pirates ' prisoners and some who were sold into Arab slavery before the North American colonies were well established . This was decades before English colonists became subject to captivity by Native Americans and began to write their own narratives . During the American Revolution , the pirates attacked American ships . On December 20 , 1777 , Morocco ' s Sultan Mohammed III declared that the American merchant ships would be under the protection of the sultanate and could thus enjoy safe passage into the Mediterranean and along the coast . The Moroccan - American Treaty of Friendship stands as the U . S . ' s oldest non - broken friendship treaty with a foreign power . In 1787 Morocco had been one of the first nations to recognize the United States . First Barbary War ( 1801 – 1805 ) . The First Barbary War ( 1801 – 1805 ) , also known as the Tripolitan War or the Barbary Coast War , was the first of two wars fought between the United States and the Northwest Africa n Berber Muslim states known collectively as the Barbary States . These were Tripoli and Algiers , which were quasi - independent entities nominally belonging to the Ottoman Empire , and ( briefly ) the independent Sultanate of Morocco . This war began during Thomas Jefferson ' s term when he refused to pay tribute , an amount that was greatly increased when he became president . A US naval fleet was sent on May 13 , 1801 , at the beginning of the war under the command of Commodore Richard Dale . Other notable officers in the fleet included Stephen Decatur , assigned to the frigate USS " Essex " and William Bainbridge in command of the USS " Essex " which was attached to Commodore Richard Dale ' s squadron which also included the " Philadelphia " , the " President " and the " Enterprise " . During this war the " Philadelphia " was blockading Tripoli ' s harbor when she ran aground on an uncharted reef . Under fire from shore batteries and Tripolitan gunboat s , the Captain , William Bainbridge , tried to refloat her by casting off all of her guns and other objects . The ship was eventually captured and the crew taken prisoners and put into slavery . To prevent this powerful war ship from being used by the Barbary pirates the ship was later destroyed by a raiding party led by Stephen Decatur . Second Barbary War ( 1815 ) . The Second Barbary War ( 1815 ) , also known as the Algerine or Algerian War , was the second of two wars fought between the United States and the Ottoman Empire ' s North Africa n regencies of Tripoli , Tunis , and Algeria known collectively as the Barbary states . The war between the Barbary States and the U . S . ended in 1815 ; the international dispute would effectively be ended the following year by the United Kingdom and the Netherlands . The war brought an end to the American practice of paying tribute to the pirate states and helped mark the beginning of the end of piracy in that region , which had been rampant in the days of Ottoman domination ( 16th – 18th centuries ) . Within decades , European powers built ever more sophisticated and expensive ships which the Barbary pirates could not match in numbers or technology . Effect in United States . When the United States military efforts of the early 19th century were successful against the pirates , partisans of the Democratic - Republicans contrasted their presidents ' refusals to buy off the pirates by paying tribute with the failure of the preceding Federalist administration to suppress the piracy . The Federalist Party had adopted the slogan , " Millions for defense , but not one cent for tribute , " but had failed to end the attacks on merchant ships . From 1796 - 1797 French raiders seized more some 316 American merchant ships flying American colors . To counter this ongoing advent three frigates were soon built , the USS " United States " USS " Constitution " and the USS " Constellation " were soon built to answer the call for security . See also : Original six frigates of the United States Navy Later . In the 21st century , the United States again conducted military operation in the North African area , specifically participating in the intervention against the government of Libya , and this operation has sometimes been termed in the media as the continuation of the previous Barbary Wars and given the name " Third Barbary War " .