<-- Back to The Cleavr Slice

09 April 2024

blog

Bun Runtime - Introduction (Part 1)

Bun is a JavaScript runtime built to serve the modern JS ecosystem. Aside from being a JavaScript runtime, Bun is a bundler, test runner, and NodeJS-compatible package manager.

While NodeJS remains a stable and popular choice as a runtime for JavaScript and TypeScript apps, Bun prioritizes speed and developer experience.

Without further delay, let's jump in and install Bun so that we can explore more of what it has to offer.

I’ll be using the Ubuntu 22.04 server from Hetzner throughout this blog. If you’re on a different platform please visit Bun’s installation documentation.

To install Bun run the following command on your terminal:

curl -fsSL https://bun.sh/install | bash

Bun Installation

As you can see in the screenshot, I successfully installed Bun. And the download path is in ~/.bun/bin/bun.

Since Bun is installed as a root user, other system users may be unable to use Bun.

Bun Command Not Found

As you can see in the screenshot above, I was able to run the bun –version command while the root user was running the command. As soon as I switched the user to cleavr, which is a non-root user, I was unable to run the same command.

To resolve this issue, you can copy the Bun installation to the /usr/local/bin/ directory. The /usr directory stores read-only files that can be shared among system users.

cp /root/.bun/bin/bun /usr/local/bin/bun

Bun Fix Permission Issue

Once you’ve successfully installed Bun, you can start building your projects using Bun. To scaffold an empty project, run the bun init command.

Bun Init

The bun init command asks you to provide answers to a few prompts, you can provide the input or just press enter to accept the default answer for each prompt.

It creates the required files to scaffold a minimal Bun project such as package.json, index.ts file, etc. At the end it also runs bun install to install the required dependencies.

You can now run bun run index.ts to execute a source file.

Bun Run

In this blog, we’ve installed the Bun runtime, scaffolded, and ran a new project using Bun runtime. I hope this blog helps you with setting up and running your projects using Bun.

In the next part of this blog, we’ll set up a Nuxt application with Bun.

Take control of your servers and deployments.Without breaking a sweat.

Sign up for a 5-day free trial of Cleavr Pro. No credit card required until you decide to subscribe.

Sign up for free