If $a^3 =a$ for all $a$ in a ring $R$, then $R$ is commutative.
Let $R$ be a ring, where $a^{3} = a$ for all $a\in R$. Prove that $R$ must be a commutative ring.
To begin with
$$ 2x=(2x)^3 =8 x^3=8x \ . $$
Therefore $6x=0 \ \ \forall x$.
Also
$$ (x+y)=(x+y)^3=x^3+x^2 y + xyx +y x^2 + x y^2 +yxy+ y^2 x + y^3 $$ and
$$ (x-y)=(x-y)^3=x^3-x^2 y - xyx -y x^2 + x y^2 +yxy+ y^2 x -y^3 $$
Adding we get
$$ 2(x^2 y +xyx+yx^2)=0 $$
Multiply the last relation by $x$ on the left and right to get
$$ 2(xy+x^2yx+xyx^2)=0 \qquad 2(x^2yx+xyx^2+yx)=0 \ . $$
Subtracting the last two relations we have
$$ 2(xy-yx)=0 \ . $$
We then show that $3( x+x^2)=0 \ \ \forall x$. You get this from
$$ x+x^2=(x+x^2)^3=x^3+3 x^4+3 x^5+x^6=4(x+x^2) \ . $$
In particular
$$ 3 (x+y +(x+y)^2) =3( x+x^2+ y+ y^2+ xy+yx)=0 \, $$
we end-up with $3(xy+yx)=0$. But since $6xy=0$, we have $3(xy-yx)=0$. Then subtract $2(xy-yx)=0$ to get $xy-yx=0$.
$\rm(1)\quad ab=0\: \Rightarrow\: ba = 0\ \ via\ \ ba = (ba)^3 = b\, ab\, ab\, a = 0$
$\rm(2)\quad c^2 = c\: \Rightarrow\: c\: $ is central $\ $ [which means that $\rm\ \color{#C00}{xc = cx}\ $ for all $\rm\:x$]
$\!\rm\begin{eqnarray}\rm Proof\!:\ \ c(x-cx) &=&\rm 0\:\Rightarrow\: (x-cx)c = 0\ \ by\ (1),\ \ so\ \ \color{#C00}{xc} = cxc\\ \rm (x-xc)c &=&\rm 0\:\Rightarrow\: c(x-xc) = 0\ \ by\ (1),\ \ so\ \ \color{#C00}{cx} = cxc\end{eqnarray}$
$\rm(3)\quad x^2\:$ central via $\rm\:c = x^2\:$ in $(2)$
$\rm(4)\quad c^2 = 2c\:\Rightarrow\: c\:$ central. $\ $ Proof: $\rm\:c = c^3 = 2c^2\:$ central by $(3)$
$\rm(5)\quad x+x^2\:$ central via $\rm\:c = x + x^2\:$ in $(4)$
$\rm(6)\quad x = (x+x^2)-x^2\:$ central via $(3),(5)\,$ by centrals closed under subtraction.$\ $ QED
Re-posting it from here. Note that $R$ is not necessarily unital.
Some general facts:
We call a ring $R$, J-ring (Jacobson ring), if for any $x \in R$ there is a natural number $n(x) >1$ s.t. $x^{n(x)}=x$. (In fact, Jacobson has proven that any J-ring is commutative, for the proof you may take a look at Non-commutative Rings written by Herestein)
Lemma 1: If $R$ be a J-ring, then $N(R)= \{0 \}$ where $N(R)$ denotes the nilradical of $R$.
Proof: Let $0\not= x\in N(R)$. Then there is a smallest natural number greater $1$ s.t. $x^m=0$. Since $R$ is a J-ring, there is an $n>1$ s.t. $x^n=x$. Let $m=nq+r$ where $0 \leq r <n$. Therefore,
$$x^m=x^{nq+r}=(x^n)^qx^r=x^qx^r=x^{q+r}=0$$
However, $q+r<m$, which is a contradiction, since $m$ was chosen to be the smallest number satisfying $x^m=0$.
Lemma 2: Suppose that in a ring $R$, $N(R)= \{0 \}$, then any idempotent element $a$ i.e. $a^2=a$, lies in the center $Z(R)$.
Proof: Suppose that $x \in R$. Then
$$(axa-ax)^2=(axa-ax)(axa-ax)=axaaxa-axaxa-axaax+axax=axaxa-axaxa-axax+axax=0.$$
Since $N(R)= \{0 \}$, then we have $axa-ax=0 \rightarrow axa=ax$. With the same approach and by considering $(axa-xa)^2$ we will obtain $axa=xa$. Hence, $ax=xa$ and since $x$ was an arbitrary element of $R$ then $a \in Z(R)$.
Lemma 3: In a J-ring $R$, we have $x^{n(x)-1} \in Z(R).$
Proof: $(x^{n(x)-1})^2=x^{2n(x)-2}=x^{n(x)}x^{n(x)-2}=xx^{n(x)-2}=x^{n(x)-1}$. Thus $x^{n(x)-1}$ is an idempotent element of $R$ and by Lemma 1 & 2. we get the result.
In particular, in your question, $n=n(x)=3$ and $x^2 \in Z(R),$ for any $x \in R.$ Moreover
$$xy=(xy)^3=xyxyxy=x(yx)^2y=(yx)^2xy=yxyx^2y=yx^3y^2=yxy^2=y^3x=yx.$$
Exercise: The same question with $x^4=x$ for any $x \in R.$
This is not the best proof, but let me add this one because it hasn't been mentioned yet. The proof rests on a single non-commutative polynomial identity (whose verification can be done quickly with a computer algebra system).
Define $f(x,y):=(x+y)^3-x^3-y^3 \in \mathbb{Z} \langle x,y \rangle$. Then we have
$$f\bigl(x,y+(x \cdot y-y \cdot x)\bigr) - f(x,y) - f\bigl(x,(x \cdot y-y \cdot x)\bigr) - (x \cdot f(x,y) - f(x,y) \cdot x)$$ $$ = -x^3 \cdot y + y \cdot x^3$$ Thus, if $R$ is a ring with $a^3=a$ for all $a \in R$, we have $f(a,b)=0$ for $a,b \in R$, and hence also $-a^3 \cdot b + b \cdot a^3=0$ resp. $ab=ba$.