In the 19th century, Vidzeme and Courland was covered with vast forests of pine trees. Ainaži's strategic position on the sea and proximity to lumber made it a perfect place for shipbuilding. In 1864 Krišjānis Valdemārs sponsored the first naval school in Livonia (today Latvia), training young Estonian and Latvian farmers to become ship captains for free. The school stood for 50 years until it was destroyed in World War I. With the opening of the school and shipbuilding industry, Ainaži grew for the rest of the 19th century. From 1857 to 1913, over 50 seaworthy vessels were built in the town, and in 1902 a working port and railway station opened. By World War I, Ainaži was the fourth-largest port in all of Latvia and chief in Vidzeme, overtaking neighboring Salacgrīva. The town also had its own windmills, fish-processing plant, and brick kiln.