Skip to content

Commit 5e49d2d

Browse files
committed
Make installer work on any os/arch
1 parent db9f060 commit 5e49d2d

File tree

1 file changed

+94
-106
lines changed

1 file changed

+94
-106
lines changed

install.sh

Lines changed: 94 additions & 106 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"
@@ -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,63 @@ 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+
OS=$(os)
213+
ARCH=$(arch)
215214

216215
distro_name
217216

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.'
217+
# Standalone installs by pulling pre-built releases from GitHub.
218+
if [ "$METHOD" = standalone ]; then
219+
if has_standalone; then
220+
install_standalone
221+
else
222+
echoerr "There are no standalone releases for $(arch)"
223+
echoerr "Please try again without '--method standalone'"
245224
exit 1
246225
fi
247-
echoh "No precompiled releases available for alpine."
248-
install_npm
249-
return
250-
fi
251-
252-
CACHE_DIR="$(echo_cache_dir)"
253-
254-
if [ "$METHOD" = standalone ]; then
255-
install_standalone
256-
return
257226
fi
258227

228+
# Otherwise try using the system's package manager where possible.
259229
case "$(distro)" in
230+
# macOS uses brew which should work for any architecture. Fall back to
231+
# standalone/npm if brew is not installed.
260232
macos)
261-
install_macos
262-
;;
263-
debian)
264-
install_deb
265-
;;
266-
fedora | opensuse)
267-
install_rpm
268-
;;
269-
arch)
270-
install_aur
233+
if command_exists brew; then
234+
install_brew
235+
else
236+
echoh "Homebrew not installed."
237+
echoh "Falling back to standalone installation."
238+
npm_fallback install_standalone
239+
fi
271240
;;
241+
# The .deb and .rpm files are pulled from GitHub and we only have amd64 and
242+
# arm64 there and need to fall back to npm otherwise.
243+
debian) npm_fallback install_deb ;;
244+
fedora | opensuse) npm_fallback install_rpm ;;
245+
# Arch uses the AUR package which only supports amd64 and arm64 since it
246+
# pulls releases from GitHub so we need to fall back to npm.
247+
arch) npm_fallback install_aur ;;
248+
# We don't have GitHub releases that work on Alpine or FreeBSD so we have no
249+
# choice but to use npm here.
250+
alpine | freebsd) install_npm ;;
251+
# For anything else we'll try to install standalone but fall back to npm if
252+
# we don't have releases for the architecture.
272253
*)
273254
echoh "Unsupported package manager."
274-
install_standalone
255+
echoh "Falling back to standalone installation."
256+
npm_install install_standalone
275257
;;
276258
esac
277259
}
@@ -326,19 +308,11 @@ fetch() {
326308
sh_c mv "$FILE.incomplete" "$FILE"
327309
}
328310

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."
311+
install_brew() {
312+
echoh "Installing from Homebrew."
313+
echoh
340314

341-
install_standalone
315+
sh_c brew install code-server
342316
}
343317

344318
install_deb() {
@@ -434,22 +408,41 @@ install_npm() {
434408
exit 1
435409
}
436410

437-
os() {
438-
case "$(uname)" in
439-
Linux)
440-
echo linux
441-
;;
442-
Darwin)
443-
echo macos
444-
;;
445-
FreeBSD)
446-
echo freebsd
411+
# Run $1 if we have a standalone otherwise run install_npm.
412+
npm_fallback() {
413+
if has_standalone; then
414+
$1
415+
else
416+
echoh "No standalone releases for $(arch)."
417+
echoh "Falling back to installation from npm."
418+
install_npm
419+
fi
420+
}
421+
422+
# Determine if we have standalone releases on GitHub for the system's arch.
423+
has_standalone() {
424+
case $(arch) in
425+
amd64) return 0 ;;
426+
# We only have amd64 for macOS.
427+
arm64)
428+
[ "$(distro)" != macos ]
429+
return
447430
;;
431+
*) return 1 ;;
448432
esac
449433
}
450434

451-
# distro prints the detected operating system including linux distros.
452-
# Also parses ID_LIKE for common distro bases.
435+
os() {
436+
uname="$(uname)"
437+
case $uname in
438+
Linux) echo linux ;;
439+
Darwin) echo macos ;;
440+
FreeBSD) echo freebsd ;;
441+
*) echo "$uname" ;;
442+
esac
443+
}
444+
445+
# Print the detected Linux distro, otherwise print the OS name.
453446
#
454447
# Example outputs:
455448
# - macos -> macos
@@ -486,7 +479,7 @@ distro() {
486479
fi
487480
}
488481

489-
# os_name prints a pretty human readable name for the OS/Distro.
482+
# Print a human-readable name for the OS/distro.
490483
distro_name() {
491484
if [ "$(uname)" = "Darwin" ]; then
492485
echo "macOS v$(sw_vers -productVersion)"
@@ -506,16 +499,11 @@ distro_name() {
506499
}
507500

508501
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-
;;
502+
uname_m=$(uname -m)
503+
case $uname_m in
504+
aarch64) echo arm64 ;;
505+
x86_64) echo amd64 ;;
506+
*) echo "$uname_m" ;;
519507
esac
520508
}
521509

0 commit comments

Comments
 (0)