Skip to content

Commit 3bb0c6d

Browse files
committed
Make installer work on any os/arch
1 parent d2cf88b commit 3bb0c6d

File tree

1 file changed

+120
-111
lines changed

1 file changed

+120
-111
lines changed

install.sh

Lines changed: 120 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
set -eu
33

44
# code-server's automatic install script.
5-
# See https://github.com/cdr/code-server/blob/main/docs/install
5+
# See https://coder.com/docs/code-server/v3.10.2/install
66

77
usage() {
88
arg0="$0"
@@ -14,7 +14,7 @@ usage() {
1414
fi
1515

1616
cath << EOF
17-
Installs code-server for Linux, macOS and FreeBSD.
17+
Installs code-server.
1818
It tries to use the system package manager if possible.
1919
After successful installation it explains how to start using code-server.
2020
@@ -48,24 +48,23 @@ Usage:
4848
--rsh <bin>
4949
Specifies the remote shell for remote installation. Defaults to ssh.
5050
51-
- For Debian, Ubuntu and Raspbian it will install the latest deb package.
52-
- For Fedora, CentOS, RHEL and openSUSE it will install the latest rpm package.
53-
- For Arch Linux it will install the AUR package.
54-
- For any unrecognized Linux operating system it will install the latest standalone
55-
release into ~/.local
51+
The detection method works as follows:
52+
- Debian, Ubuntu, Raspbian: install the deb package from GitHub.
53+
- Fedora, CentOS, RHEL, openSUSE: install the rpm package from GitHub.
54+
- Arch Linux: install from the AUR (which pulls releases from GitHub).
55+
- FreeBSD, Alpine: install from yarn/npm.
56+
- macOS: install using Homebrew if installed otherwise install from GitHub.
57+
- All others: install the release from GitHub.
5658
57-
- For macOS it will install the Homebrew package.
58-
- If Homebrew is not installed it will install the latest standalone release
59-
into ~/.local
59+
We only build releases on GitHub for amd64 and arm64 on Linux and amd64 for
60+
macOS. When the detection method tries to pull a release from GitHub it will
61+
fall back to installing from npm when there is no matching release for the
62+
system's operating system and architecture.
6063
61-
- For FreeBSD or Alpine, it will install the npm package with yarn or npm.
64+
The standalone method will force installion using GitHub releases. It will not
65+
fall back to npm so on architectures without pre-built releases this will error.
6266
63-
- If ran on an architecture with no releases, it will install the
64-
npm package with yarn or npm.
65-
- We only have releases for amd64 and arm64 presently.
66-
- The npm package builds the native modules on postinstall.
67-
68-
It will cache all downloaded assets into ~/.cache/code-server
67+
The installer will cache all downloaded assets into ~/.cache/code-server
6968
7069
More installation docs are at https://coder.com/docs/code-server/v3.10.2/install
7170
EOF
@@ -198,80 +197,68 @@ main() {
198197
return
199198
fi
200199

201-
VERSION="${VERSION-$(echo_latest_version)}"
202200
METHOD="${METHOD-detect}"
203201
if [ "$METHOD" != detect ] && [ "$METHOD" != standalone ]; then
204202
echoerr "Unknown install method \"$METHOD\""
205203
echoerr "Run with --help to see usage."
206204
exit 1
207205
fi
208-
STANDALONE_INSTALL_PREFIX="${STANDALONE_INSTALL_PREFIX-$HOME/.local}"
209206

210-
OS="$(os)"
211-
if [ ! "$OS" ]; then
212-
echoerr "Unsupported OS $(uname)."
213-
exit 1
214-
fi
207+
# These are used by the various install_* functions that make use of GitHub
208+
# releases in order to download and unpack the right release.
209+
CACHE_DIR=$(echo_cache_dir)
210+
STANDALONE_INSTALL_PREFIX=${STANDALONE_INSTALL_PREFIX:-$HOME/.local}
211+
VERSION=${VERSION:-$(echo_latest_version)}
212+
# These can be overridden for testing but shouldn't normally be used as it can
213+
# result in a broken code-server.
214+
OS=${OS:-$(os)}
215+
ARCH=${ARCH:-$(arch)}
215216

216217
distro_name
217218

218-
ARCH="$(arch)"
219-
if [ ! "$ARCH" ]; then
220-
if [ "$METHOD" = standalone ]; then
221-
echoerr "No precompiled releases for $(uname -m)."
222-
echoerr 'Please rerun without the "--method standalone" flag to install from npm.'
223-
exit 1
224-
fi
225-
echoh "No precompiled releases for $(uname -m)."
226-
install_npm
227-
return
228-
fi
229-
230-
if [ "$OS" = "freebsd" ]; then
231-
if [ "$METHOD" = standalone ]; then
232-
echoerr "No precompiled releases available for $OS."
233-
echoerr 'Please rerun without the "--method standalone" flag to install from npm.'
234-
exit 1
235-
fi
236-
echoh "No precompiled releases available for $OS."
237-
install_npm
238-
return
239-
fi
240-
241-
if [ "$OS" = "linux" ] && [ "$(distro)" = "alpine" ]; then
242-
if [ "$METHOD" = standalone ]; then
243-
echoerr "No precompiled releases available for alpine."
244-
echoerr 'Please rerun without the "--method standalone" flag to install from npm.'
219+
# Standalone installs by pulling pre-built releases from GitHub.
220+
if [ "$METHOD" = standalone ]; then
221+
if has_standalone; then
222+
install_standalone
223+
else
224+
echoerr "There are no standalone releases for $ARCH"
225+
echoerr "Please try again without '--method standalone'"
245226
exit 1
246227
fi
247-
echoh "No precompiled releases available for alpine."
248-
install_npm
249-
return
250228
fi
251229

252-
CACHE_DIR="$(echo_cache_dir)"
253-
254-
if [ "$METHOD" = standalone ]; then
255-
install_standalone
256-
return
257-
fi
230+
# DISTRO can be overridden for testing but shouldn't normally be used as it
231+
# can result in a broken code-server.
232+
DISTRO=${DISTRO:-$(distro)}
258233

259-
case "$(distro)" in
234+
case $DISTRO in
235+
# macOS uses brew when available and falls back to standalone. We only have
236+
# amd64 for macOS so for anything else use npm.
260237
macos)
261-
install_macos
262-
;;
263-
debian)
264-
install_deb
265-
;;
266-
fedora | opensuse)
267-
install_rpm
268-
;;
269-
arch)
270-
install_aur
238+
if should_use_brew; then
239+
install_brew
240+
else
241+
echoh "Homebrew not installed."
242+
echoh "Falling back to standalone installation."
243+
npm_fallback install_standalone
244+
fi
271245
;;
246+
# The .deb and .rpm files are pulled from GitHub and we only have amd64 and
247+
# arm64 there and need to fall back to npm otherwise.
248+
debian) npm_fallback install_deb ;;
249+
fedora | opensuse) npm_fallback install_rpm ;;
250+
# Arch uses the AUR package which only supports amd64 and arm64 since it
251+
# pulls releases from GitHub so we need to fall back to npm.
252+
arch) npm_fallback install_aur ;;
253+
# We don't have GitHub releases that work on Alpine or FreeBSD so we have no
254+
# choice but to use npm here.
255+
alpine | freebsd) install_npm ;;
256+
# For anything else we'll try to install standalone but fall back to npm if
257+
# we don't have releases for the architecture.
272258
*)
273259
echoh "Unsupported package manager."
274-
install_standalone
260+
echoh "Falling back to standalone installation."
261+
npm_fallback install_standalone
275262
;;
276263
esac
277264
}
@@ -326,23 +313,15 @@ fetch() {
326313
sh_c mv "$FILE.incomplete" "$FILE"
327314
}
328315

329-
install_macos() {
330-
if command_exists brew; then
331-
echoh "Installing from Homebrew."
332-
echoh
333-
334-
sh_c brew install code-server
335-
336-
return
337-
fi
338-
339-
echoh "Homebrew not installed."
316+
install_brew() {
317+
echoh "Installing latest from Homebrew."
318+
echoh
340319

341-
install_standalone
320+
sh_c "${BREW_PATH:-brew}" install code-server
342321
}
343322

344323
install_deb() {
345-
echoh "Installing v$VERSION deb package from GitHub releases."
324+
echoh "Installing v$VERSION of the $ARCH deb package from GitHub."
346325
echoh
347326

348327
fetch "https://github.com/cdr/code-server/releases/download/v$VERSION/code-server_${VERSION}_$ARCH.deb" \
@@ -353,7 +332,7 @@ install_deb() {
353332
}
354333

355334
install_rpm() {
356-
echoh "Installing v$VERSION rpm package from GitHub releases."
335+
echoh "Installing v$VERSION of the $ARCH rpm package from GitHub."
357336
echoh
358337

359338
fetch "https://github.com/cdr/code-server/releases/download/v$VERSION/code-server-$VERSION-$ARCH.rpm" \
@@ -364,7 +343,7 @@ install_rpm() {
364343
}
365344

366345
install_aur() {
367-
echoh "Installing from the AUR."
346+
echoh "Installing latest from the AUR."
368347
echoh
369348

370349
sh_c mkdir -p "$CACHE_DIR/code-server-aur"
@@ -379,7 +358,7 @@ install_aur() {
379358
}
380359

381360
install_standalone() {
382-
echoh "Installing standalone release archive v$VERSION from GitHub releases."
361+
echoh "Installing v$VERSION of the $ARCH release from GitHub."
383362
echoh
384363

385364
fetch "https://github.com/cdr/code-server/releases/download/v$VERSION/code-server-$VERSION-$OS-$ARCH.tar.gz" \
@@ -406,6 +385,9 @@ install_standalone() {
406385
}
407386

408387
install_npm() {
388+
echoh "Installing latest from npm."
389+
echoh
390+
409391
if command_exists yarn; then
410392
sh_c="sh_c"
411393
if [ ! -w "$(yarn global bin)" ]; then
@@ -434,22 +416,41 @@ install_npm() {
434416
exit 1
435417
}
436418

437-
os() {
438-
case "$(uname)" in
439-
Linux)
440-
echo linux
441-
;;
442-
Darwin)
443-
echo macos
444-
;;
445-
FreeBSD)
446-
echo freebsd
419+
# Run $1 if we have a standalone otherwise run install_npm.
420+
npm_fallback() {
421+
if has_standalone; then
422+
$1
423+
else
424+
echoh "No standalone releases for $ARCH."
425+
echoh "Falling back to installation from npm."
426+
install_npm
427+
fi
428+
}
429+
430+
# Determine if we have standalone releases on GitHub for the system's arch.
431+
has_standalone() {
432+
case $ARCH in
433+
amd64) return 0 ;;
434+
# We only have amd64 for macOS.
435+
arm64)
436+
[ "$(distro)" != macos ]
437+
return
447438
;;
439+
*) return 1 ;;
440+
esac
441+
}
442+
443+
os() {
444+
uname="$(uname)"
445+
case $uname in
446+
Linux) echo linux ;;
447+
Darwin) echo macos ;;
448+
FreeBSD) echo freebsd ;;
449+
*) echo "$uname" ;;
448450
esac
449451
}
450452

451-
# distro prints the detected operating system including linux distros.
452-
# Also parses ID_LIKE for common distro bases.
453+
# Print the detected Linux distro, otherwise print the OS name.
453454
#
454455
# Example outputs:
455456
# - macos -> macos
@@ -486,7 +487,7 @@ distro() {
486487
fi
487488
}
488489

489-
# os_name prints a pretty human readable name for the OS/Distro.
490+
# Print a human-readable name for the OS/distro.
490491
distro_name() {
491492
if [ "$(uname)" = "Darwin" ]; then
492493
echo "macOS v$(sw_vers -productVersion)"
@@ -506,19 +507,27 @@ distro_name() {
506507
}
507508

508509
arch() {
509-
case "$(uname -m)" in
510-
aarch64)
511-
echo arm64
512-
;;
513-
x86_64)
514-
echo amd64
515-
;;
516-
amd64) # FreeBSD.
517-
echo amd64
518-
;;
510+
uname_m=$(uname -m)
511+
case $uname_m in
512+
aarch64) echo arm64 ;;
513+
x86_64) echo amd64 ;;
514+
*) echo "$uname_m" ;;
519515
esac
520516
}
521517

518+
should_use_brew() {
519+
# BREW_PATH can be provided for testing but shouldn't normally be used as it
520+
# can result in the brew install failing.
521+
if [ "${BREW_PATH-}" ]; then # Set and has value.
522+
return 0
523+
elif [ "${BREW_PATH+x}" ]; then # Set but no value.
524+
return 1
525+
elif command_exists brew; then
526+
return 0
527+
fi
528+
return 1
529+
}
530+
522531
command_exists() {
523532
command -v "$@" > /dev/null
524533
}

0 commit comments

Comments
 (0)