在自己的Ubuntu服务器上构建VSCode Online

准备软件安装

  1. 安装Git
    sudo apt-get install git
  2. 安装Node.JS
    这里按照微软官方的要求,必须是x64的,而且版本号在10.x到12.x之间的nodejs,我这里采用的就是v10.19.0版本
sudo apt-get install nodejs
sudo apt-get install npm
sudo npm install npm -g
sudo npm install -g n
sudo n 10.19.0
  1. 安装yarn
sudo apt-get install curl
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt update && sudo apt install yarn
  1. 安装Python
    按照微软官网的要求,这里只能安装python2.7的版本,直接apt install默认就是2.7了
sudo apt-get install python
root@ubuntu:/home# python --version
Python 2.7.18
  1. 安装C++相关组件
sudo apt-get install libx11-dev libxkbfile-dev
sudo apt-get install libsecret-1-dev
sudo apt-get install fakeroot rpm

到这里,所有的准备工作就完成了,接下来就是编译和运行了

安装源代码

  1. clone 源代码
wget https://github.com/microsoft/vscode/archive/1.45.1.zip
unzip 1.45.1.zip
  1. 构建vscode
cd vscode-1.45.1/
yarn

看到Done就是完成了

构建完成之后需要执行下面的命令

yarn watch

接下执行

yarn web --host 10.18.0.35 --port 8080

--port 来控制端口,--host 来控制监听地址

接下来,浏览器访问 http://10.18.0.35:8080 就能看到熟悉的界面

code-server 安装

直接安装code-server 版本更好:

code-server 是 Coder公司 基于VSCode的开源项目,可以实现通过浏览器访问在远程服务器上的VS Code。
简单地说,是基于 VSCode 进行了一些修改,专门为浏览器设计优化,以便作为可托管的 Web 服务来运行。
换而言之,配置服务器端(code-server)后,就可以在任何浏览器上访问和使用 VS Code。

  1. 下载:
curl -o vscode.tar.gz https://github.com/cdr/code-server/releases/download/v3.5.0/code-server-3.5.0-linux-x86_64.tar.gz
  1. 解压
    tar -zxf code-server-3.5.0-linux-x86_64.tar.gz

  2. 进入
    cd code-server-3.5.0-linux-x86_64

  3. 开启web服务
    设置密码到换变量
    export PASSWORD="1234"

  4. 运行

./code-server --port 8080 --host 0.0.0.0 --auth password 
  1. 然后就可以在浏览器访问了
http://10.18.0.35:8080/

没有遇到问题,很顺利,建议直接安装这个就可以了

参考

[1]https://www.cnblogs.com/lee-li/p/12041546.html

[2]https://hexo.lyh.best/20210303/Linux-%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%90%AD%E5%BB%BA-Web-%E7%89%88-VSCode/#CentOS7-%E9%83%A8%E7%BD%B2

[3] https://www.cnblogs.com/billyme/p/13769847.html

问题

1. vscode遇到问题,报错如下

azheng@lishizheng:/mnt/e$ sudo apt-get  update
Hit:1 http://mirrors.aliyun.com/ubuntu focal InRelease
Hit:2 http://mirrors.aliyun.com/ubuntu focal-updates InRelease
Hit:3 http://mirrors.aliyun.com/ubuntu focal-backports InRelease
Ign:4 http://mirrors.aliyun.com/ubuntu trusty InRelease
Hit:5 http://mirrors.aliyun.com/ubuntu trusty-security InRelease
Hit:6 http://mirrors.aliyun.com/ubuntu trusty-updates InRelease
Get:7 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Ign:8 http://ppa.launchpad.net/ubuntu-desktop/ubuntu-make/ubuntu focal InRelease
Hit:9 http://mirrors.aliyun.com/ubuntu trusty-proposed InRelease
Hit:10 http://mirrors.aliyun.com/ubuntu trusty-backports InRelease
Hit:11 http://mirrors.aliyun.com/ubuntu trusty Release
Err:13 http://ppa.launchpad.net/ubuntu-desktop/ubuntu-make/ubuntu focal Release
  404  Not Found [IP: 91.189.95.85 80]
Reading package lists... Done
E: The repository 'http://ppa.launchpad.net/ubuntu-desktop/ubuntu-make/ubuntu focal Release' does not have a Release file.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.

解决方法
输入以下命令:

sudo apt-add-repository -r ppa:ubuntu-desktop/ubuntu-make
然后再输入

sudo apt update
sudo apt-get update
  1. 需要nodejs 版版本大于12:
root@ubuntu:/home/vscode-1.45.1# yarn
yarn install v1.22.17
$ node build/npm/preinstall.js
*** Please use node >=10 and <=12.

error Command failed with exit code 1.


所以要升级nodejs版本,方法如下:

由于Ubuntu20通过apt安装nodejs默认只能到10.xxx版本。最新版本的话需要通过二进制或者源码安装,源码安装需要进行编译耗时较长,本文主要介绍二进制文件安装方法。具体步骤如下:

  1. 在该目录下找到最新的node版本:https://nodejs.org/dist
  2. 通过命令下载:

wget https://nodejs.org/dist/v10.24.0/node-v10.24.0-linux-x64.tar.gz
  1. 解压,把解压文件移动到/usr/local/ 目录下
//本地解压
tar -xvf node-v10.24.0-linux-x64.tar.gz

//将解压后的文件夹整体移动到/usr/local/node
sudo mv node-v10.24.0-linux-x64/* /usr/local/node
  1. 在/usr/bin 目录下建立软连接
//切换目录
cd /usr/bin
//创建node软链接
sudo ln -s /usr/local/node/bin/node node
//创建npm软链接
sudo ln -s /usr/local/node/bin/npm npm

这里我发现这两个软连接已经存在了,我直接使用rm 命令删除了他们

  1. 验证安装
    推出到任意目录,输入命令node -v

2. yarn命令执行的时候报错

error /home/vscode/node_modules/electron: Command failed.
Exit code: 1
Command: node install.js
Arguments: 
Directory: /home/vscode/node_modules/electron
Output:
RequestError: socket hang up
    at ClientRequest.<anonymous> (/home/vscode/node_modules/got/source/request-as-event-emitter.js:178:14)
    at Object.onceWrapper (events.js:520:26)
    at ClientRequest.emit (events.js:412:35)
    at ClientRequest.origin.emit (/home/vscode/node_modules/@szmarczak/http-timer/source/index.js:37:11)
    at TLSSocket.socketOnEnd (_http_client.js:499:9)

这是由于网络异常,electron-download 模块安装异常,可以通过设置electron代理解决

npm config set electron_mirror https://npm.taobao.org/mirrors/electron/

3.

$ yarn --ignore-engines
yarn install v1.22.17
[1/4] Resolving packages...
[2/4] Fetching packages...
error Command failed.
Exit code: 128
Command: git
Arguments: ls-remote --tags --heads https://github.com/mjbvz/markdown-it-katex.git
Directory: /home/vscode/extensions/markdown-math
Output:
fatal: unable to access 'https://github.com/mjbvz/markdown-it-katex.git/': GnuTLS recv error (-110): The TLS connection was non-properly terminated.
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.

多尝试几次后,竟然好了。应该是网络问题

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。