Peter Girnus

View Original

Rust MacOS - How To Install Using Homebrew

Installing Rust on MacOS, including M1, is easy using Homebrew. In this tutorial, I will show you how to install Rust from a Mac terminal without an installer by using the Homebrew package manager to download rustup and execute the rustup-init script, which we can run to install and configure the entire Rust toolchain and its dependencies.

Install Homebrew on MacOS

If you already have Homebrew installed, you can simply skip this step and move directly to installing Rustup. If not, first install Homebrew using the following command.

See this content in the original post

This will take you through the Homebrew installation process and allow you to use the Brew Package Manager to install rustup.

Install Rust on MacOS Steps

The process of installing Rust on MacOS is straightforward. Once Homebrew is installed, we can download the rustup installation script rustup-init, which will handle the installation and configuration of the entire Rust toolchain, including rustc, cargo, and rustup.

Rust Installation: rustup with Homebrew

Once we can access the Homebrew package manager, we can run the following command from the terminal to download and install Rust programming language using rustup.

See this content in the original post

This command will download and install Rustup using the Homebrew brew command. This will download and install rustup and install the entire Rust toolchain.

Rust Installation: Toolchain Installation with rustup-init on MacOS

Next, we can run rustup-init, which will install Rust and the Rust Toolchain, which includes the following:

  • rustc - the Rust compiler

  • cargo - the Rust package manager

  • rustup - the Rust toolchain manager

You can just run the following command in the terminal.

See this content in the original post

This will download and install the entire Rust lang

Rust Installation: Refresh MacOS Path Variable

Once installation is complete, we will have access to the Rust toolchain from our command line later. First, we need to refresh our $PATH variable. To refresh the $PATH variable on MacOS, we can run the source ~/.zshenv command.  

See this content in the original post

Rust Installation: Verifying Rust Toolchain Installation

Once our environment variable is set, we can access the Rust compiler from the command line with the rustc command. The rustc binary is the Rust compiler, and you can check its version with the rustc --version command.

See this content in the original post

Once we have confirmed that our terminal $PATH variable has access to the rustc binary we can test by the compiler with a simple example.

Rust Programming on Mac: Hello World Example

To test the Rust compiler and the ability to compile and run Rust programs, we can write a simple "hello world" example, which will output "Hello World!" to the terminal. First, add the main function to a rust file, such as main.rs.

See this content in the original post

Next, we can compile this function using rustc.

See this content in the original post

After we have compiled our Rust program on our Mac using the Rust compiler, we can run the compiled binary.

See this content in the original post

Running this Rust program will output "Hello World!" to standard out.

Conclusion

In conclusion, installing Rust on MacOS, even on the latest M1 chip, is a simple process thanks to Homebrew. By following the steps outlined in this tutorial, you can bypass the traditional installer and leverage the power of Homebrew package manager to install the rustup installer which you can use to install and configure Rust directly from your Mac terminal.

See this social icon list in the original post