McNamara died at 5:30 a.m. at his home, his wife Diana told The Associated Press. She said he had been in failing health for some time.
McNamara was fundamentally associated with the Vietnam War, "McNamara's war," the country's most disastrous foreign venture, the only American war to end in abject withdrawal rather than victory.
Known as a policymaker with a fixation for statistical analysis, McNamara was recruited to run the Pentagon by President John F. Kennedy in 1961 from the presidency of the Ford Motor Co. - where he and a group of colleagues had been known as the "whiz kids." He stayed in the defense post for seven years, longer than anyone since the job's creation in 1947.
His association with Vietnam became intensely personal. Even his son, as a Stanford University student, protested against the war while his father was running it. At Harvard, McNamara once had to flee a student mob through underground utility tunnels. Critics mocked McNamara mercilessly; they made much of the fact that his middle name was "Strange."
After leaving the Pentagon on the verge of a nervous breakdown, McNamara became president of the World Bank and devoted evangelical energies to the belief that improving life in rural communities in developing countries was a more promising path to peace than the buildup of arms and armies.
A private person, McNamara for many years declined to write his memoirs, to lay out his view of the war and his side in his quarrels with his generals. In the early 1990s he began to open up. He told Time magazine in 1991 that he did not think the bombing of North Vietnam - the biggest bombing campaign in history up to that time - would work but he went along with it "because we had to try to prove it would not work, number one, and (because) other people thought it would work."
Finally, in 1993, after the Cold War ended, he undertook to write his memoirs because some of the lessons of Vietnam were applicable to the post-Cold War period "odd as though it may seem."
"In Retrospect: The Tragedy and Lessons of Vietnam" appeared in 1995. McNamara disclosed that by 1967 he had deep misgivings about Vietnam - by then he had lost faith in America's capacity to prevail over a guerrilla insurgency that had driven the French from the same jungled countryside.
Despite those doubts, he had continued to express public confidence that the application of enough American firepower would cause the Communists to make peace. In that period, the number of U.S. casualties - dead, missing and wounded - went from 7,466 to over 100,000.
"We of the Kennedy and Johnson administrations acted according to what we thought were the principles and traditions of our country. But we were wrong. We were terribly wrong," McNamara, then 78, told The Associated Press in an interview ahead of the book's release.
The best-selling mea culpa renewed the national debate about the war and prompted bitter criticism against its author. "Where was he when we needed him?" a Boston Globe editorial asked. A New York Times editorial referred to McNamara as offering the war's dead only a "prime-time apology and stale tears, three decades late."
McNamara wrote that he and others had not asked the five most basic questions: "Was it true that the fall of South Vietnam would trigger the fall of all Southeast Asia? Would that constitute a grave threat to the West's security? What kind of war - conventional or guerrilla - might develop? Could we win it with U.S. troops fighting alongside the South Vietnamese? Should we not know the answers to all these questions before deciding whether to commit troops?
He discussed similar themes in the 2003 documentary "The Fog of War: Eleven Lessons from the Life of Robert S. McNamara." With the U.S. in the first year of the war in Iraq, it became a popular and timely art-house attraction and won the Oscar for best documentary feature.
The Iraq war, with its similarities to Vietnam, at times brought up McNamara's name, in many cases in comparison with another unpopular defense secretary, Donald H. Rumsfeld. McNamara was among former secretaries of defense and state who met twice with President Bush in 2006 to discuss Iraq war policies.
In the Kennedy administration, McNamara was a key figure in both the disastrous Bay of Pigs invasion of April 1961 and the Cuban missile crisis 18 months later. The crisis was the closest the world came to a nuclear confrontation between the Soviet Union and the United States.
McNamara served as the World Bank president for 12 years. He tripled its loans to developing countries and changed its emphasis from grandiose industrial projects to rural development.
After retiring in 1981, he championed the causes of nuclear disarmament and aid by the richest nation for the world's poorest. He became a global elder statesman.
McNamara's trademarks were his rimless glasses and slicked down hair and his reliance on quantitative analysis to reach conclusions, calmly promulgated in a husky voice.
He was born June 9, 1916, in San Francisco, son of the sales manager for a wholesale shoe company. At the University of California at Berkeley, he majored in mathematics, economics and philosophy.
As a professor at the Harvard Business School when World War II started, he helped train Army Air Corps officers in cost-effective statistical control. In 1943, he was commissioned an Army officer and joined a team of young officers who developed a new field of statistical control of supplies.
McNamara and his colleagues sold themselves to the Ford organization as a package and revitalized the company. The group became known as the "whiz kids" and McNamara was named the first Ford president who was not a descendant of Henry Ford.
A month later, the newly elected Kennedy invited McNamara, a registered Republican, him to join his Cabinet. Taking the $25,000-a-year job cost McNamara $3 million in profit from Ford stocks and options.
As defense chief, McNamara reshaped America's armed forces for "flexible response" and away from the nuclear "massive retaliation" doctrine espoused by former Secretary of State John Foster Dulles. He asserted civilian control of the Pentagon and applied cost-accounting techniques and computerized systems analysis to defense spending.
Early on, Kennedy regarded South Vietnam as an area threatened by Communist aggression and a providing ground for his new emphasis on counterinsurgency forces. A believer in the domino theory - that countries could fall to communism like a row of dominoes - Kennedy dispatched U.S. "advisers" to bolster the Saigon government. Their numbers surpassed 16,000 by the time of his assassination.
Following Kennedy's assassination, President Lyndon Johnson retained McNamara as "the best in the lot" of Kennedy Cabinet members and the man to keep Vietnam from falling to the Communists.
When U.S. naval vessels were allegedly attacked off the North Vietnamese coast in 1964, McNamara lobbied Congress to pass the Tonkin Gulf Resolution, which Johnson used as the equivalent of a congressional declaration of war.
McNamara visited Vietnam - the first of many trips - and returned predicting that American intervention would enable the South Vietnamese, despite internal feuds, to stand by themselves "by the end of 1965."
That was an early forerunner of a seemingly endless string of official "light at the end of the tunnel" predictions of American success. Each was followed by more warfare, more American troops, more American casualties, more American bombing, more North Vietnamese infiltration - and more predictions of an early end to America's commitment.