{"id":9318,"date":"2021-08-17T13:05:58","date_gmt":"2021-08-17T13:05:58","guid":{"rendered":"https:\/\/linuxways.net\/?p=9318"},"modified":"2021-08-17T13:05:58","modified_gmt":"2021-08-17T13:05:58","slug":"how-to-install-rust-on-linux","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/centos\/how-to-install-rust-on-linux\/","title":{"rendered":"How to Install Rust on Linux"},"content":{"rendered":"<p>Rust (commonly known as Rust-Lang) is a modern, powerful, open-source server-side programming language. It was developed by Mozilla and first launched in 2010. It provides numerous features such as move semantics, zero-cost abstractions, pattern matching, minimal runtime, type inference, threads without data traces, efficient C bindings, etc.<\/p>\n<p>Rust is similar to c++ and runs on a number of platforms. Organizations that rely on Rust in their production servers include CoreOS, Dropbox, and Mozilla.<\/p>\n<p>In this guide, we will walk you through the installation of the Rust language on Linux and write our first program.<\/p>\n<h2><strong>Step 1: Install necessary dependencies<\/strong><\/h2>\n<p>We first need to install the necessary dependencies required for Rust in Linux.<\/p>\n<p>To install the dependencies on Debian-based systems, run the following command:<\/p>\n<pre><strong>$ sudo apt-get install build-essential -y<\/strong><\/pre>\n<p>To install the dependencies on Red Hat-based systems, run the following command:<\/p>\n<pre><strong>$ sudo dnf install cmake gcc -y<\/strong><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"737\" height=\"417\" class=\"wp-image-9319\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-296.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-296.png 737w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-296-300x170.png 300w\" sizes=\"auto, (max-width: 737px) 100vw, 737px\" \/><\/p>\n<h2><strong>Step 2: Installing curl<\/strong><\/h2>\n<p>Moving along, we will install Rust in Linux using curl, a free command-line utility. If you don&#8217;t have curl in your system, run either of the following commands to install.<\/p>\n<p>To install curl on a Ubuntu \/ Debian-based Linux distributions run:<\/p>\n<pre><strong>$ sudo apt install curl -y<\/strong><\/pre>\n<p>To install curl on Red Hat-based Linux distributions such as Rocky Linux, Fedora and CentOS, run:<\/p>\n<pre><strong>$ sudo dnf install curl -y<\/strong><\/pre>\n<h2><strong>Step 3: Install Rust<\/strong><\/h2>\n<p>With curl installed, the next step will be to install rust. Use the curl command to download and execute the installation script.<\/p>\n<pre><strong>$ curl https:\/\/sh.rustup.rs -sSf | sh<\/strong><\/pre>\n<p>The above command is distribution-agnostic and will run on any Linux environment.<\/p>\n<p>When prompted, select \u2018<strong>1<\/strong>\u2019 and hit Enter to proceed with the installation. All the required components for Rust programming language will be downloaded.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"764\" height=\"488\" class=\"wp-image-9320\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-297.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-297.png 764w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-297-300x192.png 300w\" sizes=\"auto, (max-width: 764px) 100vw, 764px\" \/><\/p>\n<p>When the installation is complete, the following output will be displayed.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"771\" height=\"226\" class=\"wp-image-9321\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-298.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-298.png 771w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-298-300x88.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-298-768x225.png 768w\" sizes=\"auto, (max-width: 771px) 100vw, 771px\" \/><\/p>\n<p>Rust will not add the Cargo bin directory (the package management and crate host for rust) to your <strong>$PATH<\/strong> during the installation process, therefore you&#8217;ll have to do it manually. Run the command:<\/p>\n<pre><strong>$ source $HOME\/.cargo\/env<\/strong><\/pre>\n<p>Next, source your user <strong>.profile<\/strong> file so as to make sure it uses the modified $PATH. This ensures your shell functions properly within the Rust environment. Execute the command below:<\/p>\n<pre><strong>$ source ~\/.profile<\/strong><\/pre>\n<p>Finally, let&#8217;s verify the version of rust installed in our machine with the command:<\/p>\n<pre><strong>$ rust --version<\/strong><\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"527\" height=\"61\" class=\"wp-image-9322\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-299.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-299.png 527w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-299-300x35.png 300w\" sizes=\"auto, (max-width: 527px) 100vw, 527px\" \/><\/strong><\/p>\n<p>From the output, the currently installed rust version is 1.54.0<\/p>\n<h2><strong>Step 4: Testing your Installation<\/strong><\/h2>\n<p>We are now ready to run our first program in rust. We will run a simple \u201chello world\u201d script. I will first create a new directory for my rust project.<\/p>\n<pre><strong>$ mkdir ~\/rustproject<\/strong><\/pre>\n<p>Next, navigate into the new directory.<\/p>\n<pre><strong>$ cd rustproject<\/strong><\/pre>\n<p>Let\u2019s now create and open a new rust file with the command:<\/p>\n<pre><strong>$ sudo nano rusttestfile.rs<\/strong><\/pre>\n<p>You should notice that rust programs are saved with the .rs extension.<\/p>\n<p>Copy the following lines of code that print the statement \u201cThis script is running\u201d. Save and close the file.<\/p>\n<pre><strong>fn main(){<\/strong>\r\n\r\n<strong>println!(\u201cThis rust script is running\u201d);<\/strong>\r\n\r\n<strong>}<\/strong><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"753\" height=\"418\" class=\"wp-image-9323\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-300.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-300.png 753w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-300-300x167.png 300w\" sizes=\"auto, (max-width: 753px) 100vw, 753px\" \/><\/p>\n<p>After saving the above file, create a Rust executable for our rusttestfile.rs file with the command:<\/p>\n<pre><strong>$ rustc rusttestfile.rs<\/strong><\/pre>\n<p>Finally, we will run the compiled program with the following command:<\/p>\n<pre><strong>$ .\/rusttestfile<\/strong><\/pre>\n<p>You should see \u201cThis script is running!\u201d printed in the output upon successful execution of the program.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"119\" class=\"wp-image-9324\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-301.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-301.png 640w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-301-300x56.png 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/p>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p>We are now set to write rust programs on Linux.<\/p>","protected":false},"excerpt":{"rendered":"<p>Rust (commonly known as Rust-Lang) is a modern, powerful, open-source server-side programming language. It was developed by Mozilla and first launched in 2010. It provides numerous features such&hellip;<\/p>","protected":false},"author":1,"featured_media":9432,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,4,5,83,165,2],"tags":[484],"class_list":["post-9318","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-centos","category-debian","category-mint","category-opensuse","category-red-hat","category-ubuntu","tag-rust"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/9318","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=9318"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/9318\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/9432"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=9318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=9318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=9318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}