開発

仮想マシンでVS Code Serverを立ち上げてサービスに登録してバックグラウンドで起動する

はじめに

VS Code Server というGithub Codespace のような、というかほぼ同じような機能を果たすリモート開発環境を試してみたいと思います。

これは以前紹介したWindows 版とは違い Linux上で動作し起動します。

インストールしてサービスとして起動するまで行います。

前提

事前に仮想マシンを作成しておきます。

OSはUbuntu 22.04で作成しています。

Github のアカウントは必須です。

VSCodeはクライアントにインストール済であることです。

インストール

とりあえずアップデートを行います。

apt update && apt upgrade

VS Code Serverのインストールは簡単です。

公式のブログではWSL環境へのインストールが紹介されていますが、通常のディストリビューションでもインストールが可能です。

wget -O- https://aka.ms/install-vscode-server/setup.sh | sh

--2023-01-04 07:08:08--  https://aka.ms/install-vscode-server/setup.sh
Resolving aka.ms (aka.ms)... 2.22.197.32
Connecting to aka.ms (aka.ms)|2.22.197.32|:443... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: https://vscodeserverlauncher.blob.core.windows.net/builds/setup-scripts/setup.sh [following]
--2023-01-04 07:08:08--  https://vscodeserverlauncher.blob.core.windows.net/builds/setup-scripts/setup.sh
Resolving vscodeserverlauncher.blob.core.windows.net (vscodeserverlauncher.blob.core.windows.net)... 20.150.78.68
Connecting to vscodeserverlauncher.blob.core.windows.net (vscodeserverlauncher.blob.core.windows.net)|20.150.78.68|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1758 (1.7K) [text/x-sh]
Saving to: ‘STDOUT’

-                             100%[=================================================>]   1.72K  --.-KB/s    in 0s

2023-01-04 07:08:08 (931 MB/s) - written to stdout [1758/1758]

glibc version is 2.35
is min? 1
Installing from https://aka.ms/vscode-server-launcher/x86_64-unknown-linux-gnu

以上でインストール自体は完了です。

初回は手動で起動します。

VS Code Server

*
* By using the software, you agree to the
* the Visual Studio Code Server License Terms (https://aka.ms/vscode-server-license) and
* the Microsoft Privacy Statement (https://privacy.microsoft.com/en-US/privacystatement).
*

? Do you accept the terms in the License Agreement (y/n) › yes
✔ Do you accept the terms in the License Agreement · yes
To grant access to the server, please log into https://github.com/login/device and use code 91F5-7156

アクセスしてコードを入力します。

アクセス先のURLが作成されます。

✔ What would you like to call this machine? · trusting-gull
[2023-01-04 07:14:51] info Creating tunnel with the name: trusting-gull

Open this link in your browser https://insiders.vscode.dev/+ms-vscode.remote-server/trusting-gull

アクセスして起動しているか確認します。


以上で起動は完了です。

終了はCtrl + C で終了します。

これではコマンドによる起動でしかできないので、サービスとして登録します。

サービス登録

2001y ( @Y20010920T )さんが記事をかいてますね。これをそのまま利用しましょう。

serviceファイルを作成します。

vi /etc/systemd/system/vscode-server.service

[Unit]
Description = VSCode Server Service

[Service]
ExecStart = /usr/local/bin/code-server
Restart = always

[Install]
WantedBy = multi-user.target

ユーザーはrootで起動する必要がるようです。

サービスをenbaleにします。

systemctl enable vscode-server.service

Created symlink /etc/systemd/system/multi-user.target.wants/vscode-server.service → /etc/systemd/system/vscode-server.service.

サービスを起動します。

systemctl start vscode-server.service

以上で起動が確認できます。

# systemctl status vscode-server.service
● vscode-server.service - VSCode Server Service
     Loaded: loaded (/etc/systemd/system/vscode-server.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2023-01-04 07:43:12 UTC; 3s ago
   Main PID: 10955 (code-server)
      Tasks: 4 (limit: 9530)
     Memory: 5.4M
        CPU: 104ms
     CGroup: /system.slice/vscode-server.service
             └─10955 /usr/local/bin/code-server

Jan 04 07:43:12 vscodeserver systemd[1]: Started VSCode Server Service.
Jan 04 07:43:13 vscodeserver code-server[10955]: Open this link in your browser https://insiders.vscode.dev/+ms-vscode.remote-server/trusting-gull/

Statusを確認するとURLが表示されるのでアクセスして起動しているか確認します。

以上でサービス化も完了です。

まとめ

VS Code Serverを起動してサービスに登録することでバックグラウンドで起動することができるので、リモート開発環境としての利便性は上がるのではないのでしょうか。

-開発
-