code-serverの使い方〜iPadから快適にVSCodeを使う方法

ブラウザで起動するVisual Studio Code がいろいろリリースされており、使っている人も多いかと思います。

具体的には、GitHub CodeSpaceや、VSCode Server などありますが、この記事では、オープンソースのcode-serverを紹介します。

code-serverとは

Code-server は、coder.comが提供するOSSの Visual Studio Code です。

Linux上で稼働させることができます。Linuxは、AWSやGCPなどの仮想マシンでも問題ありません。

iPad Safari などのブラウザから利用可能です。

System Requirements はこちら↓にあります。

code-server/docs/requirements.md at main · coder/code-server
VS Code in the browser. Contribute to coder/code-server development by creating an account on GitHub.

インストール方法

ここからはcode-serverのインストール方法を紹介します。

Linuxの起動

まず、Linuxを起動します。System Requirements より、1 GB RAM, and 2 vCPUs なLinuxを起動します。

「1 GB RAM, and 2 vCPUs」を https://aws.amazon.com/ec2/instance-types/ から調べると、T4Gインスタンスではt4g.micro以上になります。

ちなみに、t4g.microの1vCPUあたりのbaseline performanceは10% になります。baseline performance の説明は以下にあります。

Key concepts for burstable performance instances - Amazon Elastic Compute Cloud
Understand the key concepts and definitions of CPU performance for burstable performance instances.

今回はAWSのUbuntuを使用することにしました。

apt update && apt upgrade

初めて起動する、久しぶりに起動するUbuntu Linuxでしたら、Sudo apt updateSudo apt upgradeを実行しておきましょう。

code-server インストール

以下のコマンドを実行してcode-serverをインストールしていきましょう。

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

順調にインストールが進むと、以下のようなメッセージが表示されます。

Setting up code-server (4.6.0) ...

deb 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

サービス登録

以下のコマンドを実行して、サービス登録しておきましょう。

sudo systemctl enable --now code-server@$USER

以下のメッセージが表示されたら成功です。

Created symlink /etc/systemd/system/default.target.wants/code-server@ubuntu.service → /lib/systemd/system/code-server@.service.

code-server起動

以下のコマンドでcode-serverを起動します。

sudo systemctl start code-server@$USER

configファイル編集

~/.config/code-serverconfig.yamlが格納されています。

~/.config/code-server$ cat config.yaml 
bind-addr: 127.0.0.1:8080
auth: password
password: 6859xxxxxxxxxxxxxxxxxxx
cert: false

この中のbind-addr0.0.0.0:8080に変更しましょう。

~/.config/code-server$ cat config.yaml 
bind-addr: 0.0.0.0:8080
auth: password
password: 6859xxxxxxxxxxxxxxxxxxx
cert: false

こうすることでグローバルIPアドレスからアクセスできるようになりますが、そうすると全世界に対して公開してしまうことになります。

これは防がなくてはなりませんので、EC2のInbound Rulesから自分のグローバルIPアドレスに対してのみ許可するよう設定しましょう。

Inbound Rulesの設定が完了したら、以下のコマンドでcode-serverを再起動しましょう。

sudo systemctl restart code-server@$USER

使い方

iPadなどのブラウザから「http://グローバルIPアドレス:8080」にアクセスしましょう

以下のような画面が表示されますので、config.yaml に記載されている「password」の文字列を入力してください。

Visual Studio Codeの画面が表示されたら成功です。

今回は .NET6 を使用してコンソールアプリを作ってみます。

SDKをインストールしていない人は、サクッとapt installしちゃいましょう。

sudo apt install dotnet6

フォルダを作成して、dotnet new consoleしましょう。

$ mkdir consoleapp
$ cd consoleapp/
$ dotnet new console

code-serverの画面からOpen Folderしてみます。

c#の拡張機能をインストールしましょう。

Language Serverが稼働しているのがわかると思います。

Program.csを編集すると、補完が効いているのがわかりますね。

ちなみにTerminalはCode-Serverが稼働しているLinuxと繋がっていますので、なんでもできます。

終わりに

code-serverの拡張機能は、Visual Studio Maketplaceではなく、Open VSXが使われていますが、大抵のオープンソースの拡張機能は用意されていますので、利用する上では問題ではないかなと個人的には思います。

ただし製作者の名前が見慣れない名前だったりして戸惑うことも多々あると思いますので、よく確認の上利用するようにしましょう。

コメント

タイトルとURLをコピーしました