Installing Rust.

Brian KingBrian King
6 min read

TL;DR.

This post provides a step-by-step guide on how to install Rust, a powerful systems programming language, within a Linux Container (LXC). It covers the creation of the container, adding a user account to the container, fixing the home directory issue, and installing and managing Rust using the rustup tool. It also includes instructions on how to uninstall Rustup. The article emphasizes that, despite the simplicity of the installation process, Rust is an incredibly potent language.

Attributions:

https://www.rust-lang.org/↗.

An Introduction.

Rust is a compiled, (mostly) memory-safe, systems programming language.

The purpose of this post is to present a process for installing Rust.

The Big Picture.

Although this is a very short post, the possibilities presented by a Rust installation are wide, and deep, and numerous. I can study the Rust language from the official Rust Book and Cargo Book, or I can learn from a more interactive appliance. In my opinion, a great way to learn how to use a powerful language like Rust is to build a training app for Rust programming using the Rust language. That's meta. (Here's a real-world comparison to this idea: A lathe is the only machine that can build itself.)

Prerequisites.

Updating my Base System.

  • In a terminal, I update my base system:
sudo apt clean && \
sudo apt update && \
sudo apt dist-upgrade -y && \
sudo apt --fix-broken install && \
sudo apt autoclean && \
sudo apt autoremove -y

What is an LXC and LXD?

An LXC (LinuX Container) is an isolated, OS-level virtualization which, for efficiency, uses the Linux kernel of the host system. An LXC is a virtual environment where system processes within the LXC container can not affect other containers, or the host system, without specifically running certain commands.

The LXD (LinuX Daemon) is the container manager that is used to create, and manage, LXCs (LinuX Containers). It is a background service that can automatically start LXCs when the host system boots, or stop any container from starting at all.

Creating a Container.

  • I create an LXC (LinuX Container) called (Rust):
lxc launch ubuntu:22.04 Rust
  • I bash into the container:
lxc exec Rust -- bash
  • I update and upgrade the container:
sudo apt clean && \
sudo apt update && \
sudo apt dist-upgrade -y && \
sudo apt autoclean && \
sudo apt --fix-broken install && \
sudo apt autoremove -y

Adding a User Account to the Container.

  • From within the container, I create a new user:
adduser yt
  • I add the new user to the 'sudo' group:
usermod -aG sudo yt

NOTE: usermod let's me (-a)ppend the sudo (-G)roup to the yt account.

  • I reboot the container:
sudo reboot

The next step is to fix the home directory problem.

Setting the LXC Home Directory.

  • From the terminal, I log in to the container with the yt account:
lxc exec Scrapings -- su yt

NOTE: At the moment, the home directory is /root. This section will address the issue by changing the home directory to ~.

  • I use the Nano text editor to open the .bashrc file:
sudo nano ~/.bashrc
  • I copy the following, add it (CTRL + SHIFT + V) to the bottom of the .bashrc file, save (CTRL +S) the changes, and exit (CTRL + X) Nano:
cd ~

Hardening the Container.

  • From within the container, I use the Nano text editor to open the sshd_config file:
sudo nano /etc/ssh/sshd_config
  • I copy the following, add it (CTRL + SHIFT + V) to the bottom of the sshd_config file, save (CTRL +S) the changes, and exit (CTRL + X) Nano:
PasswordAuthentication no
PermitRootLogin no
Protocol 2
Port = 2424
  • I restart the SSH service:
sudo systemctl restart ssh
  • I reboot the container:
sudo reboot

NOTE: Within the container, I can also install (or enable) UFW, Fail2Ban, and CrowdSec.

What is Rust?

Rust is a systems programming language that empowers everyone to build reliable and efficient software. It is blazingly fast and memory-efficient: with no runtime or garbage collector, it can power performance-critical services and run on embedded devices. Rust is focused on performance, memory safety, and safe concurrency.

Installing Rustup.

NOTE: The rustup tool is used to install, and manage, Rust.

  • From the terminal, I log in to the container with the 'yt' account:
lxc exec Rust -- su yt
  • I use curl to download, and run, the rustup script:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
  • I reboot the container:
sudo reboot

Updating Rustup.

  • From the terminal, I log in to the container with the 'yt' account:
lxc exec Rust -- su yt
  • I use rustup to update rustup:
rustup update

Testing the Rust Compiler Installation.

  • I check the Rust Compiler version:
rustc --version

Uninstalling Rustup.

  • I use rustup to uninstall itself:
rustup self uninstall

The Results.

Rust is a powerful, memory-safe, systems programming language. This guide provided step-by-step procedures on how to install Rust within an LXC. I also covered how to add user accounts, fix the home directory issue, and manage Rust using the rustup tool. The installation process was very simple but does not detract from Rust being an incredibly powerful language.

In Conclusion.

I have now installed Rust, a powerful, memory-safe, systems programming language. This guide described the steps I used to install this language.

Rust opens up a world of possibilities, and it's not as complex to install as you might think.

In this guide, I've installed Rust in an LXC (LinuX Container). I also covered how to add user accounts and fix home directory issue within the LXC, and how to manage Rust using the rustup tool. I also included the uninstall process for Rustup. The installation process is simple but doesn't detract from Rust being a powerful tool.

Whether you're looking to explore Rust for your next project or simply want to add another tool to your programming kit, this guide has got you covered.

Ready to dive into the world of Rust? Have you used Rust before? If so, what projects have you worked on? If not, are you considering giving it a try?

Let's start a conversation below!

Until next time: Be safe, be kind, be awesome.

#Rust #Programming #SystemProgramming #RustInstallation #LXC #LinuxContainer #Rustup #MemorySafety #SystemsProgramming #DeveloperTools

0
Subscribe to my newsletter

Read articles from Brian King directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Brian King
Brian King

Thank you for reading this post. My name is Brian and I'm a developer from New Zealand. I've been interested in computers since the early 1990s. My first language was QBASIC. (Things have changed since the days of MS-DOS.) I am the managing director of a one-man startup called Digital Core (NZ) Limited. I have accepted the "12 Startups in 12 Months" challenge so that DigitalCore will have income-generating products by April 2024. This blog will follow the "12 Startups" project during its design, development, and deployment, cover the Agile principles and the DevOps philosophy that is used by the "12 Startups" project, and delve into the world of AI, machine learning, deep learning, prompt engineering, and large language models. I hope you enjoyed this post and, if you did, I encourage you to explore some others I've written. And remember: The best technologies bring people together.