如何在RHEL Linux服务器安装Cargo?rustup安装后命令未找到怎么办?
Hey there! Let's work through this problem together. The -bash: cargo: command not found error you're seeing is almost always due to your shell not having the Cargo binary directory in its PATH environment variable—even if the rustup installer completed successfully. Let's first fix that, then cover alternative ways to install Cargo on RHEL.
First: Fix the Existing rustup Installation
When you ran the curl https://sh.rustup.rs -sSf | sh command, rustup installs Cargo (and Rust) to ~/.cargo/bin, but your shell might not have picked up this path yet. Here's how to fix it:
- Temporary fix (for your current session): Run this command to reload the environment variables set by rustup:
After this, try runningsource $HOME/.cargo/envcargo --version—it should work now. - Permanent fix (for all future sessions): Add the Cargo path to your shell configuration file. For bash, edit
~/.bashrcor~/.bash_profileand add this line at the end:
Then reload the config withexport PATH="$HOME/.cargo/bin:$PATH"source ~/.bashrc(orsource ~/.bash_profiledepending on which file you edited).
Alternative Installation Methods for RHEL
If you'd prefer not to use rustup, here are a few other reliable ways to get Cargo on your RHEL server:
1. Use RHEL's Official AppStream Repository (Recommended for System-Wide Installs)
RHEL 8 and later include a rust-toolset module in the AppStream repository, which includes Cargo. This installs a stable, supported version system-wide:
- First, enable the rust-toolset module:
sudo dnf module enable rust-toolset - Then install the toolset:
Since this installs binaries to system paths likesudo dnf install rust-toolset/usr/bin, you won't need to manually configurePATH—just runcargo --versionright away to verify. Note that the version here might not be the absolute latest, but it's tested and supported for RHEL.
2. Manual Precompiled Binary Install
If you want the latest version but don't want to use rustup, you can download the official precompiled binaries:
- Download the latest stable Rust/Cargo tarball for x86_64 Linux:
curl -O https://static.rust-lang.org/dist/rust-stable-x86_64-unknown-linux-gnu.tar.gz - Extract it to a directory of your choice (e.g.,
/opt/rust):sudo tar -xzf rust-stable-x86_64-unknown-linux-gnu.tar.gz -C /opt - Add the binary directory to your
PATH(temporarily or permanently, like we did earlier):export PATH="/opt/rust-stable-x86_64-unknown-linux-gnu/bin:$PATH" - Verify with
cargo --version.
3. Compile from Source (Not Recommended for Most Users)
Compiling Rust and Cargo from source is possible but time-consuming, and only makes sense if you need a custom build. Here's the quick version:
- Install build dependencies first:
sudo dnf install gcc make curl git - Clone the Rust repository and switch to the stable branch:
git clone https://github.com/rust-lang/rust.git cd rust git checkout stable - Run the installation script:
./x.py install - Once done, add the build's
bindirectory to yourPATH(it'll be in./build/x86_64-unknown-linux-gnu/stage1/binby default).
Final Check
No matter which method you choose, always run cargo --version after setup to confirm everything's working as expected.
内容的提问来源于stack exchange,提问作者Joe




