Created
November 3, 2024 15:29
-
-
Save therealparmesh/fbfad72113abfb3803fe4718d18db2b9 to your computer and use it in GitHub Desktop.
Bun local install script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -euo pipefail | |
# Check for required arguments | |
if [[ $# -ne 2 ]]; then | |
echo "Usage: $0 <bun-version-tag> <install-directory>" | |
echo "Example: $0 bun-v0.6.9 /path/to/install/dir" | |
exit 1 | |
fi | |
BUN_VERSION=$1 | |
INSTALL_DIR=$2 | |
# Ensure unzip is installed | |
command -v unzip >/dev/null || | |
{ echo "Error: unzip is required to install Bun"; exit 1; } | |
# Detect platform | |
platform=$(uname -ms) | |
case "$platform" in | |
"Darwin x86_64") | |
target="darwin-x64" | |
;; | |
"Darwin arm64") | |
target="darwin-aarch64" | |
;; | |
"Linux aarch64" | "Linux arm64") | |
target="linux-aarch64" | |
;; | |
"Linux x86_64") | |
target="linux-x64" | |
;; | |
"MINGW64"* | "Windows NT"*) | |
target="windows-x64" | |
;; | |
*) | |
echo "Error: Unsupported platform $platform" | |
exit 1 | |
;; | |
esac | |
# Check for AVX2 support on x64 platforms | |
if [[ $target == "darwin-x64" || $target == "linux-x64" ]]; then | |
if [[ $(uname) == "Darwin" ]]; then | |
if ! sysctl -a | grep -q 'machdep.cpu.features.*AVX2'; then | |
target="${target}-baseline" | |
fi | |
elif [[ $(uname) == "Linux" ]]; then | |
if ! grep -q avx2 /proc/cpuinfo; then | |
target="${target}-baseline" | |
fi | |
fi | |
fi | |
# Set download URL | |
GITHUB="${GITHUB:-https://github.com}" | |
github_repo="$GITHUB/oven-sh/bun" | |
bun_uri="$github_repo/releases/download/$BUN_VERSION/bun-$target.zip" | |
# Set installation directories | |
BIN_DIR="$INSTALL_DIR/bin" | |
EXE="$BIN_DIR/bun" | |
# Create installation directory | |
mkdir -p "$BIN_DIR" || | |
{ echo "Error: Failed to create install directory \"$BIN_DIR\""; exit 1; } | |
# Download Bun | |
echo "Downloading Bun from $bun_uri..." | |
curl --fail --location --progress-bar --output "$EXE.zip" "$bun_uri" || | |
{ echo "Error: Failed to download Bun from \"$bun_uri\""; exit 1; } | |
# Extract Bun | |
unzip -oqd "$BIN_DIR" "$EXE.zip" || | |
{ echo "Error: Failed to extract Bun"; exit 1; } | |
# Move executable | |
if [[ "$target" == windows-* ]]; then | |
mv "$BIN_DIR/bun-$target/bun.exe" "$EXE.exe" || | |
{ echo "Error: Failed to move extracted Bun to destination"; exit 1; } | |
else | |
mv "$BIN_DIR/bun-$target/bun" "$EXE" || | |
{ echo "Error: Failed to move extracted Bun to destination"; exit 1; } | |
# Set permissions | |
chmod +x "$EXE" || | |
{ echo "Error: Failed to set permissions on Bun executable"; exit 1; } | |
fi | |
# Clean up | |
rm -r "$BIN_DIR/bun-$target" "$EXE.zip" | |
echo "Bun was installed successfully to $EXE" | |
echo "You can run Bun with:" | |
if [[ "$target" == windows-* ]]; then | |
echo " \"$EXE.exe\"" | |
else | |
echo " \"$EXE\"" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment