How to Install Rust Installer on Mac?

How to install rust on MacOS is a task that can be performed using the terminal window. Things have been made easy in MacOS when it comes to installation of any tool, any coding language (in this example, rust), any DevOps tool or any app(s). 

Prerequisites to Install Rust in MacOS

  • Terminal (command line) 
  • Curl support in Terminal 
  • Internet Connectivity
  • Basic understanding of syntax (of commands)

How to Install Rust (MacOS)

Instructions to be followed for getting Rust up and running on your Mac.

  1. Get curl using the terminal window:

NOTE: Please ignore this step if you already have curl support.

  • Open the spotlight search using COMMAND+SPACEBAR.
  • Type the keyword – terminal and press ENTER or RETURN.
  • The terminal window shall come up.
  • Enter command/text : curl -V
  • ENTER. You shall see the below.
TinaS-MacBook-Air-2:~ tinasi$ curl -V
curl 7.64.1 (x86_64-apple-darwin20.0) libcurl/7.64.1 
(SecureTransport) LibreSSL/2.8.3 zlib/1.2.11 nghttp2/1.41.0
Release-Date: 2019-03-27
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps
pop3 pop3s rtsp smb smbs smtp smtps telnet tftp 
Features: AsynchDNS GSS-API HTTP2 HTTPS-proxy IPv6 Kerberos Largefile 
libz MultiSSL NTLM NTLM_WB SPNEGO SSL UnixSockets
TinaS-MacBook-Air-2:~ tinasi$
  1. Now, you shall be connected to the internet with curl. The below text (command) helps the user to connect to Rust website. ENTER after typing the text.
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs |
sh
  1. The process of installation is initiated with the command executed.
TinaS-MacBook-Air-2:~ tinasi$ curl --proto '=https'
--tlsv1.2 -sSf https://sh.rustup.rs
| shinfo: downloading installer

Welcome to Rust!

This will download and install the official compiler for the Rust 
programming language, and its package manager, Cargo.

Rustup metadata and toolchains will be installed into the Rustuphome directory, located at:

  /Users/tinasi/.rustup

This can be modified with the RUSTUP_HOME environment variable.
The Cargo home directory located at:

  /Users/tinasi/.cargo

This can be modified with the CARGO_HOME environment variable.
The cargo, rustc, rustup and other commands will be added toCargo's bin directory, located at:

 /Users/tinasi/.cargo/bin

This path will then be added to your PATH environment variable bymodifying the profile files located at:

 /Users/tinasi/.profile  /Users/tinasi/.zshenv

You can uninstall at any time with rustup self uninstall andthese changes will be reverted.

Current installation options:

 default host triple: x86_64-apple-darwin   
  default toolchain: stable (default)          
     profile: default 
 modify PATH variable: yes

1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
>
  1. Choose the “1) Proceed with installation (default)” option by typing 1. The system starts unpacking installer.
>1

info: profile set to 'default'
info: default host triple is x86_64-apple-darwin
info: syncing channel updates for 'stable-x86_64-apple-darwin'
info: latest update on 2020-12-31, rust version 1.49.0 (e1884a8e3 2020-12-29)
info: downloading component 'cargo'info: downloading component 'clippy'
info: downloading component 'rust-docs'
info: downloading component 'rust-std' 21.1 MiB /  21.1 MiB (100 %)  13.6 MiB/s in  1s ETA:  0s
info: downloading component 'rustc' 50.8 MiB /  50.8 MiB (100 %)  12.6 MiB/s in  4s ETA:  0s
info: downloading component 'rustfmt'
info: installing component 'cargo'
info: using up to 500.0 MiB of RAM to unpack components
info: installing component 'clippy'
info: installing component 'rust-docs' 13.8 MiB /  13.8 MiB (100 %)   4.3 MiB/s in  3s ETA:  0s
info: installing component 'rust-std' 21.1 MiB /  21.1 MiB (100 %)   8.5 MiB/s in  2s ETA:  0s
info: installing component 'rustc' 50.8 MiB /  50.8 MiB (100 %)   9.4 MiB/s in  5s ETA:  0s
info: installing component 'rustfmt'
info: default toolchain set to 'stable-x86_64-apple-darwin'
  stable-x86_64-apple-darwin installed - rustc 1.49.0 (e1884a8e3 2020-12-29)

Rust is installed now. Great!

To get started you need Cargo's bin directory ($HOME/.cargo/bin) in your path environment variable. Next time you log in this will be done automatically.

To configure your current shell, run:
source $HOME/.cargo/env
TinaS-MacBook-Air-2:~ tinasi$ curl --proto '=https' --tlsv1.2 -sSf
https://sh.rustup.rs | sh
  1. The success message – Rust is installed now. Great! Shall be shown.
  2. To verify Rust is installed successfully, use the below command.
TinaS-MacBook-Air-2:~tinasi$ rustc --version
-bash: rustc: command not found
  1. The “command not found” error shall be shown as output.
  2. Before executing the above command, use or type the below first (written in blue). This is to set a path variable.
TinaS-MacBook-Air-2:~ tinasi$ source $HOME/.cargo/env
TinaS-MacBook-Air-2:~ tinasi$ 
  1. Now, verify whether the rust installation is successful or not. Write the text (in blue) below to check the version.

NOTE: “rustc” stands for rust compiler for executing or running any file in Rust.

TinaS-MacBook-Air-2:~ tinasii$ rustc --version
rustc 1.49.0 (e1884a8e3 2020-12-29)
TinaS-MacBook-Air-2:~ tinasi$ 
  1. The command has returned the output with the version of Rust installed on your Mac.

How to Write the First Program in Rust

Rust is similar to C providing more features than C using the libraries available. Here, let us write the first program in Rust to understand how it works.

  1. Open your terminal window.
  2. Write the command below to create your rust file using vim editor.
vim myfirstrustprogram.rs
  1. Paste code as below.
fn main() {    println!("Happy to Write My First Rust Program!");}
~
~
~
~
~
~
~
~
~
~
~
~
~
~
3L, 69C
  1. Hit ESCAPE.
  2. Type :wq command to save and exit the file.
  3. Next, type the command. ENTER
rustc myfirstrustprogram.rs
  1. You will see the compile success and now to view the output, enter the below command. You will see the output.
$ ./myfirstrustprogram
Happy to Write My First Rust Program!
  1. Yo! You have written your first code in Rust.

Uninstall Rust when not needed

The way we have seen installation of Rust is so quick and simple. To simply remove Rust from your system, a couple of actions are required.. Let us see how we uninstall Rust.

  1. Come back to the terminal window. (Spotlight search – COMMAND+SPACEBAR, type terminal)
  2. Type the command written in Blue. ENTER.
TinaS-MacBook-Air-2:~ tinasi$ rustup self uninstall

Thanks for hacking in Rust!
This will uninstall all Rust toolchains and data, and remove$HOME/.cargo/bin from your PATH environment variable.
Continue? (y/N) 
  1. To continue, say y.
Continue? (y/N) y
info: removing rustup home
info: removing cargo home
info: removing rustup binaries
info: rustup is uninstalledTinaS-MacBook-Air-2:~ tinasi$ 
  1. You shall see the message for successful uninstallation.
  2. There you go! You are done.

A quick tutorial to kickstart with Rust is explained in this article. You may want to get into details of it. Surely visit https://doc.rust-lang.org/rust-by-example/hello.html. Happy coding in Rust!

You may also like: