How should I install rustfmt



This question raised during setting up Travis CI for my little project.

I used recommendation from rustfmt repo

language: rust
- rustup toolchain install nightly
- rustup component add --toolchain nightly rustfmt-preview
- which rustfmt || cargo install --force rustfmt-nightly
- cargo +nightly fmt --all -- --write-mode=diff
- cargo build
- cargo test

I have noticed that which rustfmt was always succeed.
I thought that it could be because of caching, but after clearing all caches nothing changed - rustfmt was still “preinstalled”.

Travis CI support team said to me

Although I’m not super familiar with Rust and its ecosystem, I had a look and it seems that rustfmt comes already pre-installed in the location you have referenced i.e. /home/travis/.cargo/bin/rustfmt.

Looks like I am not familiar either :slight_smile:

I run empty Docker container locally, installed Rust nightly into it and saw that indeed rustfmt was already there at ~/.cargo/bin/rustfmt with the latest version

rustfmt -V
0.3.8-nightly (346238f 2018-02-04)

My question is how should I properly install rustfmt?
Should I ever use cargo install rustfmt-nightly?

Sometimes I occasionally got a warning

warning: tool rustfmt is already installed, remove it from ~/.cargo/bin, then run rustup update to have rustup manage this tool.

What does it mean exactly and why I got it?

Looks like on Travis CI I could use rustfmt which comes with the language since Rust is installed each time a new build is running.
And on my local machine I could cargo install [-f] rustfmt-nightly to install and update it without updating Rust.
But I am not sure here.

Could you please help me to figure it out?


I believe those instructions are slightly out of date: you don’t need nightly anymore. Here what’s working for me:


yes, thank you. I also think it should be enough.
Does it give the latest rustfmt always?
Ans how do you install rustfmt locally?


Nope, it’ll have the rustfmt corresponding to the Rust release. If you want to track the latest rustfmt, you’ll have to use nightly, but that requires more tinkering (IIRC, not every nightly includes a working rustfmt).

Ans how do you install rustfmt locally?

# (on stable toolchain)
rustup component add rustfmt-preview


Actually, I don’t have any project on stable Rust :slight_smile:
I will try to stick with rustup component approach. If there would be any problems I could switch to cargo install rustfmt-nightly, right?


Hm, I think for nightly just rustup component add rustfmt-preview should work as well, if you are on the nightly that actually has rustfmt.


The issue with cargo install is that you need to put the exact right versions of rustfmt with the exact right versions of the compiler. rustup knows how to do this. cargo install does not. So you can, but you have no idea if it’s actually going to work or not.


Thanks for pointing that out. I have not faced with it for now.
I didn’t know that rustfmt could installed via rustup component and always used cargo install.
Now I will try rustup component.


Has rustfmt calmed down in its changing the recommended format? If not, by having Travis track the latest rustfmt, your build can spuriously break.

Also, in case its helpful, I’m starting to centralize CI best practices for crates. Not sure if anything in the rustfmt section would be useful.


Can’t answer about pace of changing recommended format since I have been using it not a long time.
I have took brief look at crate-ci - it’s nice project, thanks!


It’s fairly calm, but there are even minor changes from what’s on stable today and what will be in a few releases. This is why the release announcement emphasized that this is rustfmt-preview, and that it’s still pre-1.0.

That being said, while it is a spurious failure, it’s also one that’s really easy to fix.