How to install code-server on cloud

Aditya Padhi
3 min readJan 23, 2022

Code-server is a Visual Studio Code instance running on a remote server accessible through any web browser. It allows you to code anywhere and on any device such as a tablet or laptop with a consistent integrated development environment (IDE).

In this tutorial, we will be installing code-server on Cloud.

VSCode On Cloud

Pre-requisites:

  1. Running Cloud Instance ( 2 vCPUS + 4GB of RAM) — Free for life on Oracle Cloud (4 oCPUs + 24GB RAM SDFLEX Server).
  2. Image on Instance — Ubuntu 18.04 / 20.04
  3. Valid domain/subdomain mapped to the public IP of the instance. (If you wish to continue with an IP only replace `code-server.example.com` with the Public IP of the instance and follow until step 12.

Process:

  1. Connect to cloud instance using SSH.
ssh ubuntu@IPAddress

2. Upgrade built-in packages.

sudo apt update
sudo apt upgrade

3. Now test & install the code-server ( Reference )

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

4. Make sure you create a directory for storing user files

sudo mkdir /var/lib/code-server

5. Create a service file

sudo nano /lib/systemd/system/code-server.service

6. Include the following content and make sure you change the default password (code-server-password).

[Unit]
Description=code-server
After=nginx.service

[Service]
Type=simple
Environment=PASSWORD=code-server-password
ExecStart=/usr/bin/code-server --bind-addr 127.0.0.1:8080 --user-data-dir /var/lib/code-server --auth password
Restart=always

[Install]
WantedBy=multi-user.target

Once done, save the file and exit the editor.

7. Reload the system daemon to include the new service file which we created above and then start and enable the service.

sudo systemctl daemon-reloadsudo systemctl start code-serversudo systemctl enable code-server

8. Configuring secure proxy with Nginx and Let’s Encrypt

sudo apt install nginx

9. Next, create a configuration file for the proxy functionality.

sudo nano /etc/nginx/sites-available/code-server.conf

10. Then enter the following to enable just HTTP as a placeholder. Replace the code-server.example.com with your domain in the server name.

server {
listen 80;
listen [::]:80;
server_name code-server.example.com;
location / {
proxy_pass http://localhost:8080/;
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection upgrade;
proxy_set_header Accept-Encoding gzip;
}
}

Once you’re done, save the file and exit the editor.

11. Next, delete the default site by removing the symbolic link and creating a new one for your site configuration file.

sudo rm /etc/nginx/sites-enabled/defaultsudo ln -s /etc/nginx/sites-available/code-server.conf /etc/nginx/sites-enabled/code-server.conf

You should now be able to see the Nginx service active and running.

sudo systemctl status nginx

12. Make sure the ports are open:

sudo iptables -I INPUT 6 -m state --state NEW -p tcp --dport 80 -j ACCEPTsudo iptables -I INPUT 6 -m state --state NEW -p tcp --dport 443 -j ACCEPTsudo netfilter-persistent save

13. Install Certbot client available through package manager:

sudo apt install certbot python3-certbot-nginx

14. Use certbot client as shown below:

sudo certbot --nginx -d code-server.example.com

15. Restart the Nginx server:

sudo systemctl restart nginx

To check the configuration:

sudo nano /etc/nginx/sites-available/code-server.conf

Sample Configuration:-

server {
server_name code-server.example.com;
location / {
proxy_pass http://localhost:8080/;
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection upgrade;
proxy_set_header Accept-Encoding gzip;
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/code-server.example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/code-server.example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = code.adityacodes.online) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
listen [::]:80;
server_name code-server.example.com;
return 404; # managed by Certbot
}

16. Now that we have completed our setup you should be able to open the domain in any browser and login into VS Code.

Once logged in, you’ll enter the IDE and have full access to your developer environment saved on your own cloud server.

Thanks!

--

--