Tutorial

Deploying an Angular CLI App to Production with Firebase

Draft updated on Invalid Date
Default avatar

By Chris on Code

Deploying an Angular CLI App to Production with Firebase

This tutorial is out of date and no longer maintained.

Introduction

Building Angular apps is a fun project. Once you’re done building, it’s time to expand the fun by sharing your project with the world.

Firebase Home Page

While there are many ways to host our Angular application, we’ll be looking at how to host with Firebase since Firebase makes it super simple. Firebase offers many great features (and has a solid free plan).

  • Realtime Database
  • Hosting
  • Authentication
  • Cloud Functions
  • Cloud Storage
  • Bunch More

We’ll just be using Firebase hosting to handle our Angular app.

  • The Angular CLI will create our dist files: ng build --prod
  • We’ll deploy using Firebase command line tools: firebase deploy
  • Firebase will host and handle routing!

Creating an Angular App

We’ll be using the Angular CLI to create our Angular app that we’ll deploy. For more info on the CLI, check out our article: Getting Started With Angular Using the Angular CLI.

Angular CLI Home Page

Install the CLI

If you don’t already have the CLI installed, we can do it using Node and npm.

  1. npm install -g @angular/cli

Note: The -g flag is to install globally across our entire machine.

Start an Angular App

Once we have the CLI, we can create a new Angular app and test it out using the following:

  1. ng new hosty-app

Navigate to the project directory:

  1. cd hosty-app

Serve it locally and see it in action:

  1. ng serve --open

ng serve

Angular CLI Test App

Our app is good to go! Let’s start setting up our Firebase app.

Creating the Firebase App

We’ll need to create a Firebase account and a Firebase app so that our application knows where to deploy to. Once you log in to your Firebase console, create a new app.

Firebase Dashboard

We’ll name our app Angular Hosting Test. You can also set the subdomain for your app. Your app will be hosted at subdomain.firebase-app.com.

Create a Firebase Project

Our new app is live! That’s all we had to do in the Firebase dashboard. The rest of the work will be done on our own computer in our applications.

Installing the Firebase Tools

In order to deploy to Firebase, we’ll need the Firebase CLI tools. Here are the steps we’re about to take:

  • Download the tools
  • Login to Firebase using the tools
  • Link our local app to our Firebase app

Install the Firebase Tools

We’ll use npm to install similar to how we installed the Angular CLI:

  1. npm install -g firebase-tools

Login to Firebase

We’ll use the CLI tools to log in to Firebase.

  1. firebase login

This will open your browser and allow us to log in from there.

Firebase CLI Login

Firebase Login Successful

That’s it! We’re all ready to start using the Firebase tools.

Using Firebase In Our Angular App

The first step is to link our local Angular app to our Firebase app we created in the Firebase dashboard.

  1. firebase init

Here are the answers to the questions Firebase tools will ask:

Are you ready to proceed? Yes
Which Firebase CLI features? Hosting (In the future, use whatever you need! Press *space* to select.)
Select a default Firebase project? Angular Hosting Test (Choose whatever app you created in the earlier steps)
What do you want to use as your public directory? dist (This is important! Angular creates the `dist` folder.)
Configure as a single-page app? Yes

Firebase Init

You may be asking, what exactly does this command do? This creates two new files:

.firebaserc: Sets the Firebase project we’re linking to

{
  "projects": {
    "default": "angular-hosting"
  }
}

firebase.json: Sets the hosting folder and rewrites

{
  "hosting": {
    "public": "dist",
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      }
    ]
  }
}

Our app is now linked to our Firebase app. We’re ready to finally deploy!

Deploying

The deploy is essentially two steps:

  • Create the production files in the dist folder
  • Deploy to Firebase!

Build Our Angular App

We’ll create our production files using the Angular CLI command:

  1. ng build --prod

This will create a brand new dist/ folder in our project with the files necessary to launch our app. You could take these files and open the index.html file locally and see your app running!

ng build

Angular CLI Dist Files

Deploy to Firebase

Now that we have the dist/ folder, we can tell Firebase to deploy it:

  1. firebase deploy

firebase deploy

Our app is now deployed and ready to view! Run this Firebase command to see it in action:

  1. firebase open hosting:site

Firebase Hosting Dashboard

You can visit the Firebase dashboard to see the logs of all your deployments. You can even set a custom domain in the dashboard.

Firebase Hosting Dashboard

Extra: Script to Deploy

If you want to automate the process of building and deploying, you can add a script to your package.json file:

"scripts": {
  ...
  "deploy": "ng build --prod && firebase deploy"
},

Now we can run the deploy script with:

  1. npm run deploy

Watch as both commands run and our site is updated!

Conclusion

With Firebase hosting, it’s an easy process to deploy our Angular CLI apps to production. In the future, we’ll be exploring more options with other hosting providers, but Firebase is a fast, free, and easy way to host quick Angular apps.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
Chris on Code

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel