How to Install Carbon Lang on Ubuntu 22.04

How to install carbon language on Ubuntu 22.04

Make sure you have basic tools

sudo apt update && sudo apt -y install build-essential git curl gdb make cmake ninja-build meson

Install Go

curl -4 -L -O --url 'https://go.dev/dl/go1.18.4.linux-amd64.tar.gz'
# wget https://go.dev/dl/go1.18.4.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf go1.18.4.linux-amd64.tar.gz 
export PATH=$PATH:/usr/local/go/bin:$HOME/go/bin #or add to your .bashrc then source it

Install Baselisk

go install github.com/bazelbuild/bazelisk@latest

Install clang-14 and define as default

sudo apt install -y llvm-14 clang-14
sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-14 100
sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-14 100

Install dependencies and execute bazelisk to install compile carbon

sudo apt install libunwind-14-dev zlib1g-dev libllvm14 libc++-14-dev
export CC=clang
export CXX=clang++
bazelisk run //explorer -- ./explorer/testdata/print/format_only.carbon

Exemple hello world


// hello_world.carbon
package Hello api;
fn Main -> i32 {
    var s: auto = "Hello World";
    Print(s);
    return 0;
}

Execute it

bazelisk run //explorer -- hello_world.carbon