The city walls date from the 1120s when, under threat of attack from the Almohads of the south, the ruling Almoravid sultan, Ali Ben Youssef decided to encircle his garrison town with fortifications. The walls he had built were up to 9 m (30 ft) high and formed a circuit of 10 km (6 miles), punctuated by some 200 towers and 20 gates.