Excerpted here from Rabbi Avi Grossman:
Concerning the upcoming Israeli elections, you have heard that various rabbanim claim that to vote is forbidden, as one should not have a hand in the establishment of a government that will inevitably violate the Torah. I hear that. Other rabbanim have claimed that voting and participating in the formation of a new Knesset, which will, in turn, lead to the formation of a new government, is a form of the fulfillment of the commandment to appoint a king. They quote Deuteronomy 17:15, “You shall surely set a king over yourself.”
I believe that the truth is somewhere in between. The verse is indeed a positive and binding commandment, and one that, God willing, is to lead us to fulfill the subsequent commandments of wiping out Amalek (i.e. securing national security even if it entails preemptive action) and building the Temple (the physical manifestation of a national sense of unity and purpose).
The full verse also says that the king should be one “whom the Lord your God shall choose.” Who is such a candidate? Well, only a prophet could tell you, but we also know that the Davidians (the worthy ones among them) have already been chosen and anointed. Therefore, it is proper that one vote for the party that intends to put a qualified and worthy Davidian in charge. Voting for anyone else is not a fulfillment of the commandment. I am surprised that I have not seen this point made elsewhere, but it is reflected in our ancient history. After Samuel anointed Saul, the Israelite majority did not accept him as king. It was only after he led a successful military campaign against the Ammonites that the people accepted him. David as well was not immediately accepted as king, and neither was Solomon, even though both had received divine approval. As with most of history, there is nothing really new.
Nahmanides, as you may recall, wrote this concerning the Hasmonean dynasty. They should have been replaced by Davidians, and Nahmanides would be right to similarly criticize our generation. Maimonides, to be fair, would countenance non-Davidians ruling Israel, if they also received a temporary prophetic endorsement, so unless a prophet arises and endorses the Jewish Home or whoever, they do not have my vote.
Found here.