React Native Installation Guide on Windows

React Native Installation Guide on Windows

Installation Guide of React Native with Code Snippets

React Native is a popular framework for building cross-platform mobile applications using JavaScript and React. In this tutorial, we will guide you through the process of setting up React Native on a Windows device using Chocolatey package manager. We will also show you how to initialize a new React Native project after installation.

Prerequisites

Before we begin, make sure your Windows device meets the following requirements:

  • Windows 7 or later (64-bit)

  • Node.js (version 8 or later)

  • JDK (version 8 or later)

  • Android Studio (version 3.0 or later) with Android SDK and Android Virtual Device (AVD) installed

Step 1: Install Chocolatey

Chocolatey is a package manager for Windows that makes it easy to install and manage software packages. To install Chocolatey, open PowerShell as an administrator and run the following command:

Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

This command will download and install Chocolatey on your Windows device.

If this does not work, then you can use this also

@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"

Step 2: Install React Native CLI

After installing Chocolatey, open PowerShell as an administrator and run the following command to install the React Native CLI:

choco install -y nodejs-lts microsoft-openjdk11

This command will install Node.js, Python 2, JDK 8, and the React Native CLI on your Windows device.

Extra :- Download and Install Android Studio

1. Install Android Studio

Download and install Android Studio. While on Android Studio installation wizard, make sure the boxes next to all of the following items are checked:

  • Android SDK

  • Android SDK Platform

  • Android Virtual Device

  • If you are not already using Hyper-V: Performance (Intel ® HAXM) (See here for AMD or Hyper-V)

Then, click "Next" to install all of these components.

If the checkboxes are grayed out, you will have a chance to install these components later on.

Once setup has finalized and you're presented with the Welcome screen, proceed to the next step.

2. Install the Android SDK

Android Studio installs the latest Android SDK by default. Building a React Native app with native code, however, requires the Android 13 (Tiramisu) SDK in particular. Additional Android SDKs can be installed through the SDK Manager in Android Studio.

To do that, open Android Studio, click on "More Actions" button and select "SDK Manager".

Android Studio Welcome

The SDK Manager can also be found within the Android Studio "Preferences" dialog, under Appearance & BehaviorSystem SettingsAndroid SDK.

Select the "SDK Platforms" tab from within the SDK Manager, then check the box next to "Show Package Details" in the bottom right corner. Look for and expand the Android 13 (Tiramisu) entry, then make sure the following items are checked:

  • Android SDK Platform 33

  • Intel x86 Atom_64 System Image or Google APIs Intel x86 Atom System Image

Next, select the "SDK Tools" tab and check the box next to "Show Package Details" here as well. Look for and expand the Android SDK Build-Tools entry, then make sure that 33.0.0 is selected.

Finally, click "Apply" to download and install the Android SDK and related build tools.

3. Configure the ANDROID_HOME environment variable

The React Native tools require some environment variables to be set up in order to build apps with native code.

  1. Open the Windows Control Panel.

  2. Click on User Accounts, then click User Accounts again

  3. Click on Change my environment variables

  4. Click on New... to create a new ANDROID_HOME user variable that points to the path to your Android SDK:

ANDROID_HOME Environment Variable

The SDK is installed, by default, at the following location:

%LOCALAPPDATA%\Android\Sdk

You can find the actual location of the SDK in the Android Studio "Settings" dialog, under Appearance & BehaviorSystem SettingsAndroid SDK.

Open a new Command Prompt window to ensure the new environment variable is loaded before proceeding to the next step.

  1. Open powershell

  2. Copy and paste Get-ChildItem -Path Env:\ into powershell

  3. Verify ANDROID_HOME has been added

4. Add platform-tools to Path

  1. Open the Windows Control Panel.

  2. Click on User Accounts, then click User Accounts again

  3. Click on Change my environment variables

  4. Select the Path variable.

  5. Click Edit.

  6. Click New and add the path to platform-tools to the list.

The default location for this folder is:

%LOCALAPPDATA%\Android\Sdk\platform-tools

React Native Command Line Interface

React Native has a built-in command line interface. Rather than install and manage a specific version of the CLI globally, we recommend you access the current version at runtime using npx, which ships with Node.js. With npx react-native <command>, the current stable version of the CLI will be downloaded and executed at the time the command is run.

Creating a new application

If you previously installed a global react-native-cli package, please remove it as it may cause unexpected issues:

npm uninstall -g react-native-cli @react-native-community/cli

React Native has a built-in command line interface, which you can use to generate a new project. You can access it without installing anything globally using npx, which ships with Node.js. Let's create a new React Native project called "AwesomeProject":

npx react-native@latest init AwesomeProject

This is not necessary if you are integrating React Native into an existing application, if you "ejected" from Expo, or if you're adding Android support to an existing React Native project (see Integration with Existing Apps). You can also use a third-party CLI to init your React Native app, such as Ignite CLI.

[Optional] Using a specific version or template

If you want to start a new project with a specific React Native version, you can use the --version argument:

npx react-native@X.XX.X init AwesomeProject --version X.XX.X

You can also start a project with a custom React Native template with the --template argument.

Preparing the Android device

You will need an Android device to run your React Native Android app. This can be either a physical Android device, or more commonly, you can use an Android Virtual Device which allows you to emulate an Android device on your computer.

Either way, you will need to prepare the device to run Android apps for development.

Using a physical device

If you have a physical Android device, you can use it for development in place of an AVD by plugging it in to your computer using a USB cable and following the instructions here.

Using a virtual device

If you use Android Studio to open ./AwesomeProject/android, you can see the list of available Android Virtual Devices (AVDs) by opening the "AVD Manager" from within Android Studio. Look for an icon that looks like this:

Android Studio AVD Manager

If you have recently installed Android Studio, you will likely need to create a new AVD. Select "Create Virtual Device...", then pick any Phone from the list and click "Next", then select the Tiramisu API Level 33 image.

If you don't have HAXM installed, click on "Install HAXM" or follow these instructions to set it up, then go back to the AVD Manager.

Click "Next" then "Finish" to create your AVD. At this point you should be able to click on the green triangle button next to your AVD to launch it, then proceed to the next step.

Step 3: Initialize a New React Native Project

After installing the React Native CLI, you can initialize a new React Native project by running the following command:

npx react-native@latest init AwesomeProject

Replace "MyProject" with the name of your project. This command will create a new React Native project with the specified name in the current directory.

Step 4: Run the React Native App

To run the React Native app, open PowerShell as an administrator and navigate to the project directory by running the following command:

cd AwesomeProject

Replace "MyProject" with the name of your project. This command will change the current directory to your project directory.

Next, start the development server by running the following command:

npx react-native start

This command will start the development server for your React Native app.

Finally, open another PowerShell window as an administrator and run the following command to launch the app in the Android emulator:

npx react-native run-android

This command will launch the app in the Android emulator.

Congratulations! You have successfully set up React Native on your Windows device and created a new React Native project. You can now start building your own cross-platform mobile apps using React Native.