Senator Ted Cruz said on Friday that he would vote for Donald J. Trump for president, two months after Mr. Cruz pointedly declined to endorse his former rival in a speech at the Republican National Convention.

“After many months of careful consideration, of prayer and searching my own conscience, I have decided that on Election Day, I will vote for the Republican nominee, Donald Trump,” Mr. Cruz wrote in a statement on Facebook.

For Mr. Cruz, who has fashioned himself as the unbending conscience of modern conservatism, the decision to endorse Mr. Trump is the latest remarkable gamble in a career defined by them, placing him in the corner of an ideologically elastic candidate who savaged Mr. Cruz — and, often, the senator’s family — at every turn during the nominating contest.

But Mr. Trump’s rise in the polls, combined with a handful of political overtures from Mr. Trump and his team in recent days, left Mr. Cruz effectively boxed in, raising the possibility that a narrow Trump defeat could be laid at his feet.