You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何在RHEL Linux服务器安装Cargo?rustup安装后命令未找到怎么办?

Fixing "cargo: command not found" on RHEL + Alternative Installation Methods

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:
    source $HOME/.cargo/env
    
    After this, try running cargo --version—it should work now.
  • Permanent fix (for all future sessions): Add the Cargo path to your shell configuration file. For bash, edit ~/.bashrc or ~/.bash_profile and add this line at the end:
    export PATH="$HOME/.cargo/bin:$PATH"
    
    Then reload the config with source ~/.bashrc (or source ~/.bash_profile depending 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:

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:
    sudo dnf install rust-toolset
    
    Since this installs binaries to system paths like /usr/bin, you won't need to manually configure PATH—just run cargo --version right 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.

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 bin directory to your PATH (it'll be in ./build/x86_64-unknown-linux-gnu/stage1/bin by 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

火山引擎 最新活动