Skip to main content
RapidDev - Software Development Agency
firebase-tutorial

How to Use Firebase CLI for Deployment

The Firebase CLI lets you deploy hosting, functions, Firestore rules, and other services from your terminal. Install it with npm install -g firebase-tools, authenticate with firebase login, initialize your project with firebase init, and deploy with firebase deploy. Use the --only flag for selective deployment and .firebaserc for managing multiple project environments like staging and production.

What you'll learn

  • How to install and authenticate the Firebase CLI
  • How to initialize a Firebase project with firebase init
  • How to deploy hosting, functions, and rules selectively
  • How to manage multiple environments with project aliases
Book a free consultation
4.9Clutch rating
600+Happy partners
17+Countries served
190+Team members
Intermediate8 min read15-20 minFirebase CLI v13+, Node.js 18+, all Firebase plansMarch 2026RapidDev Engineering Team
TL;DR

The Firebase CLI lets you deploy hosting, functions, Firestore rules, and other services from your terminal. Install it with npm install -g firebase-tools, authenticate with firebase login, initialize your project with firebase init, and deploy with firebase deploy. Use the --only flag for selective deployment and .firebaserc for managing multiple project environments like staging and production.

Deploying with the Firebase CLI

The Firebase CLI is the primary tool for deploying Firebase Hosting, Cloud Functions, Firestore and RTDB rules, and Storage rules from your local development environment. This tutorial walks you through installing the CLI, logging in, initializing your project configuration, deploying individual services, using preview channels for staging, and setting up multiple project aliases for environment management.

Prerequisites

  • Node.js 18 or later installed on your machine
  • A Firebase project created in the Firebase Console
  • A web app or Cloud Functions ready to deploy
  • Terminal or command-line access

Step-by-step guide

1

Install the Firebase CLI

Install the Firebase CLI globally using npm. This gives you the firebase command in your terminal. After installation, verify it works by checking the version. The CLI is regularly updated with new features and bug fixes, so keep it current. If you previously installed an older version, the same command upgrades it.

typescript
1# Install or update the Firebase CLI
2npm install -g firebase-tools
3
4# Verify installation
5firebase --version
6
7# Expected output: 13.x.x or later

Expected result: The firebase command is available in your terminal and shows the current version number.

2

Authenticate with Firebase

Run firebase login to authenticate with your Google account. This opens a browser window where you sign in and authorize the CLI to access your Firebase projects. The CLI stores the auth token locally so you only need to log in once per machine. For CI/CD environments where no browser is available, use firebase login:ci to generate a token that can be used with the FIREBASE_TOKEN environment variable.

typescript
1# Interactive login (opens browser)
2firebase login
3
4# Verify you're logged in and see available projects
5firebase projects:list
6
7# For CI/CD: generate a CI token
8firebase login:ci
9# Then use: FIREBASE_TOKEN=your-token firebase deploy

Expected result: You are authenticated and firebase projects:list shows your Firebase projects.

3

Initialize Firebase in your project directory

Navigate to your project directory and run firebase init. The CLI walks you through selecting which Firebase features to configure: Hosting, Functions, Firestore Rules, Realtime Database Rules, Storage Rules, and Emulators. For each feature, it creates the necessary configuration files. Choose an existing Firebase project or create a new one. The command creates firebase.json (deployment config) and .firebaserc (project aliases).

typescript
1# Navigate to your project directory
2cd your-project
3
4# Initialize Firebase (interactive wizard)
5firebase init
6
7# Or initialize specific features only
8firebase init hosting
9firebase init functions
10firebase init firestore
11firebase init emulators

Expected result: Your project directory contains firebase.json and .firebaserc with your selected features configured.

4

Configure firebase.json for deployment

The firebase.json file controls what gets deployed and how. For Hosting, it specifies the public directory (where your build output goes), rewrites for SPAs, custom headers, and redirects. For Functions, it points to the functions directory. For Firestore and RTDB, it specifies the rules files. Review and customize this file to match your project structure.

typescript
1{
2 "hosting": {
3 "public": "dist",
4 "ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
5 "rewrites": [
6 { "source": "**", "destination": "/index.html" }
7 ],
8 "headers": [
9 {
10 "source": "**/*.@(js|css)",
11 "headers": [
12 { "key": "Cache-Control", "value": "max-age=31536000" }
13 ]
14 }
15 ]
16 },
17 "functions": {
18 "source": "functions",
19 "runtime": "nodejs20"
20 },
21 "firestore": {
22 "rules": "firestore.rules",
23 "indexes": "firestore.indexes.json"
24 },
25 "database": {
26 "rules": "database.rules.json"
27 },
28 "storage": {
29 "rules": "storage.rules"
30 }
31}

Expected result: firebase.json is configured with the correct public directory, rewrites, and paths to rules files.

5

Deploy all services or select specific ones

Run firebase deploy to deploy all configured services at once, or use the --only flag to deploy specific services. Selective deployment is faster and safer because it only changes what you intend to update. You can deploy multiple specific targets by separating them with commas. Always build your frontend project before deploying Hosting.

typescript
1# Deploy everything
2firebase deploy
3
4# Deploy only hosting
5npm run build && firebase deploy --only hosting
6
7# Deploy only Cloud Functions
8firebase deploy --only functions
9
10# Deploy a specific function
11firebase deploy --only functions:myFunction
12
13# Deploy only Firestore rules and indexes
14firebase deploy --only firestore
15
16# Deploy multiple targets
17firebase deploy --only hosting,functions
18
19# Deploy with a message for version tracking
20firebase deploy --only hosting -m "v2.1.0 - Added user profiles"

Expected result: The selected services are deployed to your Firebase project and the CLI shows the deployment URL.

6

Use preview channels for staging

Firebase Hosting preview channels let you deploy to temporary URLs for testing before going to production. Each channel gets a unique URL that expires after 7 days by default. Share these URLs with your team for review. When ready, deploy to production with the standard deploy command.

typescript
1# Deploy to a preview channel
2firebase hosting:channel:deploy staging
3
4# Deploy with a custom expiration (30 days)
5firebase hosting:channel:deploy staging --expires 30d
6
7# List all active channels
8firebase hosting:channel:list
9
10# Delete a preview channel
11firebase hosting:channel:delete staging

Expected result: A temporary preview URL is generated where your team can test the deployment before going to production.

7

Set up project aliases for multiple environments

Use .firebaserc to define aliases for different Firebase projects, such as development, staging, and production. Switch between environments with firebase use and deploy to the correct project each time. This prevents accidental deployments to production. Each alias maps to a different Firebase project ID.

typescript
1// .firebaserc
2{
3 "projects": {
4 "default": "my-app-dev",
5 "staging": "my-app-staging",
6 "production": "my-app-prod"
7 }
8}
9
10// Switch between environments
11// firebase use staging
12// firebase deploy --only hosting
13
14// Or deploy to a specific alias without switching
15// firebase deploy --only hosting --project production

Expected result: You can switch between project environments and deploy to the correct Firebase project using aliases.

Complete working example

deploy.sh
1#!/bin/bash
2# Firebase deployment script with environment support
3# Usage: ./deploy.sh [environment] [service]
4# Examples:
5# ./deploy.sh production hosting
6# ./deploy.sh staging functions
7# ./deploy.sh production all
8
9ENV=${1:-"staging"}
10SERVICE=${2:-"all"}
11
12echo "Deploying to: $ENV"
13echo "Service: $SERVICE"
14
15# Validate environment
16if [[ "$ENV" != "staging" && "$ENV" != "production" ]]; then
17 echo "Error: Environment must be 'staging' or 'production'"
18 exit 1
19fi
20
21# Production safety check
22if [[ "$ENV" == "production" ]]; then
23 read -p "Are you sure you want to deploy to PRODUCTION? (y/n) " -n 1 -r
24 echo
25 if [[ ! $REPLY =~ ^[Yy]$ ]]; then
26 echo "Deployment cancelled."
27 exit 0
28 fi
29fi
30
31# Build frontend if deploying hosting
32if [[ "$SERVICE" == "hosting" || "$SERVICE" == "all" ]]; then
33 echo "Building frontend..."
34 npm run build
35 if [ $? -ne 0 ]; then
36 echo "Build failed. Aborting deployment."
37 exit 1
38 fi
39fi
40
41# Deploy
42if [[ "$SERVICE" == "all" ]]; then
43 firebase deploy --project "$ENV"
44else
45 firebase deploy --only "$SERVICE" --project "$ENV"
46fi
47
48if [ $? -eq 0 ]; then
49 echo "Deployment to $ENV ($SERVICE) complete!"
50else
51 echo "Deployment failed."
52 exit 1
53fi

Common mistakes when using Firebase CLI for Deployment

Why it's a problem: Deploying hosting without running the build command first, resulting in stale or missing files

How to avoid: Always run your build command (npm run build) before firebase deploy --only hosting. Add it to your deployment script to automate this.

Why it's a problem: Deploying all services when only one changed, which is slow and risky

How to avoid: Use firebase deploy --only hosting or firebase deploy --only functions to deploy only what changed. This is faster and reduces the risk of unintended changes.

Why it's a problem: Deploying to production instead of staging because the wrong project alias is active

How to avoid: Always use the --project flag explicitly in CI/CD: firebase deploy --project my-app-staging. Check the active project with firebase use before deploying interactively.

Why it's a problem: Running firebase init in a directory that already has a firebase.json, accidentally overwriting configuration

How to avoid: The CLI warns before overwriting existing files, but always back up your firebase.json before running firebase init again. Use firebase init [feature] to add a single feature without touching existing config.

Best practices

  • Use the --only flag to deploy specific services instead of deploying everything at once
  • Always build your frontend before deploying hosting: npm run build && firebase deploy --only hosting
  • Set up project aliases in .firebaserc for development, staging, and production environments
  • Use preview channels for staging deployments with firebase hosting:channel:deploy
  • Add a deployment script that includes build steps, environment selection, and confirmation prompts
  • Keep the Firebase CLI updated with npm install -g firebase-tools for the latest features and fixes
  • Use firebase login:ci and FIREBASE_TOKEN for automated CI/CD deployments
  • Deploy Cloud Functions in batches of 10 or fewer to avoid API rate limits

Still stuck?

Copy one of these prompts to get a personalized, step-by-step explanation.

ChatGPT Prompt

Walk me through setting up the Firebase CLI for a project that uses Hosting, Cloud Functions, and Firestore rules. Show me how to configure firebase.json, set up staging and production environments with project aliases, and write a deployment script.

Firebase Prompt

Write a bash deployment script for Firebase that supports environment selection (staging/production), selective service deployment (hosting, functions, or all), automatic frontend builds before hosting deployment, and a production safety confirmation prompt.

Frequently asked questions

Do I need to install the Firebase CLI globally?

No. You can use npx firebase-tools to run CLI commands without a global install. However, global installation (npm install -g firebase-tools) is more convenient for frequent use and avoids re-downloading on every command.

How do I deploy to a different Firebase project?

Use the --project flag: firebase deploy --project my-other-project. Or set up aliases in .firebaserc and switch with firebase use my-alias. This is essential for managing staging and production environments.

Can I deploy from a CI/CD pipeline?

Yes. Generate a CI token with firebase login:ci and set it as the FIREBASE_TOKEN environment variable in your CI/CD system. Then run firebase deploy --project your-project in your pipeline. Most CI systems like GitHub Actions, CircleCI, and GitLab CI have Firebase deployment templates.

What does firebase deploy --only functions:myFunction do?

It deploys only the specific function named myFunction instead of all functions. This is faster and safer for making changes to a single function in production without affecting others.

How long does a hosting deployment take?

Typically 30 seconds to 2 minutes depending on the size of your build output and network speed. The Firebase CDN propagates changes globally within a few minutes after deployment completes.

What happens if a deployment fails halfway through?

Firebase deploys services atomically. If hosting deployment fails, your previous version remains live. If function deployment fails, already-deployed functions from that batch may have been updated, but other batches remain unchanged. Always deploy functions selectively to minimize risk.

Can RapidDev help set up a CI/CD pipeline for Firebase deployment?

Yes. RapidDev can configure automated deployment pipelines with GitHub Actions, staging preview channels, production approval gates, and rollback strategies for your Firebase project.

RapidDev

Talk to an Expert

Our team has built 600+ apps. Get personalized help with your project.

Book a free consultation

Need help with your project?

Our experts have built 600+ apps and can accelerate your development. Book a free consultation — no strings attached.

Book a free consultation

We put the rapid in RapidDev

Need a dedicated strategic tech and growth partner? Discover what RapidDev can do for your business! Book a call with our team to schedule a free, no-obligation consultation. We'll discuss your project and provide a custom quote at no cost.