free $R$-algebras: when does $R\langle X\rangle\cong\!R\langle Y\rangle$ $\Rightarrow$ $|X|\!=\!|Y|$ hold? An analogous question regarding free groups can be found here.
INTUITION: A free $R$-algebra on indeterminates $X_1,\ldots,X_n$ is the noncommutative analogue of the polynomial ring $R[X_1,\ldots,X_n]$, which is a free commutative $R$-algebra on $X_1,\ldots,X_n$.
DEFINITIONS: For a commutative ring $R$ with $1_R$ and any set $X\!=\!\{X_i;\:i\!\in\!I\}$, the free (associative) unital $R$-algebra on $X$, denoted $R\langle X\rangle\!=\!R\langle X\,|\,\emptyset\rangle$, is the free $R$-module with basis the free monoid on $X$ of all (noncommutative) words (possibly empty) over $X$. Thus every element of $R\langle X\rangle$ has the form $$\sum r_iX_{j_1}\ldots X_{j_i}$$ and is called a noncommutative polynomial, where $r_i\!\in\!R$ (only finitely many non-zero) and $X_{j_1},\ldots,X_{j_i}\!\in\!X$ and $j_i\!\in\!\mathbb{N}_0$ (when $j_i\!=\!0$, the word $X_{j_1}\ldots X_{j_i}$ is empty, denoted with $1$); furthermore, $X$ is called the alphabet, $X_i$ are called indeterminates or variables or letters or generators, $X_{j_1}\ldots X_{j_i}$ are called monomials or words, and $r_i$ are coefficients. Multiplication is defined as follows: the product of two basis elements (monomials) is their concatenation, i.e.
    $$(X_{i_1}\!\ldots X_{i_m}) \cdot (X_{j_1}\!\ldots X_{j_n}) := X_{i_1}\!\ldots X_{i_m}X_{j_1}\!\ldots X_{j_n};$$
    the product of any two elements (polynomials) just takes into account that $\cdot$ must be $R$-bilinear. For example, for $\alpha,\beta,\gamma,\delta\!\in\!R$ we have $(\alpha X_1X_2^2\!+\!\beta X_2X_3)\cdot(\gamma X_3\!+\!\delta X_2^3X_3X_1)$ $=$ $\alpha\gamma X_1X_2^2X_3+\alpha\delta X_1X_2^5X_3X_1+\beta\gamma X_2X_3^2+\beta\delta X_2X_3X_2^3X_3X_1$. When $|X|\!=\!n\!<\!\infty$, this algebra is denoted $R\langle X_1,\ldots,X_n\rangle$.
In short, for any set $X$, the free unital $R$-algebra on $X$ is $$R\langle X\rangle:=\bigoplus_{w\in X^\ast}Rw$$ with the $R$-bilinear multiplication that is concatenation on monomials/words, where $X^\ast$ is the free monoid on $X$ and $Rw\!=\!\{rw;\:r\!\in\!R\}$ is the formal free module on $w$, i.e. $rw\!=\!r'w\Leftrightarrow r\!=\!r'$. The empty word/monomial of $X^\ast$ is the identity $1$ of $R\langle X\rangle$. The map $i\!:X\!\rightarrow\!R\langle X\rangle$, $X_i\!\mapsto\!X_i$ is called the canonical injection. If we replace $X^\ast$ with $X^+$ (the free semigroup on $X$, i.e. $X^\ast$ without the empty word) in the above construction, we create the free nonunital $R$-algebra on $X$: $R\langle X\rangle^+\!=\!R\langle X\,|\,\emptyset\rangle^+$.
DEFINITIONS: For $S\!\subseteq\!R\langle X\rangle$, an algebra presentation, denoted $R\langle X|S\rangle$, is the $R$-algebra $R\langle X\rangle/\langle\!\langle S\rangle\!\rangle$, where $\langle\!\langle S\rangle\!\rangle$ denotes the algebra ideal, generated by $S$. The notation $\langle X\,|\,p_i\!=\!p'_i;\, i\!\in\!I\rangle$ simply means $\langle X|p_i-p'_i; i\!\in\!I\rangle$. Any $R\langle X|S\rangle$ is finitely generated / finitely related / a finite presentation if $X$ is finite / $S$ is finite / $X$ and $S$ are finite. An arbitrary (unital) $R$-algebra $A$ is finitely generated / finitely related / finitely presented if it has a presentation $R\langle X|S\rangle\!\cong\!A$ that is finitely generated / finitely related / finite. Elements of $S$ are relations or relators. These notions are analogously defined for the free nonunital $R$-algebra on $X$; the presentation is then denoted $R\langle X|S\rangle^+$.
COMMENT: Rings are $\mathbb{Z}$-algebras, so the above construction gives us the free ring on $X$, namely $\mathbb{Z}\langle X\rangle^+$, and free unital ring on $X$, namely $\mathbb{Z}\langle X\rangle$.
PROPOSITION (universal property): Let $i\!:X\!\rightarrow\!R\langle X\rangle$ be the canonical injection. For any unital $R$-algebra $A$ and any map $f\!:X\!\rightarrow\!A$, there exists a unique unital algebra homomorphism $\overline{f}\!:R\langle X\rangle\!\rightarrow\!A$ such that $f\!=\!\overline{f}\!\circ\!i$, namely $\overline{f}(\sum r_iX_{j_1}\ldots X_{j_i})=\sum r_if(X_{j_1})\cdot\ldots\cdot f(X_{j_i})$.

Proof: $\overline{f}$ must be a homomorphism and $f\!=\!\overline{f}\!\circ\!i$, so $\overline{f}(\sum r_iX_{j_1}\!\ldots\!X_{j_i}\!)\!=\!\sum r_if(X_{j_1}\!)\!\cdot\ldots\!\cdot\!f(X_{j_i}\!)$  must be satisfied, hence uniqueness of $\overline{f}$. It is also evident that such $\overline{f}$ is a unital algebra homomorphism. To prove it is well defined, assume $\sum r_iX_{j_1}\ldots X_{j_i}\!=\!0$. By definition, $R\langle X\rangle$ is a free module on $X^\ast$, thus $r_i\!=\!0$. Therefore $\overline{f}(0)\!=\!0$, and $\overline{f}$ is well defined. $\blacksquare$
COMMENT: by the universal property, $R\langle X\rangle$ is the free object on $X$ in the category of unital $R$-algebras.
PROPOSITION: Every unital $R$-algebra $A$ has a presentation, i.e. $\forall A\:\exists X,S\!:\: A\!\cong\!R\langle X|S\rangle$.
Proof: Let $A$ be generated by $X\!\subseteq\!A$ ($X:=A$ suffices) and let $f\!:X\!\hookrightarrow\!A$ be the inclusion. By the universal property, $\exists!$ algebra homomorphism $\overline{f}\!:R\langle X\rangle\!\rightarrow\!A$ that extends $f$. We have $X\!\subseteq\!\mathrm{Im}\,\overline{f}$, so $\overline{f}$ is surjective. Therefore $A\!\cong\!R\langle X\rangle/\mathrm{Ker}\,\overline{f}\!=\!R\langle X| \mathrm{Ker}\,\overline{f}\rangle$. $\blacksquare$
QUESTION: how can I prove the statement $R\langle X\rangle\!\cong\!R\langle Y\rangle\:\Rightarrow\:|X|\!=\!|Y|$ ?
I've thought of using the following theorem from Grillet's Abstract algebra (page 333):

However, this just gives us $|X^\ast|\!=\!|Y^\ast|$. I've also thought of abelianising both algebras, so we get $R[X]\!\cong\!R[Y]$. But I'm not sure how to continue from here.
 A: Assuming that $R$ is commutative, then we can make the following statements.


*

*If $R\langle X\rangle\cong R\langle Y\rangle$ as $R$-algebras, then $\vert X\vert = \vert Y\vert$.

*If $R\langle X\rangle\cong R\langle Y\rangle$ as rings then either $\vert X\vert = \vert Y\vert$ or $\{\vert X\vert,\vert Y\vert\}=\{0,1\}$.


So, if we only assume that they are isomorphic as rings then we still get the desired conclusion with the exception of the counterexample mentioned by Aaron.
Let's start by proving 1. Let $f\colon R\langle X\rangle\to R$ be any $R$-algebra homomorphism and $K={\rm ker}(f)=f^{-1}(0)$. This is a two sided ideal. Also, define $R^{(X)}$ to be the free $R$-module on $X$. Note that the quotient $K/K^2$ is an $R$-algebra. In fact, I'll show that $R^{(X)}\cong K/K^2$. So, if we have an $R$-algebra isomorphism $\theta\colon R\langle Y\rangle\to R\langle X\rangle$ then
$$
R^{(X)}\cong K/K^2\cong\theta^{-1}(K)\big/\theta^{-1}(K)^2={\rm ker}(f\circ\theta)\big/{\rm ker}(f\circ\theta)^2\cong R^{(Y)}.
$$
As noted in the question, free $R$-modules have bases of the same cardinality, so $\vert X\vert=\vert Y\vert$.
To construct an isomorphism from $R^{(X)}$ to $K/K^2$, first note that $f$ takes $X_i\in X$ to some element $x_i\in R$. Then (by universality), there is an isomorphism $u\colon R\langle X\rangle\to R\langle X\rangle$ taking $X_i$ to $X_i-x_i$. By composing with $u$, we can suppose that $f(X_i)=0$. Then, $K$ is the linear combinations of monomials without constant term, and $K^2$ is the linear span of monomials of degree at least 2. So, as an $R$-module, $K=K_1\oplus K^2$ where $K_1$ is the span of the degree 1 monomials, which is isomorphic to $R^{(X)}$. Then, $K/K^2\cong K_1\cong R^{(X)}$.
Comparing with Aaron's answer, this proof is very similar to his except that he works in the dual space. The $R$-derivations on $R\langle X\rangle$ is naturally isomorphic to the space of $R$-module homomorphisms from $K/K^2$ to $R$. So, by what I have just said above,
$$
{\rm Der}_R(R\langle X\rangle,R)\cong {\rm Hom}_R(K/K^2,R)\cong{\rm Hom}_R(R^{(X)},R)\cong R^{\vert X\vert}.
$$By passing to the dual space we pass from the direct sum $R^{(X)}$ to the direct product $R^{\vert X\vert}$ which, in the infinite case loses a bit of information about the cardinality of $X$ (without making set-theoretic assumptions beyond ZFC).
Finally, we can deal with case 2 above where it is only assumed that they are isomorphic as rings. Note that if $\vert X\vert > 1$ then $X_1X_2\not=X_2X_1$, so $R\langle X\rangle$ is not commutative, and it is commutative if $\vert X\vert\le1$.
Let's deal with the $\vert X\vert > 1$ case first. Then $R\langle X\rangle$ is noncommutative with $R$ being its center. $R\langle Y\rangle$ is also noncommutative with center $R$. So, a ring isomorphism $f\colon R\langle X\rangle\to R\langle Y\rangle$ restricts to an isomorphism of the centers of the rings and, hence, restricts to an isomorphism of $R$. By composing with an isomorphism $R\langle Y\rangle\to R\langle Y\rangle$ of $R$ (fixing the $Y_i$), we can reduce to the case that $f$ fixes $R$. So, $f$ is an $R$-algebra homomorphism, and we have reduced to case 1.
Finally, suppose that $\vert X\vert\le1$. Then $R\langle X\rangle$ is commutative, so $R\langle Y\rangle$ is commutative and $\vert Y\vert\le 1$. The only situation where $\vert X\vert\not=\vert Y\vert$ is when $\{\vert X\vert,\vert Y\vert\}$ equals $\{0,1\}$. This situation can occur, as mentioned by Aaron in the comment, so is the only possible exception.

Here's another quite neat way of looking at the proof of 1. If $A$ is an $R$-algebra and $M$ is an $A$-bimodule then a derivation is a homomorphism (as $R$-modules) $D\colon A\to M$ satisfying $D(ab)=(Da)b+a(Db)$. The space of Kähler differentials $\Omega_{A/R}$ is an $A$-bimodule with derivation $d\colon A\to \Omega_{A/R}$ satisfying the following universal property: if $D\colon A\to M$ is any other derivation then there is a unique $A$-bimodule homomorphism $\theta\colon\Omega_{A/R}\to M$ satisfying $D=f\circ d$. The universal property ensures that $d\colon A\to \Omega_{A/R}$ is uniquely defined up to isomorphism. It can be seen that the Kähler differentials $\Omega_{R\langle X\rangle/R}$ is just the free $R\langle X\rangle$ bimodule generated by terms $dX_i$ for $X_i\in X$. Then any $R$-algebra homomorphism $f\colon R\langle X\rangle\to R$ allows $R$ to be viewed as an $R\langle X\rangle$-bimodule, and we can form the (bimodule) tensor product $R\otimes_{R\langle X\rangle}\Omega_{R\langle X\rangle/R}$. By the universal property of $R\langle X\rangle$, all homomorphisms to $R$ are equivalent up to an isomorphism of $R\langle X\rangle$ so, up to isomorphism the tensor product is independent of the choice of $f$. In fact, the homomorphism $R\langle X\rangle\to R\otimes\Omega_{R\langle X\rangle/R}$ takes $X_i$ to $f(X_i)\in R$ so, considered as an $R$-module, the tensor product is just the free module over $R$ generated by terms $dX_i$. So,
$$
R\otimes_{R\langle X\rangle}\Omega_{R\langle X\rangle/R}\cong R^{(X)}
$$
regardless of the choice of $f$. This determines $\vert X\vert$ in terms of the $R$-algebra structure of $R\langle X\rangle$.
We can regard the homomorphism $f$ as a generalized point ${\rm Spec}(R)\to{\rm Spec}(R\langle X\rangle)$ (although these definitions only really hold in the commutative case). The tensor product is then restricting the space of Kähler differentials to the point, which gives a free module generated by terms $dX_i$, just like in the commutative case. This is related to $K/K^2$ above because the homomorphism taking $a\in R\langle X\rangle$ to $(a-f(a)1)+K^2\in K/K^2$ is a derivation, giving a natural homomorphism $\Omega_{R\langle X\rangle/R}\to K/K^2$, and tensoring with $R$ turns it into an isomorphism. Using the universal property of Kähler differentials we also relate to Aaron's method,
$$
\begin{align}
{\rm Der}_R(R\langle X\rangle,R)&\cong{\rm Hom}_{R\langle X\rangle}(\Omega_{R\langle X\rangle/R},R)\cong{\rm Hom}_R(R\otimes\Omega_{R\langle X\rangle/R},R)\\&\cong{\rm Hom}_R(R^{(X)},R)\cong R^{\vert X\vert}.
\end{align}
$$
A: We always have that $R\langle X \rangle \cong R \langle Y \rangle$ implies $|X|=|Y|$, assuming that the map $R\langle X \rangle \cong R \langle Y \rangle$ is a map of $R$-algebras.
Given an $R$-algebra $S$ and an $S$-bimodule $M$, a map $\delta:S\to M$ is an $R$-derivation if it is $R$-linear and $\delta(st)=\delta(s)t+s\delta(t)$.  For example, if $S=R[x]$, then $f\mapsto \frac{df}{dx}$ is an $R$ derivation from $S$ to itself.  We let $\operatorname{Der}_R(S,M)$ denote the collection of all $R$ derivations.  This will be an $R$-module.  Note that we need $R$ to be commutative and to lie in the center of $S$, or we run into some minor technical difficulties.
Let us view $R\langle X \rangle$ as an augmented $R$-algebra, that is, we fix a map of $R$ algebras $\epsilon:R\langle X \rangle\to R$.  In general, such a map is given by selecting $n$ elements $r_1, \ldots r_n\in R$ and taking the algebra map satisfying $x_i\mapsto r_i$.  This allows us to view $R$ as an $R\langle X \rangle$-bimodule.  We wish to consider $N=\operatorname{Der}_R(R\langle X \rangle,R)$.  We assert the following proposition.
Proposition: There is an isomorphism of $R$-modules $\operatorname{Der}_R(R\langle X \rangle,R)\cong R^{|X|}$.
sketch of proof: If $S$ is generated as an $R$-algebra by a collection of generators $\{x_i\}$, then $\delta\in \operatorname{Der}_R(S,M)$ is determined completely by $\delta(x_i)$.  Thus an $R$ derivation $\delta$ of $R\langle X \rangle$ is determined by $\delta(x_i)$ and we have a map $\operatorname{Der}_R(R\langle X \rangle,M)\to M^{|X|}$.  This map is an isomorphism (exercise!).  Now take $M=R$.
With the proposition in hand, using your result that the rank of a free module is well defined, we easily have that $$R\langle X \rangle \cong R\langle Y \rangle \Leftrightarrow R^{|X|}\cong R^{|Y|} \Leftrightarrow |X|=|Y|.$$
N.B. While we needed to choose an augmentation, and our isomorphism  $\operatorname{Der}_R(R\langle X \rangle,R)\cong R^{|X|}$ depended on the augementation, we see that this choice was inconsequential to the final result.  
A: This is just a clarification (at least for me) of Lowther's answer (from what I understand), in case someone beside me needs an additional/easier/pedestrian explanation.
Proposition: $R\langle X\rangle\!\cong\!R\langle Y\rangle\:\Leftrightarrow\:|X|\!=\!|Y|$
Proof: $(\Leftarrow)$: If $f\!:X\rightarrow Y$ is the bijection, then $\overline{f}(\sum r_iX_{j_1}\ldots X_{j_i}):=\sum r_if(X_{j_1})\ldots f(X_{j_i})$ is the isomorphism.
$(\Rightarrow)\!:$ Let $\varphi\!:\!R\langle Y\rangle\!\rightarrow\!R\langle X\rangle$ be the unital algebra isomorphism and via universal property define $f\!:\!R\langle X\rangle\!\rightarrow\!R$ by $X_i\!\mapsto\!0,1\!\mapsto\!1$. Then $K\!:=\!\mathrm{ker}f\!=\!f^{-1}(0)$ consists of linear combinations of monomials without constant term, and $K^2\!=\!\{k_1k_1'\!+\!\ldots\!+\!k_nk_n'; n\!\in\!\mathbb{N}_0,k_i,k_i'\!\in\!K\}$ consists of linear combinations of monomials of degree at least $2$. So, as an $R$-module, $K\!=\!K_1\!\oplus\!K^2$ where $K_1$ is the span of the degree $1$ monomials, which is isomorphic to $R^{(X)}$, the free $R$-module on $X$. Thus $K/K^2\!\cong\! K_1\!\cong\!R^{(X)}$. We wish to change $\varphi$ to an isomorphism $\widetilde{\varphi}\!:\!R\langle Y\rangle\!\rightarrow\!R\langle X\rangle$, such that $f\!\circ\!\widetilde{\varphi}\!:\!R\langle Y\rangle\!\rightarrow\!R$ maps $Y_i\!\mapsto\!0$, $1\!\mapsto\!1$. To achieve this, denote $y_i\!:=\!f\!\circ\!\varphi(Y_i)\!\in\!R$ and via universal property define $\alpha\!:\!R\langle Y\rangle\!\rightarrow\!R\langle Y\rangle$ by $Y_i\!\mapsto\!Y_i\!-\!y_i$. This is an isomorphism with inverse $\alpha^{-1}\!:Y_i\!\mapsto\!Y_i\!+\!y_i$, hence we put $\widetilde{\varphi}\!:=\!\varphi\!\circ\!\alpha$, and indeed $f\!\circ\!\widetilde{\varphi}\!:Y_i\!\mapsto\!Y_i\!-\!y_i\!\mapsto\!y_i\!-\!y_i\!=\!0$. Therefore we can deduce $$R^{(X)} \cong K/K^2 \cong \widetilde{\varphi}^{-1}(K)\big/\widetilde{\varphi}^{-1}(K)^2 = {\rm ker}(f\!\circ\!\widetilde{\varphi})\big/{\rm ker}(f\!\circ\!\widetilde{\varphi})^2 \cong R^{(Y)},$$ which by the property of free modules (proposition 4.12 in the opening question) implies $|X|\!=\!|Y|$.$\blacksquare$
A: Finite Krull dimension of $R$ does not imply that $\dim R[x]=\dim R+1$. We have instead $\dim R+1\le\dim R[x]\le 2\dim R+1$. However, if $R$ is noetherian, then the equality $\dim R[x]=\dim R+1$ holds.
