Ogilvy was MP for Dundee from 1857 to 1874, originally as its sole member, but from 1868 was one of two members representing the town after it became a two-member constituency. He was first elected at the general election of 1857, defeating his future colleague George Armitstead, by 245 votes. In both 1859 and 1865 he was returned unopposed. In the first election for the new two member seat in 1868 he was elected in second place, finishing 77 votes behind Armitstead, but polling over 3,000 votes more than third placed candidate.