Muokkaa

Jaa


Install React on Windows Subsystem for Linux

This guide will walk through installing React on a Linux distribution (ie. Ubuntu) running on the Windows Subsystem for Linux (WSL) using the vite frontend tooling.

We recommend following these instructions if you are creating a single-page app (SPA) that you would like to use Bash commands or tools with and/or plan to deploy to a Linux server or use Docker containers. If you are brand new to React and just interested in learning, you may want to consider installing with vite directly on Windows.

For more general information about React, deciding between React (web apps), React Native (mobile apps), and React Native for Desktop (desktop apps), see the React overview.

Prerequisites

  • Install the latest version of Windows 10 (Version 1903+, Build 18362+) or Windows 11
  • Install Windows Subsystem for Linux (WSL), including a Linux distribution (like Ubuntu) and make sure it is running in WSL 2 mode. You can check this by opening PowerShell and entering: wsl -l -v
  • Install Node.js on WSL 2: These instructions use Node Version Manager (nvm) for installation, you will need a recent version of NodeJS to run vite, as well as a recent version of Node Package Manager (npm).

Important

Installing a Linux distribution with WSL will create a directory for storing files: \\wsl\Ubuntu-20.04 (substitute Ubuntu-20.04 with whatever Linux distribution you're using). To open this directory in Windows File Explorer, open your WSL command line, select your home directory using cd ~, then enter the command explorer.exe . Be careful not to install NodeJS or store files that you will be working with on the mounted C drive (/mnt/c/Users/yourname$). Doing so will significantly slow down your install and build times.

Install React

To install the full React toolchain on WSL, we recommend using vite.

  1. Open a WSL command line (ie. Ubuntu).

  2. Create a new project folder: mkdir ReactProjects and enter that directory: cd ReactProjects.

  3. Install React using vite :

    npm create vite@latest my-react-app -- --template react
    
  4. Once installed, change directories into your new app ("my-react-app" or whatever you've chosen to call it): cd my-react-app, install the dependencies: npm install and then start your local development server: npm run dev

    This command will start up the Node.js server and launch a new browser window displaying your app. You can use Ctrl + c to stop running the React app in your command line.

Note

Vite includes a frontend build pipeline using Babel, esbuild and Rollup, but doesn't handle backend logic or databases. If you are seeking to build a server-rendered website with React that uses a Node.js backend, we recommend installing Next.js, rather than this Vite installation, which is intended more for single-page apps(SPAs). You also may want to consider installing Gatsby if you want to build a static content-oriented website.

  1. When you're ready to deploy your web app to production, running npm run build to create a build of your app in the "dist" folder. You can learn more in the Deploying a Static Site.

Additional resources