A long time ago, but it was given to us in perpetuity in 1713.
"Under the Treaty of Utrecht in 1713 Gibraltar was ceded to Britain. This treaty stated "the town, castle and fortifications were to be held and enjoyed for ever without any exception or impediment whatsoever." This treaty was renewed again in 1763 by the Treaty of Paris, and in 1783 by the Treaty of Versailles."