UbuntuUbuntu Based

How To Install TypeScript on Ubuntu 24.04 LTS

Install TypeScript on Ubuntu 24.04

TypeScript has gained immense popularity among developers for its ability to enhance JavaScript with static typing, making it easier to build robust applications. As the demand for TypeScript continues to rise, knowing how to install it on your system is essential, especially for users of Ubuntu 24.04 LTS. This guide will provide a comprehensive, step-by-step approach to installing TypeScript, ensuring you have everything you need to start developing with this powerful language.

What is TypeScript?

TypeScript is a superset of JavaScript that adds optional static typing and other features to the language. Developed by Microsoft, it aims to improve the development experience by catching errors during compile time rather than runtime. This capability is particularly beneficial in large-scale applications where maintaining code quality is crucial.

Some key features of TypeScript include:

  • Static Typing: Allows developers to define variable types, reducing runtime errors.
  • Interfaces: Provides a way to define contracts within your code, ensuring that classes adhere to specific structures.
  • Enhanced Tooling: Offers better autocompletion and navigation in IDEs, improving developer productivity.

Unlike JavaScript, which is dynamically typed, TypeScript’s static typing helps catch mistakes early in the development process. This makes TypeScript an excellent choice for both small projects and large enterprise applications.

Prerequisites

Before installing TypeScript on Ubuntu 24.04 LTS, ensure that you meet the following prerequisites:

  • A basic understanding of the Linux command line.
  • An active internet connection for downloading necessary packages.
  • A non-root user account with sudo privileges.

Having these prerequisites in place will streamline the installation process and help avoid common pitfalls.

Step 1: Update Your Ubuntu System

The first step in installing TypeScript is to ensure that your Ubuntu system is up-to-date. Keeping your system updated is crucial for security and compatibility reasons. Open your terminal and run the following commands:

sudo apt update
sudo apt upgrade

The first command updates the package lists for upgrades and new package installations, while the second command upgrades all installed packages to their latest versions. This process may take some time depending on your system’s current state and internet speed.

Step 2: Install Node.js

Node.js is a JavaScript runtime built on Chrome’s V8 engine and is essential for running TypeScript applications. To install Node.js on Ubuntu 24.04 LTS, follow these steps:

  • Add the NodeSource repository by running:
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
  • This command fetches the setup script for Node.js version 20.x and executes it.
  • Now install Node.js with the following command:
sudo apt install -y nodejs

The `-y` flag automatically confirms the installation prompts. After installation, verify that Node.js and npm (Node Package Manager) are installed correctly by checking their versions:

node --version
npm --version

If both commands return version numbers, you have successfully installed Node.js.

Step 3: Install TypeScript

With Node.js installed, you can now install TypeScript globally using npm. This allows you to access the TypeScript compiler from anywhere on your system. Execute the following command in your terminal:

npm install -g typescript

The `-g` flag indicates a global installation, making TypeScript available system-wide. After installation, confirm that TypeScript has been installed correctly by checking its version:

tsc --version

If you see a version number displayed, TypeScript is ready for use!

Step 4: Creating a Simple TypeScript Project

Now that you have TypeScript installed, it’s time to create a simple project to get familiar with its features. Follow these steps:

  • Create a new directory for your project:
mkdir my-typescript-project
cd my-typescript-project
  • Initialize npm in your project directory:
npm init -y

This command creates a `package.json` file with default settings for your project. Next, install TypeScript locally within your project by running:

npm install typescript ts-node @types/node --save-dev

The `ts-node` package allows you to run TypeScript files directly without manually compiling them each time, while `@types/node` provides type definitions for Node.js.

  • Create a `tsconfig.json` file for project configuration:
npx tsc --init

This command generates a default `tsconfig.json` file in your project directory. This file contains various compiler options that dictate how TypeScript should behave when compiling your code.

Step 5: Writing Your First TypeScript Code

You are now ready to write your first TypeScript code! Create a new file named `app.ts` in your project directory:

touch app.ts

Edit `app.ts` using your preferred text editor (e.g., nano or vim) and add the following code snippet:

// app.ts
function greet(name: string): string {
    return `Hello, ${name}!`;
}

console.log(greet('World'));

This simple function takes a name as input and returns a greeting message. To run this code using ts-node, execute the following command in your terminal:

npx ts-node app.ts

You should see “Hello, World!” printed in the terminal, indicating that your TypeScript code has executed successfully.

Common Issues and Troubleshooting

While installing TypeScript on Ubuntu 24.04 LTS is generally straightforward, users may encounter some common issues. Here are some troubleshooting tips:

    • Error: Permission Denied: If you encounter permission errors while installing packages globally using npm, try using sudo:
sudo npm install -g typescript
  • Error: Command Not Found: If you receive an error stating that `tsc` or `ts-node` is not found after installation, ensure that npm’s global bin directory is included in your PATH environment variable. You can add it by appending the following line to your ~/.bashrc or ~/.profile file:
    export PATH=$PATH:$(npm bin -g)

    Then run:

    source ~/.bashrc
  • Error: Version Conflicts: If you experience issues related to version conflicts between Node.js and npm packages, consider using nvm (Node Version Manager) to manage different versions of Node.js easily.
  • No Output from ts-node: If running ts-node does not produce any output or throws an error related to missing types or modules, double-check that all required packages are installed correctly and that your code does not contain syntax errors.

Congratulations! You have successfully installed TypeScript. Thanks for using this tutorial for installing TypeScript programming language on the Ubuntu 24.04 LTS system. For additional help or useful information, we recommend you check the official TypeScript website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button