The legendary Hồng Bàng Dynasty of the Hùng kings is considered by many Vietnamese as the first Vietnamese state, known as Văn Lang. In 257 BCE, the last Hùng king lost to Thục Phán, who consolidated the Lạc Việt tribes with his Âu Việt tribes, forming Âu Lạc and proclaiming himself An Dương Vương. In 207 BCE, a Chinese general named Zhao Tuo defeated An Dương Vương and consolidated Âu Lạc into Nanyue. In 111 BCE, the Chinese Han Dynasty consolidated Nanyue into their empire.

For the next thousand years, Vietnam was mostly under Chinese rule. Early independence movements such as those of the Trưng Sisters and of Lady Triệu were only briefly successful. It was independent as Vạn Xuân under the Anterior Ly Dynasty between 544 and 602. By the early 10th century, Vietnam had gained autonomy, but not independence, under the Khúc family.
In 938 CE, a Vietnamese lord named Ngô Quyền defeated Chinese forces at the Bạch Đằng River and regained independence after 10 centuries under Chinese control.[9] Renamed as Đại Việt (Great Viet), the nation went through a golden era during the Lý and Trần Dynasties. During the rule of the Trần Dynasty, Đại Việt repelled three Mongol invasions. Buddhism flourished and became the state religion.
Following the brief Hồ Dynasty, Vietnamese independence was momentarily interrupted by the Chinese Ming Dynasty, but was restored by Lê Lợi, the founder of the Lê Dynasty. Vietnam reached its zenith in the Lê Dynasty of the 15th century, especially during the reign of Emperor Lê Thánh Tông (1460–1497). Between the 11th and 18th centuries, the Vietnamese expanded southward in a process known as nam tiến (southward expansion).[11] They eventually conquered the kingdom of Champa and part of the Khmer Empire.

Towards the end of the Lê Dynasty, civil strife engulfed much of Vietnam. First, the Chinese-supported Mạc Dynasty challenged the Lê Dynasty's power. After the Mạc Dynasty was defeated, the Lê Dynasty was reinstalled, but with no actual power. Power was divided between the Trịnh Lords in the North and the Nguyễn Lords in the South, who engaged in a civil war for more than four decades. During this time, the Nguyễn expanded southern Vietnam into the Mekong Delta, annexing the Champa in the central highlands and the Khmer land in the Mekong.
The civil war ended when the Tây Sơn brothers defeated both and established their new dynasty. However, their rule did not last long and they were defeated by the remnants of the Nguyễn Lords led by Nguyen Anh with the help of the French. Nguyen Anh unified Vietnam, and established the Nguyễn Dynasty, ruling under the name Gia Long.