code-server是运行在远程服务器上的VS Code,可以通过浏览器访问。使用code-server可以让多个电脑有相同的开发环境,代码编译和测试等可以直接在服务器上进行。本文主要介绍Linux上安装配置code-server的方法。

1、必要条件

安装 code-server 需要一台可以运行code-server的服务器。可以使用自己拥有的物理机器,也可以使用云服务器。至少需要2核CPU和1 GB内存,可以使用任何Linux发行版,环境必须启用websocket,因为code-server使用websocket在浏览器和服务器之间通信。

云服务器:https://www.cjavapy.com/article/822/

2、通过官方脚本安装

安装code-server最简单的方法是使用官方提供针对Linux、macOS和FreeBSD的安装脚本。如可能,install脚本尝试使用系统包管理器。

1)显示安装命令但不进行安装

[root@localhost ~]# curl -fsSL https://code-server.dev/install.sh | sh -s -- --dry-run
CentOS Linux 7 (Core)
Installing v4.19.0 of the amd64 rpm package from GitHub.
mkdir -p ~/.cache/code-server
curl -#fL -o ~/.cache/code-server/code-server-4.19.0-amd64.rpm.incomplete -C - https://github.com/coder/code-server/releases/download/v4.19.0/code-server-4.19.0-amd64.rpm
mv ~/.cache/code-server/code-server-4.19.0-amd64.rpm.incomplete ~/.cache/code-server/code-server-4.19.0-amd64.rpm
rpm -U ~/.cache/code-server/code-server-4.19.0-amd64.rpm
rpm package has been installed.
To have systemd start code-server now and restart on boot:
sudo systemctl enable --now code-server@$USER
Or, if you don't want/need a background service you can run:
code-server
Deploy code-server for your team with Coder: https://github.com/coder/coder

2)执行安装

curl -fsSL https://code-server.dev/install.sh | sh

命令说明:

选项

描述

--dry-run

显示安装过程中的命令,但不执行它们。

--method

选择安装方法。

--method=detect

检测包管理器,但如果失败则使用

--method=standalone

方法。

--method=standalone

将独立发布的归档文件安装到

~/.local

目录。

--prefix=/usr/local

将独立发布的归档文件安装到系统范围内(例如

/usr/local

目录)。

--version=X.X.X

安装

X.X.X

版本,而不是最新版本。

--help

查看使用文档。

--edge

安装最新的边缘版本(即预发布版本)。

注意:安装 使用脚本安装时,可能遇到 "curl: (7) Failed connect to raw.githubusercontent.com:443; 拒绝连接" 的错误,此时可以尝试设置 http 代理来解决,命令如下:

export https_proxy=http://192.168.31.31:8118

参考文档 :https://coder.com/docs/code-server/latest/install 

3、通过程序文件安装 

如不能使用上面脚本方式安装,可以尝试下载编译好的二进制程序文件进行安装,命令如下: 

下载地址:https://github.com/coder/code-server/releases/download/v3.4.1/code-server-3.4.1-linux-amd64.tar.gz 

若要区分使用的程序文件,取决于操作系统和CPU的架构。CentOS 7上推荐使用code-server-3.4.1版本,如想使用较新的版本,可以尝试使用近几年CentOS Stream 、Rocky Linux、AlmaLinux或 Ubuntu 发布的新版本的Linux。 

1)查看系统

cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)

2)下载程序文件并解压

wget https://github.com/cdr/code-server/releases/download/3.4.1/code-server-3.4.1-linux-x86_64.tar.gz
tar -xvzf code-server-3.4.1-linux-x86_64.tar.gz

3)查看帮助信息

cd code-server-3.4.1-linux-x86_64
./code-server --help
***** Please use the script in bin/code-server instead!
***** This script will soon be removed!
***** See the release notes at https://github.com/cdr/code-server/releases/tag/v3.4.0
info  Wrote default config file to ~/.config/code-server/config.yaml
info  Using config file ~/.config/code-server/config.yaml
code-server 3.4.1 48f7c2724827e526eeaa6c2c151c520f48a61259

Usage: code-server [options] [path]

Options
      --auth                The type of authentication to use. [password, none]
      --password            The password for password authentication (can only be passed in via $PASSWORD or the config file).
      --cert                Path to certificate. Generated if no path is provided.
      --cert-key            Path to certificate key when using non-generated cert.
      --disable-telemetry   Disable telemetry.
   -h --help                Show this output.
      --open                Open in browser on startup. Does not work remotely.
      --bind-addr           Address to bind to in host:port. You can also use $PORT to override the port.
      --config              Path to yaml config file. Every flag maps directly to a key in the config file.
      --socket              Path to a socket (bind-addr will be ignored).
   -v --version             Display version information.
      --user-data-dir       Path to the user data directory.
      --extensions-dir      Path to the extensions directory.
      --list-extensions     List installed VS Code extensions.
      --force               Avoid prompts when installing VS Code extensions.
      --install-extension   Install or update a VS Code extension by id or vsix.
      --uninstall-extension Uninstall a VS Code extension by id.
      --show-versions       Show VS Code extension versions.
      --proxy-domain        Domain used for proxying ports.
 -vvv --verbose             Enable verbose logging.

注意:

如在CentOS 7 上安装较新版本的 coder-server,glibc版本会有问题,可能提示如下信息:

node: /lib64/libm.so.6: version GLIBC_2.27' not found (required by node) node: /lib64/libc.so.6: version GLIBC_2.25' not found (required by node)
node: /lib64/libc.so.6: version GLIBC_2.28' not found (required by node) node: /lib64/libstdc++.so.6: version CXXABI_1.3.9' not found (required by node)
node: /lib64/libstdc++.so.6: version GLIBCXX_3.4.20' not found (required by node) node: /lib64/libstdc++.so.6: version GLIBCXX_3.4.21' not found (required by node)

解决最好的办法,安装使用新版本的Linux。

4)启动 code-server

./code-server --bind-addr 0.0.0.0:8080
***** Please use the script in bin/code-server instead!
***** This script will soon be removed!
***** See the release notes at https://github.com/cdr/code-server/releases/tag/v3.4.0
info  Using config file ~/.config/code-server/config.yaml
info  Using user-data-dir ~/.local/share/code-server
info  code-server 3.4.1 48f7c2724827e526eeaa6c2c151c520f48a61259
info  HTTP server listening on http://0.0.0.0:8080
info      - Using password from ~/.config/code-server/config.yaml
info      - To disable use `--auth none`
info    - Not serving HTTPS

在浏览器上即可访问了。初次访问,需要输入登录密码,密码可以在~/.config/code-server/config.yaml中找到。

ssh会话断开,code-server就会退出,后台运行程序可以参考下面的文档。

参考文档:

linux screen的使用及后台运行程序进程的方法

Linux 使用&、ctrl+z或screen后台运行命令程序的方法

推荐文档