How to Set Up Carbon Programming Language on Ubuntu 22.04

In this tutorial, we will learn how to set up Carbon programming language on Ubuntu 22.04. Carbon is a general-purpose programming language. First introduced by Google engineer Chandler Carruth at the CppNorth conference. Carbon fixes some shortcomings of C++ and Rust languages, the Carbon project is available on GitHub.

Follow the steps to install the Carbon programming language.

1. Install Homebrew

Homebrew is a software package manager that helps to install software on macOS and Linux.

To install build-essentials run the following commands.

sudo apt install build-essential git
sudo apt-get install libz-dev

Next, we can install Homebrew using the command :

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Add brew to PATH :

eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"

Verify brew installation using the command :

brew doctor
Brew doctor on Ubuntu

2. Install Clang/LLVM

Install the Clang/LLVM using the brew. It will take some time to install.

brew install llvm

Export the path using the below command :

export PATH="$(brew --prefix llvm)/bin:${PATH}"

Add the following lines at the bottom of the .bashrc file, so next time we can run the brew, bazel command in the terminal.

eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
export PATH="$(brew --prefix llvm)/bin:${PATH}"

The bazel is an open-source build and test tool developed by Google. We will use bazel to build the carbon programs.

3. Clone Carbon code

First clone the Carbon code from GitHub, run the following command :

git clone https://github.com/carbon-language/carbon-lang

Enter into the carbon-lang directory.

cd carbon-lang
Clone carbon from Github

4. Build and run a hello world program

To run the format_only.carbon hello world program run the following command:

bazel run //explorer -- ./explorer/testdata/print/format_only.carbon
package ExplorerTest api;

fn Main() -> i32 {
  var s: auto = "Hello world!";
  Print(s);
  return 0;
}

The first time it will take some time to build, “Hello world” program output as shown below :

The folder(carbon-lang/explorer/testdata) has carbon sample programs.

Conclusion

Hope you are able to compile and run the Carbon program in Ubuntu 22.04. You can also read 16 New Features of Ubuntu 22.04 LTS.