Farquhar was born at Newhall, Aberdeenshire, near Aberdeen in 1774 as the youngest child of Robert Farquhar and Agnes Morrison, his father's second wife. His brother, Arthur, two years his senior, rose to the rank of rear admiral in the Royal Navy, and received a knighthood for his distinguished services during the Napoleonic Wars.
Farquhar joined the East India Company as a cadet at age 17. Shortly after arriving in Madras on 19 June 1791, he was promoted to a low-rank commissioned officer of the Madras Engineers on 22 June 1791. Two years later, on 16 August 1793, he became a lieutenant in the Madras Engineers.
Farquhar was Chief Engineer in the expeditionary force which took Malacca from the Dutch on 18 August 1795. He spoke Malay, and kept a Malaccan-French mistress, Antoinette "Nonio" Clement on 1795 whom was popularly known as the Rajah of Malacca. he had at least six children with her, with one died in infancy. Their eldest daughter Esther Farquhar married Capt. Francis James Bernard who became a prominent part of early Singapore. On 1 January 1803 he was promoted to the rank of full captain.
From 1803 he acted as Resident of Malacca, and was made a full major in Corps on 26 September 1811, before he was officially appointed Resident and Commandant of Malacca in December 1813; a position he held for several years, in charge of both civil and military offices, until the Dutch returned in September 1818. During his tenure here he assisted in missions around the region, including the British invasion of Java led by Governor-General Lord Minto and Sir Stamford Raffles in August 1811.
An engineer, his reputation was established by his long and successful term as Resident of Malacca, of which he was given charge when it passed into British hands from the Dutch. A task given him by the British Government was to demolish all the structures left by the former occupants; he blew up the fortifications with gunpowder but spared some of the other buildings, showing great intelligence.
With his long Malayan experience, and an intimate knowledge of Riau-Lingga politics, Major Farquhar was given the task to help Sir Stamford Raffles find a settlement on Singapore island. He helped negotiate the provisional agreement of 30 January 1819 with the local chieftain Temenggong Abdul Rahman; and the more formal Singapore Treaty of 6 February 1819, which Raffles signed with the Temenggong and His Highness the Sultan Hussein Mohammed Shah, confirming the right for the British to set up a trading post. One that same day, Farquhar, accompanied by a few Malaccan Malays, had to climb up the hill known as Bukit Larangan. He drew up the first gun and set up the post to hoist the Union Jack flag on top of the hill, marking the birth of Singapore as a British settlement.