win10 安装 ubuntu20(WSL) 并安装 swift5.2 使用 vscode 开发

标题很长,但也不是故意的,最近打算使用swiftUI开发个APP试试,开发一点后发现,很多的swift语法不是特别的熟悉,而工作电脑的是win10的,所以打算在工作电脑上学习下swift的基本语法。

win10 WSL 安装 ubuntu

在WSL刚出来的时候ubuntu的安装还是比较麻烦的,但是现在容易了,直接在Microsoft Store 中搜索 ubuntu 后找到对应的想要的ubuntu版本即可:
win10 安装 ubuntu20(WSL) 并安装 swift5.2 使用 vscode 开发_第1张图片

通过Windows Terminal 访问Ubuntu

Windows Terminal的安装与上面的Ubuntu的安装类似,可以在Windows Store中直接安装,为什么用这个终端软件呢?因为好看啊!

Windows Terminal 相关的配置可以参考 Windows Terminal 配置参考,配置起来很方便,比较类似于vscode的配置方式,可以参考下我的配置方案:

// This file was initially generated by Windows Terminal (Preview) 1.0.1402.0
// It should still be usable in newer versions, but newer versions might have additional
// settings, help text, or changes that you will not see unless you clear this file
// and let us generate a new one for you.

// To view the default settings, hold "alt" while clicking on the "Settings" button.
// For documentation on these settings, see: https://aka.ms/terminal-documentation
{
  "$schema": "https://aka.ms/terminal-profiles-schema",

  "defaultProfile": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",

  // You can add more global application settings here.
  // To learn more about global settings, visit https://aka.ms/terminal-global-settings

  // If enabled, selections are automatically copied to your clipboard.
  "copyOnSelect": true,

  // If enabled, formatted data is also copied to your clipboard
  "copyFormatting": false,

  // 深色/浅色主题
  "theme": "dark",

  // 默认启动时的行数
  "initialRows": 40,

  // A profile specifies a command to execute paired with information about how it should look and feel.
  // Each one of them will appear in the 'New Tab' dropdown,
  //   and can be invoked from the commandline with `wt.exe -p xxx`
  // To learn more about profiles, visit https://aka.ms/terminal-profile-settings
  "profiles": {
    "defaults": {
      // Put settings here that you want to apply to all profiles.
    },
    "list": [{
        // Make changes here to the powershell.exe profile.
        "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
        "name": "Windows PowerShell",
        "colorScheme": "One Half Dark",
        "fontFace": "Cascadia Code",
        "commandline": "powershell.exe",
        "hidden": false
      },
      {
        // Make changes here to the cmd.exe profile.
        "guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
        "name": "命令提示符",
        "commandline": "cmd.exe",
        "hidden": false
      },
      {
        "guid": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",
        "hidden": false,
        "name": "PowerShell",
        "source": "Windows.Terminal.PowershellCore"
      },
      {
        "guid": "{759f2abc-edd5-55fe-975a-d09104ce666b}",
        "hidden": false,
        "name": "ubuntu18",
        "colorScheme": "One Half Dark",
        "backgroundImage": "F:\\WSL\\ubuntu18\\ubuntu-logo32.png",
        "backgroundImageOpacity": 0.3,
        "backgroundImageStretchMode": "none",
        "fontFace": "Cascadia Code",
        "acrylicOpacity": 0.9,
        "source": "Windows.Terminal.Wsl"
      },
      {
        "guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}",
        "hidden": false,
        "name": "Azure Cloud Shell",
        "source": "Windows.Terminal.Azure"
      },
      {
        "guid": "{07b52e3e-de2c-5db4-bd2d-ba144ed6c273}",
        "hidden": false,
        "name": "Ubuntu-20.04",
        "colorScheme": "One Half Dark",
        "backgroundImage": "F:\\WSL\\ubuntu18\\ubuntu-logo32.png",
        "backgroundImageOpacity": 0.3,
        "backgroundImageStretchMode": "none",
        "fontFace": "Cascadia Code",
        "acrylicOpacity": 0.75,
        "startingDirectory": "F:\\WSL\\ubuntu20",
        "source": "Windows.Terminal.Wsl",
        "useAcrylic" : true // 设置背景透明度
      }
    ]
  },

  // Add custom color schemes to this array.
  // To learn more about color schemes, visit https://aka.ms/terminal-color-schemes
  "schemes": [{
    "name": "Tomorrow Night Blue",
    "black": "#000000",
    "red": "#ff9da4",
    "green": "#d1f1a9",
    "yellow": "#ffeead",
    "blue": "#bbdaff",
    "purple": "#ebbbff",
    "cyan": "#99ffff",
    "white": "#ffffff",
    "brightBlack": "#000000",
    "brightRed": "#ff9da4",
    "brightGreen": "#d1f1a9",
    "brightYellow": "#ffeead",
    "brightBlue": "#bbdaff",
    "brightPurple": "#ebbbff",
    "brightCyan": "#99ffff",
    "brightWhite": "#ffffff",
    "background": "#002451",
    "foreground": "#ffffff"
  }],

  // Add custom keybindings to this array.
  // To unbind a key combination from your defaults.json, set the command to "unbound".
  // To learn more about keybindings, visit https://aka.ms/terminal-keybindings
  "keybindings": [
    // Copy and paste are bound to Ctrl+Shift+C and Ctrl+Shift+V in your defaults.json.
    // These two lines additionally bind them to Ctrl+C and Ctrl+V.
    // To learn more about selection, visit https://aka.ms/terminal-selection
    {
      "command": {
        "action": "copy",
        "singleLine": false
      },
      "keys": "ctrl+c"
    },
    {
      "command": "paste",
      "keys": "ctrl+v"
    },

    // Press Ctrl+Shift+F to open the search box
    {
      "command": "find",
      "keys": "ctrl+shift+f"
    },

    // Press Alt+Shift+D to open a new pane.
    // - "split": "auto" makes this pane open in the direction that provides the most surface area.
    // - "splitMode": "duplicate" makes the new pane use the focused pane's profile.
    // To learn more about panes, visit https://aka.ms/terminal-panes
    {
      "command": {
        "action": "splitPane",
        "split": "auto",
        "splitMode": "duplicate"
      },
      "keys": "alt+shift+d"
    }
  ]
}

最终配置下过如下图:
win10 安装 ubuntu20(WSL) 并安装 swift5.2 使用 vscode 开发_第2张图片
需要注意的是如果安装了zsh,某些主题需要特殊的字体,则需要安装powerline相关的字体。

Ubuntu20.04中安装swift

下载Ubuntu对应版本的swift下载地址,下载完成后进行解压,将解压后的目录配置到 .bashrc 文件当中:

# 添加swift目录
export PATH=/mnt/f/WSL/ubuntu20/swift-5.2.4-RELEASE-ubuntu20.04/usr/bin:$PATH

# 运行版本查看命令
swift --version
# 显示以下内容
Swift version 5.2.4 (swift-5.2.4-RELEASE)
Target: x86_64-unknown-linux-gnu

最后安利下自己的小程序,一款程序员刷题小程序。

你可能感兴趣的:(swiftUI)